Chapter 4/Debugging 4-1.png
__MACOSX/Chapter 4/._Debugging 4-1.png
Chapter 4/Debugging 4-3b.png
__MACOSX/Chapter 4/._Debugging 4-3b.png
Chapter 4/Chapter 4 Excercises.pdf
Print Previerv
I of 8
https:
ng.cengage.com/static/n
eade
ui/apps/n
cade
pri nt_p..
Chapter 4: More Obiect Concepts: 4-1Od Exercises
Book Tifle: Java* Programming
Printed By : WeMy Pefiick (werdy. petrick@ email. saintleo.edu)
@ 2019 Cengage, Cengage Leaming, lnc.
Chapter Review
+10d Exercises
Programming Exercises
1. Create a dass named FormletterWri ter that indudes two overloaded
methods named d i spl ay Sa1 utat i on ( ) . The first method takes one S t r i ng
parameter that represents a customer's last name, and it displays the
salu&ation Dear Mr. or Ms. followed bythe last name. The second method
acoepts two Stri ng parameters that represent a first and last name, and it
displays the greeting Dearfollowed bythe first name, a spa@, and fte last
name. After each salutation, display the rest of a short business letter: Thank
you for your reent order. Write a mai n ( ) method that tests each overloaded
meftod. Save the file as Forml-etterWriter.java.
2- Creale a class named Bi lli ng that indudes three overloaded
computeBi 11O methods for a photo book store.
. When computeBi XXXXXXXXXXreceives a single parameter, it represents the
price of one photo book ordered. Add 8% tax, and return the total due-
. When computeBi 11O receives two parameters, they represent the
price of a photo book and the quantity ordered. Multiply the two values,
a&$o/" tan, and retum the total due.
. When computeBi 11O receives three parameters, they represent the
price of a photo book, the quantity ordered, and a coupon value.
Multiply the quantity and price, reduce the result by the coupon value,
and then add 8% tax and retum the total due.
Write a ma i n ( ) method that tests all three overloaded methods- Save the
application as Billing.iava.
4l3O/19,7:31 PM
Print Preview
3.
https:
ng cengage.comlstatic/n
eade
ui/apps/n
eade
print_p...
a. Create a Fi tnessTracker dass that indudes data fields for a frhess
activity, the number of minutes spent participating, and the date. The
dass includes meffiods to get each feld. ln additbn, create a default
constructor that automatically sets the activity lo running, the minutes to
0, and the date to January 1 of the cunent year. Save the file as
Fthess'Tracker,java. Create an application that demonstrates each
method works conectly, and save it as TestFitnessTrac-ker.iava.
. Create an additionaloverloaded consffiuctorforthe Fi tnessTracke
dass you created in Exercise 3a- This consffuctor receives paraneters
for each of the data fields and assigns them appropriately. Add any
needed staternents to fte TestFi tnessTracker application to ensure
that the overloaded constructor works conectly, save it, and then test it.
c. Modify the Fi tnessTracker class so that the default constructor calls
the three-parameter constuctor. Save the dass as
Fihess'Trackerllava. Create an application to test the new version of
the class, and name it TestFrtnesSTracker2.iava.
a. Create a dass named BloodData that indudes fields that hold a blood
t)rpe (tre four blood types are O, A, B, aN AB) aN an Rh factor (the
factors are + and -). Create a default @nstructor that sets the fields to
Oafi+, and an overloaded @nsfiuctorthat requirm values for both
fields. lnclude gpt and set methods for eacfi field. Save this file as
BloodDah java. Create an application named TestBloodData that
dernonsfrates each metpd works conec{y. Save the application as
T6tBlood[lata.java.
. Greate a class named Patient that includes an lD number, age, and
BloodData. Provide a default oonstructor that sets the lD number to 0,
the age to 0, and the BloodData values to O and +. Create an
overloaded onsfructorthat provides values for each field. Also provide
get methods for each field. Save the file as PaUentjava. Create an
application that demonstates that each method works onecty, and
save it as TestPatienliava-
a. Create a class to hold data about a high school sports team. The Team
class holds data fields for high school name (such as Roosevelt High),
sport (such as Girls'BaskebalD, and team name (such as Dolphinsl-
lnclude a constructor that takes parameters for each field, and include
4.
2 of8
5.
4l3ol19,'7:3lYM
Print Prer ies
3 of8
6.
https:
ng -cengage.comlstalic/n
eade
ui/apps/n
eade
pri nt_p...
get methods that return the values of the fields. Also include a publ i c
f i naI stati c Stri ng named H0TT0 and initialize it to
Sportsmanship!. Write an application named TestTeam to instantiate
three Team objects with different values, and then display all the data,
including the motto, for each object. Save both the Team.java and
TestTeam.java files.
. Create a class named Game. lrxCude two Team fields that hold data
about the teams participating in the game. Also include a field for game
time (for example, 7 PM.lrdude a onstructor that takes parameters
for two Team objects and a time. Write an application named TestGame
to instantiate a Game obiect, then pass the Game to a method that
displays the details about the Game.
a. Create a class named Ci rcle with fields named radi us, di ameter,
and area. lnclude a mnstructor that sets the radius to 1 and calculates
the other two values. Also include methods named setRadi us ( ) and
getRadi us ( ). The setRadi us ( ) method not only sets the radius, but
it also calculates the other two values- (The diameter of a circle is twice
the radius, and the area of a circle is pi multiplied by the square of the
adius. Use the ltlath class PI constant for this calculation.) Save the
class as Circle.java.
. Create a class named TestCi rcle whose mai n O rnethod declares
severai Ci rcle objects. Using the setRadi us ( ) method, assign one
Ci rcle a smali radius value, and assign another a larger radius value.
Do not assign a value to the radius of the third circle; instead, retain the
value assigned at construction. Display all the values for all the C i r c i e
objects. Save the application as TestCircle.java.
7. Write a Java application that uses the Hath class to determine the answers
for each of the following:
a. The square root of 37
. The sine and cosine of 300
c. The value of the floor, ceiling, and round of 22-8
d. The larger and the smaller of the character'D'and the integer 71
1l30ll9-7:31 PM
I)rint Prer ieu
-tof8
https:
ng.cengage.com/static/n
eade
ui/appvn
eade
pri nt p..-
e. A random number between 0 and 20 (Hint The randomO method
eturns a value between 0 and 1;you want a number that is 20 times
larger.)
Save the application as MathTest.iava.
8. Write a program that declares two LocalDate obiects and assign values that
epresent January 31 and December 31 in the cu
ent year. Display output
that demonstrates the dates displayed when one, two, and three rnonths are
added to each of the obiects. Save the applicatbn as
Testilonth Hand I i ng. java.
9. Write an application that oomputes and displays the day on whicfr you
ecome (or became) 10,000 days old. Save ttre application as
TenThousand DaysOld.iava.
10. The LocalDate class includes an instance method named
length0f t{onth ( ) that retums the number of days in the rnonth. Write an
application that uses methods in the LocalDate class to calculate how many
dalts are left until the first day of next month. Display the result, induding the
name of the next month. Save the file as DaysTilNextlrlonth.iava.
11. Create a class named Person that holds the following fields: two Str i ng
obiects for the person's first and last name and a LocalDate obiect for the
person's birthdate. Create a class named Couple that contains two Person
objects. Create a class named Weddi ng for a wedding planner that includes
the date of the wedding, the names of the Couple being manied, ard a
Stri ng for the location. Provide constructors for each class that accept
parameters for each field, and provide get methods for eacfi field. Then write
a program that creates two Weddi ng objecb and in tum passes each to a
method that displap allthe details. Save the ftles as Person.iava,
Gouple.java, Wedding .iava, and TestWedding.iava.
Debugging Exercises
1. Each of the following files in the Chapte(X folder of your downloadable
student files has syntax and/or logic enors. ln each case, determine the
1l30l19.7:31PM
Print Pretieu.
5 of8
https:
ng.cen gage.com/static/n
eade
ui/apps/n
eade
pri nt_p.
problem and fix the program. After ),ou corect the enors, save eaclr file using
the same filename preceded with frx. For example, save DebugFourl.iava as
FixllebugFourl.iava.
a. DebugFourl.java
. DebugFour2.iava
c. DebugFour3.java and DebugBox.java
d. DebugFour4.iava
When you change a filename, remember to change every instance
of the class name within the file so that it matches the new filename.
ln Java, the filename and class name must always match.
"$'-4f{t€
Game Zone
1. Dice are used in many games. One die can be thrown to randomly shovr a
value from 1 through 6. Design a Di e class that can hold an integer data field
for a value (frorn 1 to 6). lnclude a constructor that randomly assigns a value
to a die object. contains information about generating random
numbers. To fully understand the process, you must learn more about Java
classes and methods. However, for now, you can copy the following statement
to generate a random number between 1 and 6 and assign it to a variable.
Uslng this statement assumes you have assigned appropriate values to the
static constants.
arndomValue 5 ((;nt,t(n"ath.r;rndonr() '' 100) '', ttICltEST DIE VALUE +
L0},EST i)IF. VALtJFi:
Also include a method in the class to retum a die's value. Save the class as
Die.java. Write an application that randomly throws" two dice and displays
their values. After you read the chapter "Making Decisions," you will be able to
have the game determine the higher die. For now, just observe how the
values change as you execute the program multiple times. Save the
application as TwoDice.java.
$3A119.7:31 PM
Print Prer ieu
6 of8
https:1/ng.cengage.com/static/n
eade
ui/apps/n
eaderlpri nt_p...
2. Using the Di e class, write an application that randomly throws" five dice fo
the computer and five dice for the player. Display the values and then, by
observing the results, decide who wins based on the following hierarchy of
Di e values. (The computer will not decide the winner; the