Download Lab 6 - Loops Galore (Save, PrimeFact)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Pattern recognition wikipedia , lookup

Time value of money wikipedia , lookup

Corecursion wikipedia , lookup

Transcript
Lab 6 - Loops Galore (Save, PrimeFact)
CS 161 - Fall 2016
Due date: 11pm, Tuesday, October 18th
Goals
- to read until end of file with a while loop
- to use a counting for loop
- to build test cases
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 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
- 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, input, is done with:
while(input.hasNextDouble()){
doublevalue=input.nextDouble();
//stuffinsidetheloop
}
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, use printf:
System.out.printf("\nThetotalis$%.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
- create the test case showing the result for 42
- start with a for loop to just print the values between start and stop
for(intnum=start;num<=stop;num++){
//stuffinsidetheloop
}
- write a method to compute the prime factors of one number
- this is a complex fencepost problem. You might start just adding an extra X.
- You may wish to include an if to skip the first or last X. Count the factors or find another
thing to test. There are many ways to do this. Alternatively, it is possible to print the last
value after the loop, but not very easy to print the first value before the loop.
Example Input and Output
Startingvalue(atleast2):59
Endingvalue(atleast2):65
Primefactorsfornumbersbetween59and65
59=59
60=2x2x3x5
61=61
62=2x31
63=3x3x7
64=2x2x2x2x2x2
65=5x13
Another Example Input and Output
Startingvalue(atleast2):4
Endingvalue(atleast2):1
Valuesmustbeatleast2ratherthan4and1.Quitting.
Another Example Input and Output note the empty range
Startingvalue(atleast2):1024
Endingvalue(atleast2):1000
Primefactorsfornumbersbetween1024and1000