8A
Write a class RandomPlayer
which performs some tests using a Random
object.
RandomPlayer
has this constructor:
public RandomPlayer(Random generator, int upperBound, int numberOfIterations)
where
generator
is the Random objectupperBound
is the maximum value of the generated numbers. If upperBound is 50, then generate random ints greater than or equal to 0 and less than 50 in the methods. numberOfIterations
is the number of random ints to generate (the number of times to run your loop).It has these methods
public double average()
- generates the specified number of random ints and gets the average of the values generarated.public int countGreaterThanMidValue()
- generates the specified number of random ints and counts how many are greater than the upperBound / 2. You would expect it to be about half the time, but is it? public int count(int value)
- generates the specified number of random ints and gets the number of times the value appears public void setIterations( int numberOfIterations)
sets the number of iterations for this objectProvide Javadoc
For the draft: provide the constructor. In the average method, just generate and return the first random int. Provide the setIterations method.
8B
Avengers is a universe of characters created by Marvel. They first appeared as comic books and are now in movies.
There is no starter file this time. You create a class called AvengersList
with a main method. It has no instance variables. In the main method, do the following
add
"Captian America", "Hulk", "Iron Man", "Thor" in that order.add
"Black Panther" at position 1set
methodset
method. Do this in a manner that would replace the next to the last element, no matter the size of the array list. Think about how to find the index of the last element in an array list, then find the index of the next to the last element and use that in the set
method. Don't just use 3 in the set methodremove
"Thor" Remove the object by name. Do not use the index. We want to remove Thor no matter where it is in the ArrayList. (See the note below)get
and print the 0th element followed by "***"Note: You must use an ArrayList and its methods. Do not fake it by just printing the correct strings. You will need to know how to use these methods in a situation where the output can not be faked.
Note: There is a version of remove method that will remove a specific object. Use that. Here is the code to remove target from the ArrayList called stuff
stuff.remove(target);
Note: To print an ArrayList in the [xxx, yyy, ...] format, use its toString method like this
System.out.println(stuff.toString());
8C
Write a SimpleLineManager
class which manages SimpleLine
objects. A SimpleLine represents a line in 2-d space. It is simpler than the one in the graphics library. Copy it into your project. You can look at the documentation for the class by opening the class in the editor window. On the right-hand side where it says "Source Code", click the down arrow and select "Documentation."
The SimpleLineManager
has an instance variable ArrayList<SimpleLine>
. Call the instance variable lines. It has a no-argument constructor (That means it takes no parameters) which initializes an ArrayList
to an empty ArrayList
of SimpleLines
.
Provide these method
public void add(SimpleLine line)
adds this SimpleLine
to the SimpleLineManager
(adds it to the ArrayList instance variable)public void swap(int index1, int index2)
- swaps the element at index1 with the element at index2. If either index is out of bounds, do not changing anything.public double shortest()
gets the length for the shortest line. If the SimpleLineManager
object is empty, return 0. Remember the Pythagorean theorem. I made a private helper method getLength because I needed to calculate the length in two places and did not want to repeat the workpublic String toString()
gets a string representation of the ArrayList - provided. (This is why the instance variable must be called lines.) Provide Javadoc
For the draft, define the instance variable and implement the constructor. Also implement the add method.