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 StaticPractice
. Notice how the methods are invoked in StaticPracticeTester
.
StaticPractice
is a utility class. It should not have instance variables or a constructor. Its purpose is to house some useful static methods
public static double max(int[] numbers)
Gets the maximum value in the array. If the array is empty, we want to return something to indicate there is no largest number. But any number we pick could be the maximum in some array. So just return this value Integer.MIN_VALUE. It is the smallest possible int. Use the enhanced for looppublic static double max(ArrayList<Integer> numbers)
Gets the maximum value in the ArrayList. If the ArrayList is empty, return Integer.MIN_VALUE. Use the enhanced for looppublic static boolean containsMultiple(String[] list, String target)
determines if the target is in the array more than once. Returns true if it is, otherwise returns false. If the array is empty return falsepublic static boolean containsMultiple(ArrayList<String> list, String target)
determines if the target is in the ArrayList more than once. 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 containsMultiple
. 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 StaticPractice
. The Javadoc utility works on static methods, too.
For the draft, implement first the max method
10B
Now we are going to use the design pattern for collecting objects. We are going to model a Nursery
that sells plants. A Nursery
uses an ArrayList
to keep track of Plant
objects. You will write both a Nursery
class and a Plant
class.
A Plant
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.
A Nursery
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 Plant
to the Nursery
average()
Finds the average cost of all Plants
in the Nursery
. Return 0 if the Nursery has no plantssameContents(Nursery otherNursery)
determines if this Nursery
and otherNursery contain Plants with the same name although perhaps in a different order. Returns true if they have the exactly the same Plants. Otherwise false. (this will be much easier if you use enhanced for loops)plantList()
gets an ArrayList<String> with names of the Plants
in the Nursery
.Provide Javadoc for both classes.
For the draft: implement the Plant
class
Note: The Plant
class will not change in the final, but you will need to submit it again so that Nursery 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 plants) for every plant in this nursery set a boolean flag otherContains to false (assume this plant is not in the other nursery) for every plant in otherNursery if the name of the plant in nursery is the same as the name of the plant in other nursery 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 Wildebeest
class. A Wildebeest
has 4 states. You will define and use these static constants to represent the states.
public static final int NOT_HUNGRY = 5;
public static final int SOMEWHAT_HUNGRY = 2;
public static final int HUNGRY = 1;
public static final int VERY_HUNGRY = 4;
In your code, do not make any assumptions about the values of the constants. That is do not assume that HUNGRY is 1
Wildebeests roam the Serengeti Plain in Tanzania.
(image from Wikipedea)
A Wildebeest
travels across the plain and as it travels, it becomes more hungry. If it is NOT_HUNGRY
, it becomes SOMEWHAT_HUNGRY
. If it is SOMEWHAT_HUNGRY
, it becomes HUNGRY
and so on. When the Wildebeest
sees food, if it is in any of the "hungry states", it will eat and become one level less hungry. If the Wildebeest
is VERY_HUNGRY
when it sees food, it will eat and become HUNGRY
. The next time it sees food, it will eat again and become SOMEWHAT_HUNGRY
. If it is NOT_HUNGRY
, it does not eat and its state does not change.
The state of the Wildebeest
must be one of the four values
The constructor takes no parameters. A Wildebeest
is very hungry when it is creates so the constructor must initialize the state to VERY_HUNGRY
.
Provide methods:
public void travel()
the Wildebeest
becomes more hungry if not already VERY_HUNGRY
public void seeFood()
the Wildebeest
will eat if it is hungry and become less hungrypublic int getState()
Gets the integer representing the state, an integer 1 through 4. public String getHungerLevel()
Gets a string describing the current hunger state of the Wildebeest
: "Not hungry", "Somewhat hungry ,"Hungry", or "Very hungry"Provide Javadoc
For the draft, provide the static constants, implement the constructor, and the getState()
method. Implement the other methods as stubs. Provide Javadoc