In class practice   Expressions, Input and Output

June 26, 2007

1. Run the following example:

Compute the area of a Circle

For following variables and expressions:

variable name Description data type initial value
PI A mathematic constant constant double 3.14
r radius double User Input
circleArea the area of a circle double

circleArea = PI*r*r;

We can have the corresponding Java program:

circleArea.java

2.Follow the above example and write Java programs based on the given variables and expressions. Please be careful to the data type. 

ASolution

Compute the volume of a sphere

variable name Description data type initial value
PI A mathematic constant constant double 3.14
r radius double User Input
sphereVol the volume of a sphere double

sphereVol = 3/4*PI*r*r*r;

BSolution

Compute the volume of a cuboid

variable name Description data type initial value
width width int User Input
height height int User Input
depth depth int User Input
cuboidVol the volume of a cuboid int

cuboidVol = width*height*depth;

C. Solution

Compute the traveling miles of a car for a given amount of money

variable name Description data type initial value
DOLLAR_PER_GALLON the gas price of a gallon constant double 3.12
MILAGE_PER_GALLON the milage per gallon constant double 27.2
money the total money double User Input
gasGallon the amount of gas double
distance the traveling distance double

gasGallon = money/DOLLAR_PER_GALLON;

distance=gasGallon*MILAGE_PER_GALLON;