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 StuffAndThings
. Notice how the methods are invoked in StuffAndThingsTester
.
StuffAndThings
is a utility class. It has only static methods. Do not give it a constructor or instance variables
Implement these methods:
public static int min(int[] list)
Gets the minimum value in the array. You can assume the array has at least one elementpublic static int min(ArrayList<Integer> list)
Gets the minimum value in the ArrayList as an int. You can assume the ArrayList contains at least one elementpublic static ArrayList<String> wordsEndingIn(ArrayList<String> list, String letter
) Gets an ArrayList of all the words in the given ArrayList that contain the given letter. The comparison is case-insensitive. 'A' and 'a' are counted as the same letterpublic static String[] wordsEndingIn(String[] list, String letter)
Gets an array of all the words in the given array that contain the given letter. The comparison is case-insensitive. 'A' and 'a' are counted as the same letter. You will need to use Arrays.copyOf
() to return an array that contains just the words ending in the letterNotice that there are two methods called min
and two methods called wordsEndingIn
. 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.
If you use the enhanced for loop, the implementation of the pairs of methods will be similar.
Provide Javadoc. Look at the documentation produced for StuffAndThings
. The Javadoc utility works on static methods, too.
For the draft, implement first the min
method
10B
(18 in.) to (22 in.)
Now we are going to use the design pattern for collecting objects. We are going to model a hospital nursery filled with newborn babies. A Nursery
uses an ArrayList
to keep track of Newborn
objects. You will write both a Nursery
class and a Newborn
class.
A Newborn
has a name and a length. Provide a constructor that takes name and length, 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 it must initialize the instance variable
It has methods
add()
Adds the specified Newborn
to the Nursery
remove()
Removes the first Newborn
in the list with the specified name.contains()
determines if a Newborn
with a given name is in the Nursery
. Returns true if a Newborn with that name is in the Nursery. Otherwise false.nameList()
gets an ArrayList names of the Newborn
in the Nursery
.Provide Javadoc for both classes.
For the draft, implement the Newborn
class
Note: The Newborn class will not change in the final, but you will need to submit it again so that Nursery and NurseryTester can find it.
10C
In this problem you will use the design pattern for maintaining state. Write a Fish
class. A Fish
has 4 states. You will define and use these static constants to represent the states
public static final int NOT_HUNGRY = 1;
public static final int SOMEWHAT_HUNGRY = 2;
public static final int HUNGRY = 3;
public static final int VERY_HUNGRY = 4;
A Fish
can swim and when it swims, 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 Fish
sees food, if it is in any of the "hungry states", it will eat and become one level less hungry. If the Fish
is VERY_HUNGRY
when it sees food, it will eat and become HUNGRY
. The next time it sees food, it will eat and become SOMEWHAT_HUNGRY
. If it is NOT_HUNGRY
, it does not eat and its state does not change.
The Fish
can not be less than NOT_HUNGRY
or more than VERY_HUNGRY
.
The constructor takes no parameters but must initialize the state to NOT_HUNGRY
.
Provide methods:
public void swim()
the Fish becomes more hungry if not already VERY_HUNGRY
public void seeFood()
the Fish will eat if it is hungrypublic int getState()
Gets the integer representing the state, an integer 1 through 4. This method should also work in the final when the other methods are implementedpublic String getHungerLevel()
Gets a string describing the current hunger state of the Fish
: "Not hungry", "Somewhat hungry ,"Hungry", or "Very hungry"This problem is based on the class described in your text on page 389.
For the draft, provide the static constants, implement the constructor, and the getState()
method.