Java Reference
In-Depth Information
Display 4.6
A Formal Parameter Used as a Local Variable
(part 1 of 2)
This is the file
Bill.java.
1
import
java.util.Scanner;
2
public class
Bill
3{
4
public static final double
RATE = 150.00;
//Dollars per quarter hour
5
private int
hours;
6
private int
minutes;
7
private double
fee;
8
public void
inputTimeWorked()
9
{
10
System.out.println("Enter number of full hours worked");
11
System.out.println("followed by number of minutes:");
computeFee
uses the
parameter
minutesWorked
as a local variable.
12
Scanner keyboard =
new
Scanner(System.in);
13
hours = keyboard.nextInt();
14
minutes = keyboard.nextInt();
15
}
16
private double
computeFee(
int
hoursWorked,
int
minutesWorked)
17 {
18 minutesWorked = hoursWorked*60 + minutesWorked;
19
int
quarterHours = minutesWorked/15;
20
//Any remaining fraction of a quarter hour is not
//charged for.
21
return
quarterHours*RATE;
22
}
Although
minutes
is plugged in
for
minutesWorked
and
minutesWorked
is changed, the
value of
minutes
is not changed.
23
public void
updateFee()
24
{
25
fee = computeFee(hours, minutes);
26
}
27
public void
outputBill()
28
{
29
System.out.println("Time worked: ");
30
System.out.println(hours + " hours and " + minutes +
31
" minutes");
32
System.out.println("Rate: $" + RATE + " per quarter hour.");
33
System.out.println("Amount due: $" + fee);
34
}
35 }