1 Homework Set 2- Class Design 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.
P2A 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 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 and d) display the instance variables of the song2 using the object s tostring() method. 2
3 P2B P2B. Write a class Alarm that stores information about a clock alarm. It should have integer instance variables for the Alarms s hours, minutes, seconds. Include a zero-argument and three-argument constructor to initialize all instance variables (hour, min, sec). It should also have a tostring() method to display the time in the format shown below: Wake up time is 3:45:16 Write a separate AlarmDriver class to: a) create an instance of a Alarm clock called phonealarm using its zeroargument b) display the time of the phonealarm using its tostring() method, c) create an instance of a Alarm called a bedroomalarm and set the hours, minutes, and seconds using the Alarm s three-arguments constructor using a JOptionPane, and d) display the time of the bedroomalarm using its tostring() method. NOTE: 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: String brand = new String( );
P2C P2C. Let s write another class and test it with a driver. This time let s create a Student class that models a SFHS 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 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 and d) display the instance variables of the frosh001 using the object s tostring() method. Print out the results of the program using JOptionPane.showMessageDialog(). 4
P2D 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 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 and d) display the instance variables of the obj2 using the object s tostring() method. 5