CS46A Spring 19

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

Notice 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

10A draft:
10A final:

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

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
    

10B draft:
10B final:

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.

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:

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.

10C draft:
10C final: