2 |
Suppose we had the following statement:
Vehicle v = new SuperFerry(this);
State which of the following statements would be valid. For those that are invalid state why.
- v.setVisible(true);
- v.moveRelative(20, 30);
- v.setSize(50, 80);
|
(a) and (c) are valid because they are defined in Vehicle. (b) is invalid
because moveRelative was only defined in SuperFerry. You can only use moveRelative
if the variable is of type SuperFerry or a subclass (if any exist). All is not lost however. If you really
want to use moveRelative you can do it by using casting. Here is code to do it:
SuperFerry sf = (SuperFerry)v
Even though it was stored as a Vehicle all the SuperFerry information still exists. We just have
to promote it to a SuperFerry by using a cast. That was done by putting the type in brackets.
Go back. |
3 |
- List all the fields available in Bunny. Indicate which class they were defined in.
- Ignoring the methods from Object, which methods are available in Bunny. Indicate
which class they were defined in or if they were overridden in Bunny.
|
- Fields from Face:
x, y, diameter, colour, eyeColour, isVisible
Fields from Bunny:
earColour, surpriseStatus
- Methods from Face:
setVisible(boolean), move(int, int), setSize(int), setColor(Color),
setEyeColor(Color), getX(), getY()
Methods from Bunny:
hop(int, int), setEarColor(Color), setSurprise(boolean)
Methods overridden in Bunny:
draw(), erase()
Go back. |
4 |
Consider a class that has the following form:
public class MyDate
{
int year, month, day;
/** Only the method signatures are given **/
public MyDate() // a method to intialize a date to Jan. 1, 1900
//set all fields. 1=Jan.,2=Feb.,...
public void setDate(int yr, int mn, int dy)
// returns a string in the form July 5, 2001
public String toString()
// allows the entry of a date from the keyboard
public void enterDate()
}
Answer the following questions:
- Implement the setDate method.
- Implement the toString() method.
- If you were implementing the enterDate method what consideration would you need to make?
- Write a fragment of code that will have a MyDate object called date. Set the date
to today's date. Print the date. Read a new date from the keyboard. Print the
date.
- Suppose you were going to create a new class DateTime that will inherit from MyDate.
In addition to storing a date the new class will allow you to store a time in 24 hour format. It will have
three additional fields: hour, minute and second. It will override the toString
method to produce a string in the format: July 5, 2001 - 4:52:11 p.m..
- Give the definition of the class (just give method signatures - don't fully implement)
- Would the following be legal? Explain.
DateTime dt = new MyDate();
|
-
public void setDate(int yr, int mn, int dy)
{
year = yr;
month = mn;
day = dy;
}
public String toString()
{
String[] months={"January","February","March","April","May","June",
"July","August","September","October","November","December"};
String s;
s = months[month-1] + " " + day + ", " + year;
return s;
}
- You would need to ensure that the year, month and date that the user entered was valid before you store
it. Otherwise other MyDate methods might get into trouble (for instance toString()).
MyDate date = new MyDate();
date.setDate(2001, 7, 5);
System.out.println(date.toString());
date.enterDate();
System.out.println(date.toString());
public class DateTime extends MyDate
{
int hour, minute, second;
public DateTime()
public void setTime(int hr, int mn, int sc)
public String toString()
public void enterTime()
}
- This is illegal since DateTime is a subclass of MyDate.
It would need to be the other way around. MyDate dt = new DateTime();
would be legal.
Go back. |