COW Q2 N6

advertisement
COW Q2 N6
Randoms & Wrapper Classes
Level 1
Fill in the code for the RandomNumbers Class. Each method in the RandomNumbers Class should
print out a series of 30 integers randomly picked numbers with the given range (inclusive):
random1()
random2()
random3()
random4()
random5()
random6()
-
a
a
a
a
a
a
range
range
range
range
range
range
of
of
of
of
of
of
0 to 10
0 to 100
10 to 110
12 to 578
-142 to -13
-300 to 412
Level 2
Fill in the code for the RandomStuff Class. Each method in the RandomStuff Class should return a
random item from the indicated set:
Hint – set up a static array of the given items, pick a random index, and use the random index to
pick a random number
randomLowerCaseLetter():
randomUpperCaseLetter():
randomSpecialCharacter():
randomNumberCharacter():
randomLetter():
randomName():
randomString():
lowercase letter
uppercase letter
special character
one digit number (char)
lowercase, uppercase, special, number
names of AP Computer Science students
returns a random String of length 3
Level 3
Create a LotteryTicket Class with the following attributes:
LotteryTicket
String nameOfOwner
ArrayList<Integer> digits
LotteryTicket(String name) – a constructor that takes in the name of the player who purchased the
ticket. It should then create an ArrayList of 5 randomly chosen Integers from 0 to 99 inclusive.
LotteryTicket(String name, int numDigits) – a constructor that takes in the name of the player who
purchased the ticket. It should then create an ArrayList of chosen Integers from 0 to 99 inclusive. The
number of items in the ArrayList should be equal to numDigits.
LotteryTicket(String name, int numDigits, int lowValue, int highValue) – a constructor that takes in
the name of the player who purchased the ticket. It should then create an ArrayList of chosen Integers
from lowValue to highValue inclusive. The number of items in the ArrayList should be equal to
numDigits.
toString() – returns a String that contains the name of the player and all the values stored in the ticket
get(int index) – returns the number at the given index
equal(LotteryTicket other) – returns whether two LotteryTickets are equal
Level 4
Create a Lottery Class with the following attributes:
Lottery
ArrayList<LotteryTicket> tickets
Lottery () – a constructor that creates but does not populate the ArrayList of Lottery Tickets.
add(String name, int numDigits, int lowValue, int highValue) – creates a Lottery Ticket with the
indicated parameters and adds it to the ArrayList
get(int index) – returns the Lottery Ticket at the given index
print() – prints a listing of all the lottery tickets
pickWinner() – picks a random lottery ticket and returns the name of the owner of that ticket
Fill in the code for the randomString(int n) of the RandomStuff Class. It should returns a
random String of length n.
Level 5
Add the following method for Lottery:
pickRandomNumbers(int numDigits, int lowValue, int highValue) – creates a Lottery ticket with the
given parameters and assigns “winner” to the name. It then prints out the winning Lottery Ticket.
Then it goes through and prints out the names of the owners of any tickets that match the winning
ticket.
Fill in the code for the RandomNumbers Class for random 7(). It should print out a series of 30
integers randomly picked numbers with the given range (inclusive) of:
-372,036,854,775,808 to 372,036,854,775,808
Download