Lab 8

advertisement
Lab 8
1903
Oct 31, 2007
Student Identification
Student number
____________________________________________
Last name
____________________________________________
First name
____________________________________________
Workstation user id
____________________________________________
Email address
____________________________________________
This lab covers basic material you need to be very familiar with: for loops, while
loops, sentinel values, end-of-file, if-else structures.
Question 1 deals with for loops; question 2 requires reading data till a sentinel is
encountered; question 3 deals requires reading data until end-of-file. Questions 2 and
3 are based on questions from the recent test.
On the last page you have clues how to read the data:
a) until a sentinel,
b) until end-of-file.
1. How many lines are printed by the following statement groups
Answer
for (int i=1; i<=10; i++)
for (int j=i; j<=10; j++)
{
System.out.println("hello");
}
for (int i=1; i<=10; i++)
for (int j=i-1; j<=i+1; j++)
{
System.out.println("hello");
}
int i=0;
while(i<10)
{
System.out.println("hello");
i++;
}
int i=0;
do
{
count++;
System.out.println("hello");
i++;
} while (i<10);
Show your results to the lab demonstrator:
initials
Lab 8
1903
Oct 31, 2007
2. Write a Java program that determines a minimum payment. When the user enters a
balance owing at the keyboard, the program responds by printing the balance owing
and the minimum payment. The minimum payment is calculated as 2% of the
balance owing. However, if this yields a result that is less than $10, then the
minimum payment is the minimum of $10 and the balance owing. Your program
must continue processing balances and calculating payments until the user enters the
sentinel value of 0.
Examples
Balance owing
2% of Amount owing
Minimum payment
$1000.00
$20.00
$20.00
$100.00
$2.00
$10.00
$ 5.00
$0.10
$ 5.00
Version 1.0: Notice this program reads input until a sentinel value is reached. As
version 1.0 of your program, you are urged to write the code that reads and prints the
balance owing until the user enters a 0 value. See the appendix if you are uncertain
about this.
Version 2.0: Once you have the input portion of this program working, you should
go on to version 2.0 that includes the calculation of the minimum payment.
Show a working version to your lab demonstrator:
initials
3. The table below indicates how grades are converted to grade point values.
Grade
A+
A
AB+
B
C+
D
F
Grade point value
4.5
4.25
4.0
3.5
3.0
2.5
1.0
0.0
Write a Java program that reads a single student’s history (StudentHistory.txt can be
downloaded or created yourself) and prints the student’s grade point average (gpa). The
gpa is calculated
gpa = Σ(credit hours * grade point) / Σ(credit hours)
Lab 8
1903
Oct 31, 2007
This calculation requires a sum (over all courses the student has taken) of the product of
credit hours and grade point, and it requires the sum (over all courses the student has
taken) of the credit hours.
At the UofW, the last character in a course “number” is a single digit for credit hours.
For instance, 92.4901/6 is the course 4901 in department 92 and it is a 6 credit hour
course. You may assume:
 The credit hour digit is in position 8 of the line. If line contains one line of
the student history then the credit hours is obtained via:
int crHrs=Character.digit(line.charAt(8), 10)

The grade starts in position 12 of the line. If line contains one line of a
student history, then the grade is obtained via:
String grade=line.substring(12);

Note that you need if-else-if structures such as:
if (grade.equals("A+")) gp = 4.5;
else if (grade.equals("A")) gp = 4.25;
else if (grade.equals("A-")) gp =4.0;
etc.
The lines in StudentHistory.txt happen to be:
92.4901/6
A
92.3913/3
B+
44.2915/3
B
55.3932/1
A+
Given the above data, the gpa calculation is
= ( 6*4.25 + 3*3.5 + 3* 3.0 + 1*4.5) / (6+3+3+1) = 49.5 / 13 =
3.81
Version 1.0: Notice this program reads input until end-of-file occurs. As version 1.0 of
your program you are urged to write the code that reads and prints the student history
until end-of-file is reached. See the appendix if you are uncertain about this.
Version 2.0: Once you have the input portion of this program working, you should go on
to version 2.0 that includes the calculation of a gpa. Your input loop must be modified to
accumulate the two sums: Σ(credit hours * grade point) and Σ(credit hours). When the
loop terminates you calculate and print the gpa.
Show a working version to your lab demonstrator:
initials
Lab 8
1903
Oct 31, 2007
APPENDIX
Reading until a sentinel
// prompt for amount owing
System.out.println("enter an amount, or 0 to end");
// open the keyboard for input.
Scanner keyboard = new Scanner(System.in);
double amountOwing = keyboard.nextDouble();
// other initializations
while (amountOwing != 0.0) //test for sentinel
{
// process the input here
System.out.println(amountOwing);
amountOwing = keyboard.nextDouble();
}
Reading until end-of-file
// make file available for input
FileReader freader = new FileReader("StudentHistory.txt");
BufferedReader br = new BufferedReader(freader);
String line = br.readLine();
// other initializations
while (line != null) // test for end-of-file
{
// process the line here
System.out.println(line);
line = br.readLine();
}
br.close();
Download