COSC 236

Lab 10

The goal of this lab is to get used with basic Java notions introduced so far and Java methods (discussed in Lecture #9). 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 2 programs, given below.


1.  Write a Java program that consists of repeated calls to 2 methods to compute the sum and product of all integers between two bounds. For example if the lower bound is 3 and the upper bound is 7, the first method will return the sum 25 (3 + 4 + 5 + 6 + 7) and the second method will return the product 2520 (3 * 4 * 5 * 6 * 7). The program will include a loop (count controlled or sentinel controlled, your choice); inside the loop call the methods for various values of the lower bound and upper bound (user input - make sure you do input validation and check if the lower bound is less than the upper bound), and print the results.
The following specifications describe a method that computes the sum of all integers between 2 bounds:

     Input parameters: int lower, int upper (where lower is the lower bound and upper is the upper bound)
     Return: the sum of all numbers between lower and upper

The following specifications describe a method that computes the product of all integers between 2 bounds:

     Input parameters: int lower, int upper (where lower is the lower bound and upper is the upper bound)
     Return: the product of all numbers between lower and upper


2.  Write a Java program that uses a value-returning method to identify the prime numbers between 2 bounds (input from the user). The method should identify if a number is prime or not. Call it in a loop for all numbers between the 2 bounds and display only prime numbers. Check for errors in input.
Note: A number is prime if it is larger than 1 and it is divisible only by 1 and itself (Note: 1 is NOT a prime number)
Example: 15 is NOT prime because 15 is divisible by 1, 3, 5, and 15; 19 is prime because 19 is divisible only by 1 and 19.

Sample run:

How many times to test for prime numbers? -3
Error! Should be positive. Reenter: 3

Enter lower bound/upper bound: 5 3
Error! Lower bound should be larger. Reenter: 3 15
Prime numbers between 3 and 15 are: 3 5 7 11 13

Enter lower bound/upper bound: 2 25
Prime numbers between 2 and 25 are: 2 3 5 7 11 13 17 19 23

Enter lower bound/upper bound: 1 55
Prime numbers between 1 and 55 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53


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#10 in the Subject box of your e-mail.
C. In case you have any problems, contact the TA or the instructor for assistance.