Exercises:
1. Create a class that will bundle together several static methods for tax computations. This class should
not have a constructor.
public class TaxComputation{
public static double basicRate= 0.04;
public static double luxuryRate=0.10;
}
6. Create a class RoomOccupancy that can be used to record the number of people in the rooms of a
building.
public class RoomOccupancy{
private int numberInRoom;
private static int totalNumber=0;
public RoomOccupancy(){
numberInRoom=0;
}
Answer the following questions:
1. In what ways would a static variable useful?
oA static variable is common to all the objects with in the class because it's a class variable.
These variables can have static block, static method and static class in a java program.
2. In what ways would a static method be useful?
oStatic methods in java use no instance variables and will usually take input from the
parameters, perform actions on it, then return some result and they belong to a class with in
the program.
3. What values are returned by each of these statements:
a. Math.round(4.3)
o4
b. Math.round(4.7)
o5
c. Math.floor(2.8)
o2
d. (int) (Math.random() * 10.0) + 10
o19.99999
4. What is a wrapper class?
oA primitive wrapper class is a wrapper class hides or wraps data types from the eight
primitive data types, so that these can be used to create instantiated objects with methods in
another class(es).
5. Describe overloading?
oOverloading means that the same method name can be defined with more than one list of
formal parameters.
6. Explain Listing 6.20, an enhanced enumeration suit, in your own words.
oThe program is defined private instance variables and assign values in this case colors to the
objects in the enumeration. Which are: CLUBS("black"), DIAMONDS("red"),
HEARTS("red") and SPADES("black"). Then the program sets the value of CLUBS,
DIAMONDS, HEARTS and SPADES private instance variable color to the string they were
given earlier in the program. Since it is declared as final the colors can’t not change. The
method getColor provides public access to the value of color.
Powered by TCPDF (www.tcpdf.org)