CS100M --> Question(s) of the Week
The "Question of the Week" will be discussed and solved during an optional Monday Section for students who would like additional help and practice. The Question of the Week will be posted here on Fridays and will be solved the following Monday. Note that your regular, required, section may or may not cover the Question of the Week.
Professor Van Loan, Professor Fan, and upper year CS students will teach the "Monday Section." The Monday Section will take place on Mondays (of course) 3:35 to 4:25pm in Upson 211. The optional Monday Section will start 1/27 (2nd week of class) and will run until Spring Break. We may extend the Monday Section until the end of the semester depending on demand.
13: Inheritance
| Consider class Coin below.
Class Coin represents a coin with only one state variable:
the side of the coin that faces up after a flip (heads or tails).
You will extend class
Coin to represent a coin with monetary value--class
MoneyCoin. You will then extend class MoneyCoin
to represent a "collectible" coin--class OldMoneyCoin. |
public class Coin {
public final int HEADS = 0;
public final int TAILS = 1;
private int face;
public Coin() { flip(); }
public void flip() { face = (int) (Math.random() * 2); }
public int getFace() { return face; }
public int getValue() { return 0; } //no monetary value
public String toString() {
String faceName;
if (face == HEADS)
faceName = "heads";
else
faceName = "tails";
return faceName;
}
}
|
Class MoneyCoin is a subclass of Coin and has
the following members (variables and methods):
- static integer array CENTS contains the fixed monetary
values of the coins that we will use (1-, 5-, 10-, and 25-cent coins).
- static String array NAMES contains the
common names of the coins we will use
- private integer instance variable value:
the monetary value in cents
- protected String instance variable name:
the common name of the coin
- Constructor takes one integer input argument representing the monetary
value of the MoneyCoin and assigns values to the fields. If
the input parameter value is not one of the values in array
CENTS above, then set field value to
0
and field name to "unknown coin".
- Instance method getValue returns the monetary value of the
MoneyCoin.
- Instance method toString returns the
String description of the MoneyCoin including
the common name and the face (that lands up).
Class OldMoneyCoin is a subclass of MoneyCoin
and has the following members:
- Class integer constant OLD with value 1950. We will treat
coins minted in or before the year 1950 as old and collectible! I.e., its
worth is higher than its value as a currency.
- private integer instance variable worth
- private integer instance variable year:
the year that the coin is minted
- Constructor takes two integer input arguments representing the monetary
value and the year of minting and assigns values to the fields. If the coin is
considered old as defined above, then its worth is
twice its monetary value. Otherwise the worth is the same as
the monetary value.
- Instance method getValue returns the worth
instead of the value.
- Instance method toString returns the
String description of the OldMoneyCoin, always
adding the word "old" in front of the coin's
name.
|
|
Questions of previous weeks