Logic3eCh13 Solution..

advertisement
Chapter 13 Answers to Exercises
1.
Identify three objects that might belong to each of the following classes:
a. Automobile
b. NovelAuthor
c. CollegeCourse
The students will have a variety of answers for these questions. Some examples
might be:
a. myRedChevroletCamaro, theBlackfordMustangWithTheDentThatBobDrives,
thePorschee911ThatDonaldTrumpOwns
b. Terry Brooks, Steven King, Ray Bradbury
c. English Composition, Calculus, Physics
2. Identify three different classes that might contain each of these objects:
a. Wolfgang Amadeus Mozart
b. My pet cat named Socks
c. Apartment 14 at 101 Main Street
The answers to this question will also vary. Some examples are:
a. musicians, composers, baroque music performer
b. animals, domestic pets, felines
d. building, housing, locations
3. Design a class named CustomerRecord that holds a customer number, name, and
address. Include methods to set the values for each data field and print the values for each
data field. Create the class diagram and write the pseudocode that defines the class.
class CustomerRecord
private num CustomerNumber
private char Name
private char Address
public setCustomerNumber (num Number)
CustomerNumber = Number
return
public setName (char newName)
Name=newName
return
public setAddress (char newAddress)
Address=newAddress
return
4. Design a class named House that holds the street address, price, number of bedrooms,
and number of baths in a House. Include methods to set the values for each data field. In
the set methods, do not allow the price, bedrooms, or baths to be negative. Include a
method that displays all the values for a House. Create the class diagram and write the
pseudocode that defines the class.
class House
private char Address
private num Price
private num Bedrooms
private num Baths
public SetAddress(char newAddress)
Address=newAddress
return
public SetPrice(num newPrice)
if newPrice>0 then
Price=newPrice
endif
return
public SetBedrooms(num newRooms)
if newRooms > 0 then
Bedrooms=newRooms
endif
return
public SetBathrooms(num newBaths)
if newBaths > 0 then
Baths=newBaths
endif
return
public print()
print Address, Price, Bedrooms, Baths
return
5. Design a class named Loan that holds an account number, name of account holder,
amount borrowed, term, and interest rate. Include methods to set values for each data
field. In the set methods, do not allow the amount borrowed to be negative or over
$100,000, the term to be over 30 years, or the interest rate to be over 17%. Also include a
method that prints all the loan information. Create the class diagram and write the
pseudocode that defines the class.
class Loan
private num AcctNumber
private char Name
private num Amount
private num Term
private num Rate
public setAccoutNumber(num newNumber)
AcctNumber = newNumber
return
public setName(char newName)
Name = newName
return
public setAmount(num newAmount)
if newAmount >= 0 and newAmount <= 100000 then
Amount = newAmount
endif
return
public setTerm(num newTerm)
if newTerm <= 360 then
Term=newTerm
endif
return
public setRate(num newRate)
if newRate>=0.0 and newRate <= 0.17 then
Rate = newRate
endif
return
public print()
print AcctNumber, Name, Term, Amount, Rate * 100
return
6. Complete the following tasks:
a. Design a class named Book that holds a stock number, author, title, price, and
number of pages for a book. Include methods to set and print the values for each data
field. Create the class diagram and write the pseudocode that defines the class.
class Book
private num StockNumber
private char Author
private char Title
private num Price
private num Pages
public SetStockNumber (num newNumber)
StockNumber = newNumber
return
public SetAuthor (char newAuthor)
Author = newAuthor
return
public SetTitle (char newTitle)
Title = newTitle
return
public SetPrice (num newPrice)
Price = newPrice
return
public SetPages (num newPages)
Pages = newPages
return
public print()
print StockNumber, Author, Title, Price, Pages
return
b. Design a class named TextBook that is a child class of Book. Include a new
data field for the grade level of the book. Override the Book class methods that set and
print the data so that you accommodate the new grade-level field. Create the class
diagram and write the pseudocode that defines the class.
class TextBook descends from Book
private num GradeLevel
public SetGradeLevel(num newLevel)
GradeLevel=newLevel
return
public print()
print StockNumber, Author, Title, Price, Pages, GradeLevel
return
7. Complete the following tasks:
a. Design a class named Player that holds a player number and name for a sports
team participant. Include methods to set the values for each data field and print the values
for each data field. Create the class diagram and write the pseudocode that defines the
class.
Class Player
private num Number
private char Name
public SetNumber(num newNumber)
Number=newNumber
return
public SetName(char newName)
Name=newName
return
public Print()
print Number, Name
return
b. Design two classes named BaseballPlayer and BasketballPlayer that are child
classes of Player. Include a new data field in each class for the player’s position. Include
an additional field in the BaseballPlayer class for batting average. Include a new field in
the BasketballPlayer class for free-throw percentage. Override the Player class methods
that set and print the data so that you accommodate the new fields. Create the class
diagram; then write the pseudocode that defines the class.
class BaseballPlayer descends from Player
private char Position
private num BatAverage
public SetPosition (char newPosition)
Position = newPosition
return
public SetBatAverage(num newAverage)
Average=newAverage
return
public Print()
print Number, Name, Position, BatAverage
return
class BasketballPlayer descends from Player
private char Position
priave num FreeThrowPct
public SetFreeThrowPct(num newFTPct)
FreeThrowPct=newFTPct
return
public SetPosition (char newPosition)
Position = newPosition
return
public Print()
print Number, Name, Position, FreeThrowPct
return
8. Complete the following tasks:
a. Design a class named PlayingCard. Its attributes include suit (clubs, diamonds,
hearts, or spades), value (a number 1 through 13), and valueName. If a PlayingCard’s
value is between 2 and 10 inclusive, the valueName is blank; however, if the value is 1,
the valueName is “Ace”, and if it is 11, 12, or 13, the value name is “Jack”, “Queen”, or
“King”, respectively. Create two overloaded constructors for the class. One takes no
arguments and uses a built-in method that returns a randomly generated number. This
method’s signature is num rand (num high) where high represents the highest value the
method might return—the method returns a random number between 0 and this high
value inclusive. Use the random number generating method to select both the suit and the
value for any PlayingCard that uses the default constructor. The second constructor
method assigns values to the PlayingCard attributes based on passed arguments. This
constructor verifies that the passed arguments are within range (that is, only one of the
four allowed suits and only one of the 13 allowed values); if the values are out of range,
force the PlayingCard to be the Ace of Spades. This nondefault constructor also sets the
valueName for cards valued at 11 through 13. Also create a showCard() method for the
class that displays a PlayingCard’s value.
class PlayingCard
private char suit
private num value
private char valueName
private suitArray[4]=(“clubs”, “diamonds”, “hearts”, “spades”)
public PlayingCard()
declare variables
newSuit=suitArray[rand(3) + 1]
newValue=rand(12) + 1
suit=newSuit
value=newValue
perform setValueName()
return
public PlayingCard (char newSuit, num newValue)
declare variables
if newValue>=1 and newValue <=13 then
value=newValue
else
value=1
endif
perform setValueName()
if newSuit = “clubs”
or newSuit = “hearts”
or newSuit = “spades”
or newSuit = “diamonds” then
suit = newSuit
else
suit = “spades”
endif
return
public showCard()
if valueName <> “” then
print valueName, “ of ”, suit
else
print value, “ of “, suit
endif
return
private setValueName()
if value = 1 then
valueName = “Ace”
else
if value >=2 and value <= 10 then
valueName = “”
else
case based on value
case 11
valueName = “Jack”
case 12
valueName = “Queen”
case 13
valueName = “King”
default
valueName = “Error, Not Possible”
endcase
endif
endif
return
b. Design the logic for a program that instantiates two PlayingCard objects. Allow
one object’s values to be randomly generated, but use user input values for the second
object.
start
PlayingCard card1
getInput()
PlayingCard card2(suit,value)
card1.showCard()
card2.showCard()
stop
getInput()
do
print “Pick a suit”
print “1. clubs”
print “2. diamonds”
print “3. hearts”
print “4. spades”
read choice
case based on choice
case 1
suit = “clubs”
case 2
suit = “diamonds”
case 3
suit = “hearts”
case 4
suit = “spades”
default
suit = “”
endcase
until suit <> “”
do
print “Enter a value”
print “1. Ace”
print “2 – 10 for number cards”
print “11. Jack”
print “12. Queen”
print “13. King”
read value
until value >= 1 and value <= 13
return
c. Add any additional class methods you need so that you can create a game in
which the user tries to guess the value of a randomly generated PlayingCard. Give the
user 1 point for guessing the suit correctly, 2 points for guessing the value correctly, and
10 points for guessing both values of the PlayingCard correctly.
public makeGuess()
do
print “Pick a suit”
print “1. clubs”
print “2. diamonds”
print “3. hearts”
print “4. spades”
read choice
case based on choice
case 1
testSuit = “clubs”
case 2
testSuit = “diamonds”
case 3
testSuit = “hearts”
case 4
testSuit = “spades”
default
testSuit = “”
endcase
until testSuit <> “”
do
print “Enter a value”
print “1. Ace”
print “2 – 10 for number cards”
print “11. Jack”
print “12. Queen”
print “13. King”
read testValue
until testValue >= 1 and testValue <= 13
return
public scoreGuess()
score = 0
if value = testValue then
score = score + 2
endif
if suit = testSuit then
score = score + 1
endif
if suit = testSuit and value = testValue then
score = score + 1
endif
return
Download