AP Computer Science Homework Set 2 Class Design P2A. Write a class Song that stores information about a song. Class Song should include: a) At least three instance variables that represent characteristics of a song. b) A zero-argument and three-argument constructor to initialize all instance variables c) A tostring() method to display the object s instance variables in a userfriendly 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. Page 1
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 zeroargument 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 zeroargument constructor, b) display the time of the kitchenclock using its tostring() method, c) create an instance of a Clock called a bedroomclock and set the hours, minutes, and seconds using the Clock s three-arguments constructor d) display the time of the bedroomclock using its tostring() method. Page 2
P2C. Let s write another class and test it with a driver. This time let s create a Student class that models a Fairfax 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 zeroargument 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. 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 s 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. Page 3
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 multiargument 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 zeroargument 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 new 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. Page 4
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 a for 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. Page 5
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 6