Java Reference
In-Depth Information
2
1
n
2
(
ǃ
x
i
ɨ
ǃ )
x
i
s =
n ɨ 1
You can compute this quantity by keeping track of the count, the sum, and
the sum of squares in the
DataSet
class as you process the input values.
Exercise P6.6. Factoring of integers. Write a program that asks the user
for an integer and then prints out all its factors in increasing order. For
example, when the user enters 150, the program should print
2
3
5
5
Use a class
FactorGenerator
with a constructor
FactorGenerator(int numberToFactor)
and methods
nextFactor
and
hasMoreFactors
. Supply a class
FactorPrinter
whose
main
method reads a user input, constructs a
FactorGenerator
object, and prints the factors.
Exercise P6.7. Prime numbers. Write a program that prompts the user for
an integer and then prints out all prime numbers up to that integer. For
example, when the user enters 20, the program should print
2
3
5
7
11
13
17
19
Recall that a number is a prime number if it is not divisible by any number
except 1 and itself.
Supply a class
PrimeGenerator
with a method
nextPrime
.
281