CS46A Spring 18

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 Util. Notice how the methods are invoked in UtilTester.

Util is a utility class. It has no instance variables and should not have a constructor. Its purpose is to house some useful static methods

Notice that there are two methods called average and two methods called containsThrice. 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 Util. The Javadoc utility works on static methods, too.

For the draft, implement first the average method

10A draft:
10A final:

10B

Now we are going to use the design pattern for collecting objects. We are going to model a Orchard with trees. An Orchard uses an ArrayList to keep track of Tree objects. You will write both a Orchard class and a Tree class.

Tree has a type and a height. Provide a constructor that takes type and height, in that order. Provide getters and setters for the instance variables. This is the design pattern for managing properties of objects.

An Orchard has a constructor that takes no parameters. Remember it must initialize the instance variable

It has methods

Provide Javadoc for both classes.

For the draft: implement the Tree class

Note: The Tree class will not change in the final, but you will need to submit it again so that Orchard class can find it.

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 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 so on. When the Toddler sleeps, its state changes. 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 eat and 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

10C draft:
10C final: