Java Reference
In-Depth Information
else
//Line 21
System.out.println("Line 22: The radius of "
+ "both the circles are the same.");
//Line 22
} //end main
//Line 23
}
//Line 24
Sample Run: (In this sample run, the user input is shaded.)
Line 10: firstCircle: Radius = 0.00, Perimeter = 0.00, Area = 0.00
Line 11: secondCircle: Radius = 12.00, Perimeter = 75.40, Area = 118.44
Line 12: Enter the radius: 10
Line 16: firstCircle: Radius = 10.00, Perimeter = 62.83, Area = 98.70
Line 20: The radius of the first circle is less than the radius of the
second circle.
The preceding program works as follows. The statement in Line 7 creates the object
firstCircle and using the default constructor sets the radius to 0 . The statement in
Line 8 creates the object secondCircle and sets the radius to 12 . The statement in Line
9 declares the double variable radius . The statement in Line 10 outputs the radius, area,
and perimeter of the firstCircle . Similarly, the statement in Line 11 outputs the
radius, area, and perimeter of the secondCircle The statement in Line 12 prompts the
user to enter the value of radius . The statement in Line 13 stores the value entered by
the user in the variable radius . The statement in Line 15 uses the value of radius to set
the radius of firstCircle . The statement in Line 16 outputs the radius, area, and
perimeter of the firstCircle . The statements in Lines 17 to 23 compare the radius of
firstCircle and secondCircle and output the appropriate result.
EXAMPLE 8-5
In Example 7-3, the method rollDice rolls a pair of dice until the sum of the numbers
rolled is a given number and returns the number of times the dice are rolled to get the
desired sum. In fact, we can design a class that implements the basic properties of a die.
Consider the definition of the following class RollDie .
public class RollDie
{
private int num;
//Default constructor
//Sets the default number rolled by a die to 1
RollDie()
{
num = 1;
}
 
Search WWH ::




Custom Search