Java Reference
In-Depth Information
Self-Test Exercises
5. Write a method called
fractionDone
that could be added to the class
DateThirdTry
in Display 4.4. The method
fractionDone
has a parameter
targetDay
of type
int
(for a day of the month) and returns a value of type
double
. The value returned is
the value of the
day
instance variable divided by the
int
parameter
targetDay
. (So
it returns the fraction of the time passed so far this month where the goal is reaching
the
targetDay
.) Do floating-point division, not integer division. To get floating-
point division, copy the value of the
day
instance variable into a local variable of
type
double
and use this local variable in place of the
day
instance variable in the
division. (You may assume the parameter
targetDay
is a valid day of the month
that is greater than the value of the
day
instance variable.)
6. Write a method called
advanceYear
that could be added to the class
DateThirdTry
in Display 4.4. The method
advanceYear
has one parameter of
type
int
. The method
advanceYear
increases the value of the
year
instance vari-
able by the amount of this one parameter.
7. Suppose we redefine the method
setDate
in Display 4.4 to the following:
public
void
setDate(
int
newMonth,
int
newDay,
int
newYear)
{
month = monthString(newMonth);
day = newDay;
year = newYear;
System.out.println("Date changed to "
+ newMonth + " " + newDay + ", " + newYear);
}
Indicate all instances of
newMonth
that have their value changed to
6
in the
following invocation (also from Display 4.4):
date.setDate(6, 17, year);
8. Is the following a legal method definition that could be added to the class
DateThirdTry
in Display 4.4?
public
void
multiWriteOutput(
int
count)
{
while
(count > 0)
{
writeOutput();
count
−−
;
}
}
9. Consider the definition of the method
monthString
in Display 4.4. Why are there
no
break
statements in the
switch
statement?