Java Reference
In-Depth Information
7. If you get an error stating that a package or class is not found, either
run the command from the directory that contains \lottery or add
the directory that contains \lottery to your CLASSPATH.
8. Open the index.html file that was created, and check out your
comments!
Lab 9.3: Using the Vector Class
This lab demonstrates using the Vector class. You will write a class called
GuessingGame that randomly tries to “guess” a number you pick
between 1 and 100. Each guess is saved in a Vector.
1. Write a new class named GuessingGame. Add a field of type int
named target and a field of type Vector named guesses.
2. Add a constructor that takes in an int, which is stored in the target
field. Within the constructor, initialize guesses to a new Vector with
an initial capacity of 100 and a capacity increment of 25.
3. Add a public void method named startGuessing(). This method
should contain a while loop that randomly generates an int between
1 and 100. If the random number matches the target, then break out
of the for loop. If it doesn't, add the random number to the guesses
Vector and keep repeating the loop.
4. Add a public void method named printGuesses() that uses a for
loop to print out all of the elements in the guesses Vector. After the
for loop, print out the size of the Vector.
5. Add main() to the GuessingGame class. Within main(), parse the
first command-line argument to an int to represent the target. Use
the method Integer.parseInt().
6. Within main(), instantiate a new GuessingGame object, passing in
the int from the command line. Invoke startGuessing(), and then
printGuesses().
7. Save and compile the GuessingGame class.
8. Run the GuessingGame program, making sure that you pass in an
int on the command line. The startGuessing() method will randomly
generate ints between 1 and 100 until your number is guessed. You
should see the output of the Vector after your number is guessed.
Search WWH ::




Custom Search