Comprehensive Introduction To Object Oriented
Programming With Java
By Thomas Wu,
1st Edition
, Chapter 1 Solutions
1.1 Graphically Represent A Vehicle Class And Three Vehicle Objects Named Car1,
Car2, And Car3.
Vehicle
car1:Vehicle car2:Vehicle car3:Vehicle
1.2 Graphically Represent A Person Class With The Following Components:
• Instance Variables Name, Age, And Gender.
• Instance Methods Setname, Getname, And Getage.
• Class Method Getaverageage.
Person
Setname()
Name
Getage()
Age Age
Getgender() Gender
Gender
Getaverageage()
Average Age
1.3 Design A CD Class Where A CD Object Represents A Single Music CD. What
Kinds Of Information (Artist, Genre, Total Playing Time, Etc.) Do You Want To
Know About A CD? Among The Information In Which You Are Interested,
Which Are Instance Variables? Are There Any Class Variables Or Class
Constants?
State
Album Name Instance Variable
Artist Instance Variable
Genre Instance Variable
Total Playing Time Instance Variable
Maximum Playing Time Class Constant
, Behaviors
Getalbumname(), Setalbumname() Instance Methods
Getartist(), Setartist() Instance
Methods
Getgenre(), Setgenre() Instance
Methods Getplayingtime(), Setplayingtime() Instance
Methods Getmaxplayingtime() Class Method
Students May Also Include A List Of Songs On The CD And Methods To Access
Them. A Song Itself Would Probably Have Its Own Class To Represent It.
However This Concept Is More Advanced Than Is Necessary At This Stage.
1.4 Suppose The Vehicle Class In Exercise 1 Is Used In A Program That Keeps
Track Of Vehicle Registration For The Department Of Motor Vehicles. What
Kinds Of Instance Variables Would You Define For Such Vehicle Objects?
Can You Think Of Any Useful Class Variables For The Vehicle Class?
Instance Variables
Owner
Licenseid
Registrationnumber
Make
Mode
l
Color
Value
Class Variables
At This Stage, The Number Of Total Vehicles Could Be Thought To
Belong To The Class. Information Relating To The Length Of A
Licenseid Or Registrationnumber Could Be Stored As Class Constants.
Aside From These, There Are No Obviously Necessary Traits For Which
All Vehicles Share The Same Value Or Which The Class May Need To
Function.
1.5 Suppose The Following Formulas Are Used To Compute The Annual
Vehicle Registration Fee For The Vehicle Registration Program In
Exercise 1.4:
• For Cars, The Annual Fee Is 2 Percent Of The Value Of The Car.
• For Trucks, The Annual Fee Is 5 Percent Of The Loading
Capacity (In Pounds) Of The Truck.
Define Two New Classes Car And Truck As Subclasses Of Vehicle.
Hint: Associate Class And Instance Variables Common To Both Car And Truck
To Vehicle.
Vehicle Class
See Exercise 1.4
Car Class (Subclass Of Vehicle)
Registrationrate Class Constant
, Note: Value Is Already An Instance Variable In Vehicle Since All
Vehicles Have Some Value.
Truck Class (Subclass Of Vehicle)
Registrationrate Class Constant
Loadingcapacity Instance Variable
1.6 Consider A Student Registration Program Used By The Registrar’s Office. The
Program Keeps Track Of Students Who Are Registered For A Given Semester.
For Each Student Registered, The Program Maintains The Student’s Name,
Address, And Phone Number; The Number Of Classes In Which The Student Is
Enrolled And The Student’s Total Credit Hours. The Program Also Keeps Track
Of The Total Number Of Registered Students. Define Instance And Class
Variables Of A Student Class That Is Suitable For This Program.
Instance Variables
Name
Address
Phonenumber
Numclassesthissemester
Totalcredithours
Class Variables
Numstudentsregistered
1.7 Suppose The Minimum And Maximum Number Of Courses For Which A
Student Can Register Are Different Depending On Whether The Student Is A
Graduate, Undergraduate, Or Work-Study Student. Redo Exercise 6 By
Defining Classes For Different Types Of Students. Relate The Classes Using
Inheritance.
Student
See Exercise 1.6
Graduatestudent (Subclass Of Student)
Maximumhours Class Constant
Minimumhours Class Constant
Undergraduatestudent (Subclass Of Student)
Maximumhours Class
Constant
Minimumhours Class Constant
Workstudystudent (Subclass Of Student)
Maximumhours Class Constant
Minimumhours Class Constant