AP Computer Science

Homework Set 2

Class Design

P2A.Write a class Song that stores information about a song. Class Song should include at least three instance variables that represent characteristics of the song. Include a zero-argument and three-argument constructor to initialize all instance variables. It should also have a toString() method to display the object’s instance variables in a user-friendly format.

Write a separate “SongDriver” class to:

a)create an instance of a Song called “song1” using its zero-argument constructor,

b)display the instance variables of the “song1” using the object’s toString() method,

c)create an instance of a Song called a “song2” and initialize its instance variables using the object’s multi-argument constructor, and

d)display the instance variables of the “song2” using the object’s toString() method.

P2B.Write a class Clock that stores information about a clock. It should have integer instance variables for the Clock’s hours, minutes, seconds. Include a zero-argument and three-argument constructor to initialize all instance variables. It should also have a toString() method to display the time in the format shown below:

The time is 3:45:16

Note that zero-argument constructors should initialize instance variables to known values, most commonly zero for integers, “” for Strings, or false for boolean values. See the example below:

public Clock()

{

int hour = 0;

double cost = 0.0;

String brand = new String(“”);

boolean isOn = false;

} // end constructor Clock

Write a separate “ClockDriver” class to:

a)create an instance of a Clock called “kitchenClock” using its zero-argument constructor,

b)display the time of the “kitchenClock” using itstoString() method,

c)create an instance of a Clock called a “bedroomClock” and set the hours, minutes, and seconds using the Clock’sthree-arguments constructor using a JOptionPane, and

d)display the time of the “bedroomClock” using itstoString() method.

P2C.Let’s write another class and test it with a driver. This time let’s create a Student class that models a Loyola student. The Student class should have instance variables for the student’s first and last name, and at least TWO other instance variables chosen by you. Include a zero-argument and multi-argument constructor to initialize all instance variables. Choose the most appropriate variable types (i.e. int, double, String, boolean) for each instance variable. It should also have a toString() method to display the object’s instance variables in a user-friendly format.

Write a separate “StudentDriver” class to:

a)create an instance of a Student called “senior001” using its zero-argument constructor,

b)display the instance variables of the “senior001” using the object’s toString() method,

c)create an instance of a Student called a “frosh001” and initialize its instance variables using the object’s multi-argument constructor, and

d)display the instance variables of the “frosh001” using the object’s toString() method.

If you are looking for an extra challenge, try to printout the results of the program using JOptionPane.showMessageDialog(). See the LewTube video for this HW Set on the details of how to use this very cool tool (no rhyme intended.)

P2D.Design a class that models an object of your choice (e.g. type of car, sports team, musical instrument, etc.) Include at least two different instance variables of two different types (e.g. String and an int, int and a double, boolean and a String, etc.) for your class. It should also have a toString() method to display the object’ instance variables in a user-friendly format.

Write a separate “YourObjectDriver” class to:

a)create an instance of a your object called “obj1” (or a name more appropriate for your object) using its zero-argument constructor,

b)display the instance variables of the “obj1” using the object’s toString() method,

c)create an instance of a your object called a “obj2” and initialize its instance variables using the object’s multi-argument constructor, and

d)display the instance variables of the “obj2” using the object’s toString() method.

P2E.Blackjack! Design a class called “Card” that models a playing card. Decide what instance variables you will need for your class in order to play Blackjack (or any other card game.) Include a zero-argument constructor and the appropriate multi-argument constructor for your class. It should also have a toString() method to display ALL of the object’s instance variables in a user-friendly format…this can be a bit tricky…

Write a CardDriver that will perform the following tasks:

a)Create three Card objects: a blank card (i.e. a Card created using its zero-argument constructor), a “number” card, and a “face” card…you pick the cards.

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

Below are the lines of code that demonstrate how to create an array of Card objects and how to place a Card object into an array called myHand.

Card[] myHand = new Card[10];

Card card1 = new Card( 10, “Jack”, “Hearts” );

myHand[0] = card1;

c)Use a for loop to traverse the array of your Cards and print each Card using System.out.println().

The following programs DO NOT need a separate class and driver files. They are stand-alone programs that are meant to review previous topics (and introduce a couple of new non-class concepts)

P2F.Write a program that creates an array that can hold 9 double values that represent baseball batting averages for a starting baseball lineup. Use a for loop to populate array with double values in the range of 0.00 to 0.500. Recall that “double” values are what Java calls “real” numbers. Use a second for loop to print the values in the array with one number per line. Finally, use a third for loop to traverse the array and find and print the maximum batting average in the array. See the LewTube for this HW assignment to see how to control the precision of a double number when you print it.

P2G.College application time! Write a program that creates an array of Strings. Fill the array with the name of the universities that you will be applying to. Then perform the following tasks:

a.traverse the array using afor loop and printout the length of each university’s name.

You can find the length of a String as shown below:

String name = new String( “UCLA” );

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

b.print the name of the university that has the shortest length.

c.You’ve decided not to apply to one of the schools on your list. Remove that school from the list by setting that element in the array to “null”. This can be done as shown below:

school[3] = new String( “NYU” );

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

d.print your list of schools again using a second for loop.

P2H.Let’s upgrade the “Login” program from HW Set 1 so that it will allow the user to input a username/password combination for a total of three attempts. If incorrect on the fourth attempt, the program should print out the following message:

“You have exceeded your three attempts.

Please try again next period.”

Hint: Use a loop along with a counter to count the number of unsuccessful attempts. If a successful password has been entered, the program should end (i.e. the program should not ask the user for another password or remain in an endless loop).

P2I.Design a class that maintains information about a user profile for a phone or tablet. Decide what instance variables you want to include in the profile. Create a driver that creates at least two user profiles and prints out the details of each user’s profile.

By the end of the lesson students should be able to:

a.Write the Java code define a class, its data members, and its constructors.

b.Write a toString() method for a class.

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

d.Write the Java code for a “driver” class that creates instance of an object and initializes the class’ instance variables and calls the class’ toString() method.

Page 1

AP Computer Science – HW Set 2 Programs