10A
In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class StaticUtility
. Notice how the methods are invoked in StaticUtilityTester
.
StaticUtility
is a utility class. It should not have instance variables or a constructor. The purpose of a class like this is to house some useful static methods
public static double max(double[] numbers)
Gets the maximum value in the array. If the array is empty, we want to return something to indicate there is no largest double. But any number we pick could be the maximum in some array. So return this value Double.NEGATIVE_INFINITY. This is a constant in the Double wrapper class. We will use it to indicate that the array way empty. Use the enhanced for looppublic static double max(ArrayList<Double> numbers)
Gets the maximum value in the ArrayList. If the ArrayList is empty, return Double.NEGATIVE_INFINITY. Use the enhanced for looppublic static boolean containsTwice(String[] list, String target)
determines if the target is in the array exactly 2 times. Returns true if it is, otherwise returns false. If the array is empty, return falsepublic static boolean containsTwice(ArrayList<String> list, String target)
determines if the target is in the ArrayList exactly 2 times. Returns true if it is, otherwise returns false. If the ArrayList is empty, return falseNotice that there are two methods called max
and two methods called containsTwice.
These are examples of overloading - methods with the same name but different number and type of parameters. The compiler tells them apart because in each case, one takes an array and one an ArrayList as a parameter.
When using the enhanced for loop, the implementation of the pairs of methods will be very similar. The exact same loop works for both
Provide Javadoc. Look at the documentation produced for StaticUtility
. The Javadoc utility works on static methods, too.
For the draft, implement the first max method
10B
Now we are going to use the design pattern for collecting objects. We are going to model an AmazonOrder
that contains the items in an order. A AmazonOrder
uses an ArrayList
to keep track of Item
objects. You will write both an AmazonOrder
class and a Item
class.
An Item
has a name and a price. Provide a constructor that takes name and price, in that order. Provide getters and setters for the instance variables. This is the design pattern for managing properties of objects.
An AmazonOrder
has a constructor that takes no parameters. Remember the job of the constructor is to initialize the instance variable
It has methods
add()
Adds the specified Item
object to the AmazonOrder
average()
Finds the average cost of all Items
in the AmazonOrder
. Return 0 if the AmazonOrder
has no itemssameContents(AmazonOrder otherOrder)
determines if this AmazonOrder
and otherOrder contain Items with the same name although perhaps in a different order. Returns true if they have the exactly the same Items
. Otherwise false. (this will be much easier if you use enhanced for loops)order()
gets an ArrayList<String> with names of the Items
in the AmazonOrder
.Provide Javadoc for both classes.
For the draft: implement the Item
class
Note: The Item
class will not change in the final, but you will need to submit it again so that AmazonOrder
can find it.
Help on sameContents
: You will used nested loops for this. Here is pseudocode
if they are not the same size return false set a boolean match to true (assume they have all the same items) for every item in this AmazonOrder set a boolean flag otherContains to false (assume this item is not in the other order) for every item in otherOrder if the name of the item in AmazonOrder is the same as the name of the item in otherOrder set otherContains to true (never set it back to false) if otherContains is false set match to false (and never set it back to true) return match
10C
In this problem you will use the design pattern for maintaining state. Write a Toddler
class. A Toddler
has 4 states.(state will be the only instance variable) You will define and use these static constants to represent the states.
public static final int HAPPY = 1;
public static final int SOMEWHAT_CRANKY = 2;
public static final int CRANKY = 4;
public static final int VERY_CRANKY = 7;
In your code, use the constants and do not assume what the value is for any of the constants.
A Toddler
runs around all day, and as it runs, it becomes more tired and cranky. If it is HAPPY
, it becomes SOMEWHAT_CRANKY
. If it is SOMEWHAT_CRANKY
, it becomes CRANKY
and if it is CRANKY,
it becomes VERY_CRANKY
. If it is VERY_CRANKY
, its state does not change. When the Toddler
sleeps, its state becomes less cranky. If it is in any of the "cranky states", it will become one level less cranky. If the Toddler
is VERY_CRANKY
when it sleeps, it will become CRANKY
. If it sleeps some more, it will become SOMEWHAT_CRANKY
. If it is HAPPY
, its state does not change.
The constructor takes no parameters. A Toddler
is CRANKY
when it is born so the constructor must initialize its state to CRANKY
.
Provide methods:
public void run()
the Toddler
becomes more cranky. If it is already VERY_CRANKY
, the state does not changepublic void sleep()
the Toddler
becomes less cranky. If it is already HAPPY
, the state does not changepublic int getState()
Gets the integer representing the statepublic String getMood()
Gets a string describing the current mood of the Toddler
: "Happy", "Somewhat cranky,"Cranky", or "Very cranky"Provide Javadoc
For the draft, provide the static constants, implement the constructor, and the getState()
method. Implement the other methods as stubs. Provide Javadoc. Remember that the Javadoc for a stub says what the method will do in the final version.