08.04.2015 Views

HW Set 2 - thecubscientist.com

HW Set 2 - thecubscientist.com

HW Set 2 - thecubscientist.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

AP Computer Science<br />

Homework <strong>Set</strong> 2<br />

Class Design<br />

P2A. Write a class Song that stores information about a song. Class Song should<br />

include at least three instance variables that represent characteristics of the song.<br />

Include a zero-argument and three-argument constructor to initialize all instance<br />

variables. It should also have a toString() method to display the object’s instance<br />

variables in a user-friendly format.<br />

Write a separate “SongDriver” class to:<br />

a) create an instance of a Song called “song1” using its zero-argument<br />

constructor,<br />

b) display the instance variables of the “song1” using the object’s<br />

toString() method,<br />

c) create an instance of a Song called a “song2” and initialize its instance<br />

variables using the object’s multi-argument constructor, and<br />

d) display the instance variables of the “song2” using the object’s<br />

toString() method.<br />

Page 1<br />

AP Computer Science – <strong>HW</strong> <strong>Set</strong> 2 Programs<br />

http://<strong>thecubscientist</strong>.<strong>com</strong>


P2B. Write a class Clock that stores information about a clock. It should have<br />

integer instance variables for the Clock’s hours, minutes, seconds. Include a zeroargument<br />

and three-argument constructor to initialize all instance variables. It should<br />

also have a toString() method to display the time in the format shown below:<br />

The time is 3:45:16<br />

Note that zero-argument constructors should initialize instance variables to known<br />

values, most <strong>com</strong>monly zero for integers, “” for Strings, or false for boolean values.<br />

See the example below:<br />

public Clock()<br />

{<br />

int hour = 0;<br />

double cost = 0.0;<br />

String brand = new String(“”);<br />

boolean isOn = false;<br />

} // end constructor Clock<br />

Write a separate “ClockDriver” class to:<br />

a) create an instance of a Clock called “kitchenClock” using its zeroargument<br />

constructor,<br />

b) display the time of the “kitchenClock” using its toString() method,<br />

c) create an instance of a Clock called a “bedroomClock” and set the hours,<br />

minutes, and seconds using the Clock’s three-arguments constructor using a<br />

JOptionPane, and<br />

d) display the time of the “bedroomClock” using its toString() method.<br />

Page 2<br />

AP Computer Science – <strong>HW</strong> <strong>Set</strong> 2 Programs<br />

http://<strong>thecubscientist</strong>.<strong>com</strong>


P2C. Let’s write another class and test it with a driver. This time let’s create a<br />

Student class that models a Loyola student. The Student class should have<br />

instance variables for the student’s first and last name, and at least TWO other<br />

instance variables chosen by you. Include a zero-argument and multi-argument<br />

constructor to initialize all instance variables. Choose the most appropriate variable<br />

types (i.e. int, double, String, boolean) for each instance variable. It should also have<br />

a toString() method to display the object’s instance variables in a user-friendly<br />

format.<br />

Write a separate “StudentDriver” class to:<br />

a) create an instance of a Student called “senior001” using its zeroargument<br />

constructor,<br />

b) display the instance variables of the “senior001” using the object’s<br />

toString() method,<br />

c) create an instance of a Student called a “frosh001” and initialize its instance<br />

variables using the object’s multi-argument constructor, and<br />

d) display the instance variables of the “frosh001” using the object’s<br />

toString() method.<br />

If you are looking for an extra challenge, try to printout the results of the program<br />

using JOptionPane.showMessageDialog(). See the LewTube video for this<br />

<strong>HW</strong> <strong>Set</strong> on the details of how to use this very cool tool (no rhyme intended.)<br />

P2D. Design a class that models an object of your choice (e.g. type of car, sports<br />

team, musical instrument, etc.) Include at least two different instance variables of two<br />

different types (e.g. String and an int, int and a double, boolean and a String, etc.) for<br />

your class. It should also have a toString() method to display the object’ instance<br />

variables in a user-friendly format.<br />

Write a separate “YourObjectDriver” class to:<br />

a) create an instance of a your object called “obj1” (or a name more appropriate<br />

for your object) using its zero-argument constructor,<br />

b) display the instance variables of the “obj1” using the object’s toString()<br />

method,<br />

c) create an instance of a your object called a “obj2” and initialize its instance<br />

variables using the object’s multi-argument constructor, and<br />

d) display the instance variables of the “obj2” using the object’s toString()<br />

method.<br />

Page 3<br />

AP Computer Science – <strong>HW</strong> <strong>Set</strong> 2 Programs<br />

http://<strong>thecubscientist</strong>.<strong>com</strong>


P2E. Blackjack! Design a class called “Card” that models a playing card. Decide<br />

what instance variables you will need for your class in order to play Blackjack (or<br />

any other card game.) Include a zero-argument constructor and the appropriate multiargument<br />

constructor for your class. It should also have a toString() method to<br />

display ALL of the object’s instance variables in a user-friendly format…this can be<br />

a bit tricky…<br />

Write a CardDriver that will perform the following tasks:<br />

a) Create three Card objects: a blank card (i.e. a Card created using its zeroargument<br />

constructor), a “number” card, and a “face” card…you pick the<br />

cards.<br />

b) Create an array called “myHand” that can hold 3 Card objects.<br />

Below are the lines of code that demonstrate how to create an array of Card objects<br />

and how to place a Card object into an array called myHand.<br />

Card[] myHand = new Card[10];<br />

Card card1 = new Card( 10, “Jack”, “Hearts” );<br />

myHand[0] = card1;<br />

c) Use a for loop to traverse the array of your Cards and print each Card using<br />

System.out.println().<br />

The following programs DO NOT need a separate class and driver files. They<br />

are stand-alone programs that are meant to review previous topics (and<br />

introduce a couple of new non-class concepts)<br />

P2F. Write a program that creates an array that can hold 9 double values that<br />

represent baseball batting averages for a starting baseball lineup. Use a for loop to<br />

populate array with double values in the range of 0.00 to 0.500. Recall that “double”<br />

values are what Java calls “real” numbers. Use a second for loop to print the values in<br />

the array with one number per line. Finally, use a third for loop to traverse the array<br />

and find and print the maximum batting average in the array. See the LewTube for<br />

this <strong>HW</strong> assignment to see how to control the precision of a double number when you<br />

print it.<br />

Page 4<br />

AP Computer Science – <strong>HW</strong> <strong>Set</strong> 2 Programs<br />

http://<strong>thecubscientist</strong>.<strong>com</strong>


P2G. College application time! Write a program that creates an array of Strings. Fill<br />

the array with the name of the universities that you will be applying to. Then perform<br />

the following tasks:<br />

a. traverse the array using a for loop and printout the length of each university’s<br />

name.<br />

You can find the length of a String as shown below:<br />

String name = new String( “UCLA” );<br />

System.out.println( name.length() ); // prints “4”<br />

b. print the name of the university that has the shortest length.<br />

c. You’ve decided not to apply to one of the schools on your list. Remove that<br />

school from the list by setting that element in the array to “null”. This can be<br />

done as shown below:<br />

school[3] = new String( “NYU” );<br />

school[3] = null; // null “removes” NYU from array<br />

d. print your list of schools again using a second for loop.<br />

P2H. Let’s upgrade out “Login” program from <strong>HW</strong> <strong>Set</strong> 1 so that it will allows the<br />

user to input a username/password <strong>com</strong>bination for a total of three attempts. If<br />

incorrect on the fourth attempt, the program should print out the following message:<br />

“You have exceeded your three attempts.<br />

Please try again next period.”<br />

Hint: Use a loop along with a counter to count the number of unsuccessful attempts.<br />

By the end of the lesson students should be able to:<br />

a. Write the Java code define a class, its data members, and its constructors.<br />

b. Write a toString() method for a class.<br />

c. Write the Java code that uses the “new” operator to instantiate an object.<br />

d. Write the Java code for a “driver” class that creates instance of an object and<br />

initializes the class’ instance variables and calls the class’ toString()<br />

method.<br />

Page 5<br />

AP Computer Science – <strong>HW</strong> <strong>Set</strong> 2 Programs<br />

http://<strong>thecubscientist</strong>.<strong>com</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!