Exercises 6.4
1.
/**
lower by a (double) amount in degrees of my scale.
*
Receive:
double amount
*
Precondition: myDegrees - amount produces a valid magnitude
*
for a Temperature whose scale is myScale.
*
Return: a Temperature that is amount
degrees lower than myself.
*/
public Temperature lower(double amount)
{
double newDegrees = myDegrees -
amount;
if
(!isValidTemperature(newDegrees, myScale))
ann.util.Controller.fatal("raise(double)", newDegrees + "
" + myScale
+ " is not a valid temperature");
return new
Temperature(newDegrees, myScale);
}
2.
/**
Celsius converter
*
Return: the Celsius equivalent to myself
*/
public Temperature inCelsius()
{
Temperature result = null;
if (myScale == 'C')
result = new
Temperature(myDegrees, 'C');
else if (myScale == 'F')
result = new
Temperature(myDegrees * 1.8 + 32.0, 'F');
else if (myScale == 'K')
result = new
Temperature(myDegrees - 273.15, 'C');
return result;
}
3.
/**
Kelvin converter
*
Return: the Kelvin equivalent to myself
*/
public Temperature inKelvin()
{
Temperature result = null;
if (myScale == 'K')
result = new
Temperature(myDegrees, 'K');
else if (myScale == 'F')
result = new
Temperature((myDegrees - 32)/1.8 + 273.15, 'K');
else if (myScale == 'C')
result = new
Temperature(myDegrees + 273.15, 'K');
return result;
}
4. int
myMonth,
int myDay,
int myYear;
5. int myHours,
myMinutes,
mySeconds;
6. int myAreaCode,
myLocalExchange,
myNumber;
7. char myValue; // '2' Ð '9', 'T' (ten), 'J',
'Q', 'K', 'A'
char mySuit; //
'C', 'S', 'D', or 'H'
8. double myX,
myY;
9. double myR,
myTheta;
10 Ð 18. There are obviously many different ways that these classes can be implemented. They are all straightforward but require quite a lot of code. Because of space limitations, only one implementation is given here Ñ for Problem 13, an implementation of the class PlayingCard in Exercise 7. The others are similar Ñ choose appropriate types for the members of each class and supply member functions to set and retrieve those members, in addition to member functions for I/O, etc.
/**
PlayingCard.java
*
Class Invariant:
* mySuit is one of 'H', 'S', 'D', or 'C'
* myValue is one of '2', '3', '4', '5', '6', '7',
'8', '9',
*
'T' (ten) , 'J', 'Q', 'K', or 'A'
*/
import
ann.easyio.*;
class
PlayingCard
{
//--- Constructors ---
/** Default-value constructor.
*
Postcondition: myValue == 'A' && mySuit == 'C'.
*/
public PlayingCard()
{
myValue = 'A';
mySuit = 'C';
}
/** Explicit-value constructor.
*
Receive: char value, char suit
*
Precondition: suit is one
of 'H', 'S', 'D', or 'C'
*
and Value is one of '2', '3', '4', '5', '6', '7',
'8', '9', 'T', 'J', 'Q', 'K' or 'A'
*
Postcondition: myValue == (uppercase) value
* && mySuit == (uppercase) suit.
*/
public PlayingCard(char value, char
suit)
{
if (isValidPlayingCard(value,
suit))
{
myValue =
Character.toUpperCase(value);
mySuit =
Character.toUpperCase(suit);
}
else
fatal("PlayingCard(value, suit)", "invalid args: " +
value + suit);
}
//--- Accessors ---
/** value accessor.
*
Return: myValue
*/
public char getValue()
{
return myValue;
}
/** suit accessor.
*
Return: mySuit
*/
public char getSuit()
{
return mySuit;
}
//--- Input/Output ---
/** read(Keyboard) using the ann
Keyboard class.
*
Receive: theKeyboard, a Keyboard object
*
Input: value, a char, and
suit, a char, from theKeyboard
*
Return: true if (value, suit)
comprises a valid playing card,
*
false otherwise
*
Postcondition: myValue == value && mySuit == suit if a valid
*
playing card has been input; otherwise the data members
*
are unchanged.
*/
public boolean read(Keyboard
theKeyboard)
{
char inValue =
theKeyboard.readChar();
char inSuit = theKeyboard.readChar();
if
(isValidPlayingCard(inValue, inSuit))
{
myValue =
Character.toUpperCase(inValue);
mySuit =
Character.toUpperCase(inSuit);
return true;
}
else
return false;
}
/** toString converter (used by +,
print(), println(), ...).
*
Return: a String representation of myself
*/
public String toString()
{
return myValue + " " +
mySuit;
}
/** copy instance method, to support
assignments, initializations.
*
Return: A distinct copy of myself
*/
public PlayingCard copy()
{
return new PlayingCard(myValue,
mySuit);
}
/** PlayingCard validation utility.
* Receive: value, a char; suit, a char
* Return: true if (value, suit) represents a valid playing card,
* false
otherwise.
*/
public static boolean
isValidPlayingCard(char value, char suit)
{
if (suit == 'C' || suit == 'c' ||
suit == 'H' || suit == 'h'
||
suit == 'D' || suit == 'd' || suit == 'S' || suit == 's')
return (value == '2'
|| value == '3' || value == '4' || value == '5'
|| value == '6' || value == '7' ||
value == '8' || value == '9'
|| value == 'T' || value == 't'
|| value == 'J' || value == 'j' ||
value == 'Q' || value == 'q'
||
value == 'K' || value == 'k' || value == 'A' || value == 'a');
else
return false;
}
/** Fatal error utility.
*
Receive: methodName, the name of the
method where the error
*
occurred, and diagnostic, a message explaining the problem
*
Output: an error message
containing methodName and diagnostic
*
Postcondition: Execution has been terminated.
*/
private static void fatal(String
methodName, String diagnostic)
{
System.err.println("\n***
" + methodName + ": " + diagnostic);
System.err.flush();
System.exit(1);
}
//---
Attribute variables
private char myValue; // '2' -
'T', 'J', 'Q', 'K', 'A'
private char mySuit; //
'C', 'S', 'D', or 'H'
}
19.
class Student
{
int myStudentNumber;
string myLastName;
string myFirstName;
char myMiddleInitial;
string myCity;
string myState
string myPhoneNumber;
char myGender;
short int myYear;
int myCreditsToDate;
double myGPA;
string myMajor;
...
}
20.
class Inventory
{
int myItemNumber;
int myInStock;
double myUnitPrice;
int myMinumumInventory;
string myItemName;
...
}
21.
class UserId
{
int myIDNumber;
string myLastName;
string myFirstName;
string myPassword;
int myResourceLimit;
double myResourcesUsed;
...
}