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
public static double average(int[] numbers) gets the average of the values values in the array or 0 if the array has no contents. Use the enhanced for looppublic static double average(ArrayList<Integer> numbers) Gets the average of the values in the ArrayList or 0 if the ArrayList is empty. Use the enhanced for looppublic static boolean containsThrice(int[] list, int target) determines if the target is in the array exactly three times. Returns true if it is, otherwise returns falsepublic static boolean containsThrice(ArrayList<Integer> list, int target) determines if the target is in the ArrayList exactly three times. Returns true if it is, otherwise returns falseNotice 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
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.
A 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
add() Adds the specified Tree to the Orchardtallest() Gets the type of the tallest Tree in the Orchard or null if the Orchard is empty. Initialize the tallest with the first element.contains() determines if a Tree of the given type is in the Orchard. Returns true if a Tree of the given type is in the Orchard. Otherwise false.treeList() gets an ArrayList<String> containing the types of all the Trees in the OrchardProvide 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.
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 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:
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