Goals
- to read until end of file with a while loop
- to use a for loop
- to use a more general loop
Overview
For this lab, you will write two Java programs.
Together, they count as one assignment.
The save program counts 25% and the primefact program counts 75%.
The save program will read monetary amounts from the user until end of file. It will then report on how many values were read and their total.
The primefact program will compute the prime factors for all integers between two integers greater than or equal to 2 provided by the user. Prime factors of 84 are 2 x 2 x 3 x 7
Choose good variable names. Use methods and parameters when appropriate. Write descriptive and precise comments including at least one for each of your methods. Submit the programs separately as save and primefact.
Save Specification (save)
This program reads monetary values from the user until end of file.
It then reports on how many values were read and their total.
That total should have two digits after the decimal point and a dollar sign
in front. The number of values should be an integer.
Approaching the Problem
This is a straightforward program if you remember the accumulation plan:
- declare a counter (int) and initialize it to zero
- declare a total of the correct type (double) and initialize it to zero
- use a loop where every time through the loop you
- read a new value (in the loop condition here)
- increment (add one to) the counter
- add the current value to the total
- print the answers when you are done reading
Remember that reading until end of file with a Scanner, in, is done with:
while (in.hasNextDouble()) {
double value = in.nextDouble();
// stuff inside the loop
}
Use control-D to tell the system that you are done with input (Windows uses
control-Z).
That is how we indicate end-of-file from the console or terminal.
Use control-C to interrupt an infinite loop from the terminal, use the stop button within eclipse.
To print real numbers with two digits after the decimal point, you can either use a DecimalFormat object as in:
DecimalFormat fmt = new DecimalFormat("0.00");
System.out.println("\nThe total is $" + fmt.format(total));
or use:
System.out.printf("\nThe total is $%.2f\n", total);
Prime Factors Specification (primefact)
The input will be two integers each greater than or equal to 2. Your
program will print the prime factors of each integer between those two
values. The prime factors are the prime numbers that divide evenly into a
number.
They should be displayed for each value as shown in the example.
If the starting or ending value is not at least 2, print an error message and stop.
Note that values with more than 9 digits may not be read or stored properly as integers.
We know that if there is no remainder when a divisor divides a number, the divisor "evenly divides" the number. If divisor evenly divides a number and divisor is prime, divisor is one of its prime factors. Therefore, print it and divide number by it. If you start looking with 2, you'll print only prime factors.
This program is more complex than others you've written. But if you develop it using iterative enhancement, you can do it. It's easier than it looks especially when you use methods that each do one thing well.
Hints
- think about what happens for 42 - walk through the algorithm by hand
- start with a for loop to just print the values between start and stop
for (int num = start; num <= stop; num++) {
// stuff inside the loop
}
Example Input and Output
Starting value (at least 2): 57 Ending value (at least 2): 65 Prime factors for numbers between 57 and 65 57 = 3 x 19 58 = 2 x 29 59 = 59 60 = 2 x 2 x 3 x 5 61 = 61 62 = 2 x 31 63 = 3 x 3 x 7 64 = 2 x 2 x 2 x 2 x 2 x 2 65 = 5 x 13
Another Example Input and Output
Starting value (at least 2): 4 Ending value (at least 2): 1 Values must be at least 2 rather than 4 and 1. Quitting.
Another Example Input and Output note the empty range
Starting value (at least 2): 1024 Ending value (at least 2): 1000 Prime factors for numbers between 1024 and 1000