Using Arrays
Arrays with first empty index
UsingArrays.java
HasNums.java
-- class with array instance var and first empty index
HasNumsHarness.java
Classwork
Part A
☑
Create a project, and give it a class with a main.
☑
Add a class MadChooser which has an instance variable subjects which is an
array of Strings. We will be
filling this up gradually with Strings, so add another
instance var to keep track of the first empty position in the array.
We'll leave out accessors and mutators for both of these and not make them available
at all outside the class.
☑
In the default constructor for MadChooser, set the array up to
be length 20.
☑
Add a method addSubject which takes a String and adds it to the array
subjects in the first space that is empty (if there is any space left). Remember to update the
variable for keeping track of the first empty space.
☑
Add a method chooseSubject which chooses and returns a random String from those
already added to the array subjects
To choose a random
element of the array we can just choose a random index in the array, and
then use the element at that index. Usually that would mean a
number from zero up to but not including the length, but we need it to only include ones
that have been added (so up to but not including what number?) If no words have been added
to subjects yet, always return just the String "subject"
instead.
☑
Add a toString which returns something likeSubjects (3 words):
dog
cat
diving helmet
showing the number of words that have been added, and a list of those
words, but not the unfilled spaces later in the array.
A toString for a class with an array almost always starts by creating a
string, adding to it as you loop through the array, and returning a
final result at the end.
☑
In a separate class in a main, create a MadChooser, print a randomly chosen subject and the MadChooser itself,
then add at least three subjects, print the chooser and a random subject
again. (hint: run several times to be sure randomization works)
Part B
☑ To MadChooser, add another array of Strings, verbs.
you will also need to keep track of the first empty
spot in this array (we may not always add the same number of verbs and subjects,
so we can't use the same variable for both arrays). Don't forget to set
it up in the constructor!
☑
Add two more methods, addVerb and chooseVerb, for adding Strings and randomly choosing
verbs. If no verbs have been added yet, chooseVerb should just return "verbs".
☑
Update toString to return something likeSubjects (3 words):
dog
cat
diving helmet
Verbs (2 words):
eats
invents
☑
Also add a constructor that takes two ints sizeSubject and sizeVerb, and sets up the arrays to be those sizes
instead of size 20.
☑
Add a method chooseSentence which returns a sentence of the form "The
S V." Where S is a String returned by chooseSubject() and V is a
String returned by chooseVerb(). So if all arrays had nothing added to them, we
would get "The subject verbs." but if words were added
we might get "The dog eats" or "The diving helmet
invents" ☑ In your main, add at least 3
verbs to your MadChooser before printing it. Then, in a loop, generate 10 random sentences with
chooseSentence.
[EC]
☑
In MadChooser add a method addSubjects which takes an array of Strings
newSubs as parameter and adds each of the Strings from newSubs to subjects until
subjects runs out of room (don't forget that subjects might already have some
words in it, and that you can't assume newSubs will be the same length as
subjects. (hint: do you already have a method that adds a word for you,
including checking for running out of room?).
☑
In your main, before printing the MadChooser or generating the 10 random
sentences, create an array of 5 subject Strings, then use addSubjects to
give these to your Madchooser.
[EC] Add two more arrays
Add an array to Madchooser for objects, and
methods to be able to add individual Strings and choose a random
object.
Include the new array in the toString and change chooseSentence to
return something of the form "The S V the O" where O is a String
returned by chooseObject, so we get sentences like "The dog eats the
velociraptor". Make sure to add some objects in your
main.
Add another array to Madchooser for
adjectives, and arrange to be able to add individual Strings to this new array and choose a random adjective.
Include the new array in the toString and change chooseSentence to
return something of the form "The S V the A O" where A is a String
returned by chooseAdjective so we get sentences like "The dog eats the
enormous velociraptor". Make sure to add some adjectives in
your main. (You may choose to do adverbs instead.)
(If you do these, you should be able to get some hilarious sentences;
feel free to post them to the student discussion board)
[EC] add a constructor to MadChooser which takes
arrays of Strings, newSubs and newVerbs. Set up
subjects to be twice the length of newSubs, put the contents of newSubs
into subjects, and set the correct first empty
spot in subjects. Do the same for verbs. (hint: a lot of this can
be done with things you already wrote, it is always okay to add extra
methods if it helps)