COSC 236

Lab 6

The goal of this lab is to get used with basic Java notions introduced so far and  while statements (discussed in Lecture #6). Take a look at the examples and the sample programs in the lecture notes and try to apply the same concepts and style when writing your own programs. You will write 3 programs, given below.


1.   Grades problem: Last week you wrote a Java program that computes a student's total score and final grade. Modify that program so that you add a variable for student id (of type int). You should input for each student the student ID, the assignments, and the test results. To stop the loop you will enter a fake student whose id is 0 (sentinel). Compute the total score and final grade for every student. Finally, compute the average score for the entire class.

SAMPLE OUTPUT:

Enter student id# (0 to STOP): 1
Enter values for test#1, test#2, final exam and assignments: 89 90 87 80
For test#1 = 89, test#2 = 90, final = 87, and assignments = 80 total score is 85.65 and letter grade is B.

Enter student id# (0 to STOP): 2
Enter values for test#1, test#2, final exam and assignments: 78 67 58 70
For test#1 = 78, test#2 = 67, final = 58, and assignments = 70 total score is 65.95 and letter grade is D.

Enter student id# (0 to STOP): 3
Enter values for test#1, test#2, final exam and assignments: 89 90 98 95
For test#1 = 89, test#2 = 90, final = 98, and assignments = 95 total score is 94.55 and letter grade is A.

Enter student id# (0 to STOP): 0
===============================
Class average is 82.05


2.   The factorial of a positive integer n is the product of the integers from 1 to n. You can express the factorial of a positive integer n (in mathematics this is denoted by n!) using the following formula:

n! = 1 * 2 * 3 * ... * (n - 1) * n

Write a Java program that will compute the factorial of some numbers n (input from the user, accept only range 1 - 10). For each valid number in input, the output should be the value of n!. Your program should use a loop, to allow the user to input more than one number (count-controlled or sentinel-controlled, your choice)

SAMPLE OUTPUT:

Do you want to start(Y/N): Y
Enter an integer (1 - 10): 4
4! = 24

Do you want to continue(Y/N): y
Enter an integer (1 - 10): 5
5! = 120

Do you want to continue(Y/N): Y
Enter an integer (1 - 10): 8
8! = 40320

Do you want to continue(Y/N): y
Enter an integer (1 - 10): 11
ERROR! OUT OF RANGE (1 - 10). REENTER: 3
3! = 6

Do you want to continue(Y/N): y
Enter an integer (1 - 10): 10
10! = 3628800

Do you want to continue(Y/N): n


3. Write a Java program that first asks the user how many numbers will be entered - let's call this positive integer n. Then the program takes input  n integers (not ordered) and outputs how many even values and how many odd values have been entered. Input control: the input should be restricted to only integers between 0 and 100.

SAMPLE OUTPUT:

How many numbers in input? 10
Now enter 10 integers:
123
ERROR! Valid range 0 - 100. REENTER: 1
2
3
129
ERROR! Valid range 0 - 100. REENTER: 4
5
6
7
176
ERROR! Valid range 0 - 100. REENTER: 8
9
10
You entered 5 odd numbers and 5 even numbers.


Notes:
A. The lab will NOT be graded, do NOT hand anything in to the instructor.
B. The lab should be completed by the start of the next scheduled lab class.
Save the .java files on your disk and e-mail them (attachments) to Vishal Patel at vpatel12@students.towson.edu

Very important: Make sure that you have COSC 236.section, your name, and Lab#6 in the Subject box of your e-mail.
C. In case you have any problems, contact the TA or the instructor for assistance.