Description
Question 1. (Stats.java) Write a program that prompts the user to enter an arbitrary (this
means unknown) number of positive double numbers. When the user enters a negative number the
input is complete. At this point the program should display the count, maximum, minimum, sum,
product, average and range (distance between maximum and minimum) of the numbers. Assume
the user enters properly-formatted numbers as input. Hint: most of this work can be done inside of
a single while loop.
C:\cosc1046\a2\>java Stats
Enter a double number (negative to quit): 1.0
2.0
3.0
4.0
5.0
-1
Count: 5
Max: 5.0
Min: 1.0
Sum: 15.0
Product: 120.0
Average: 3.0
Range: 4.0
Program complete.
C:\cosc1046\a2\>java Stats
Enter a double number (negative to quit): 10.39
14.219
81.2
0.001
9.983
6.9
12
10012.34
-2
Count: 8
Max: 10012.34
Min: 0.0010
Sum: 10147.033
Product: 9.928133947689667E7
Average: 1268.379125
Range: 10012.339
Program complete.
Question 2. (Nested.java) Write nested for loops that will display a multiplication table. Ask
for an integer from the user. Continue to re-prompt the user as long as the input is less than 1 or
greater than 10. When you get valid input display a table similar to what is shown below.
C:\cosc1046\a2\>java Nested
Enter an integer between 1 and 10:-1
Enter an integer between 1 and 10:0
Enter an integer between 1 and 10:4
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
C:\cosc1046\a2\>java Nested
Enter an integer between 1 and 10:7
1 2 3 4 5 6 7
2 4 6 8 10 12 14
3 6 9 12 15 18 21
4 8 12 16 20 24 28
5 10 15 20 25 30 35
6 12 18 24 30 36 42
7 14 21 28 35 42 49
If you look at the output above you’ll see that it is symmetric on the diagonal. Try removing the
duplicate output so that it looks more like this:
C:\cosc1046\a2\>java Nested
Enter an integer between 1 and 10:4
1 2 3 4
4 6 8
9 12
16
Question 3. (Primes.java) Write a program that prompts for an integer input (assume the user enters a
positive integer, you don’t need to do input checking this time). Compute and display all of the prime
numbers up to and including that integer. Remember, a prime number is a number that is only divisible by 1
and itself.
There are lots of ways to solve this problem. Here is pseudo code for one possible solution that describes the
process and assumes the user requested all of the primes between 1 and n:
boolean primeFlag set to true
For each number i between 2 and n:
For each number j between 2 and i:
if the remainder of j / i is not zero
set primeFlag to false
Check the value of primeFlag and print the number if value was prime (ignore otherwise)
C:\cosc1046\a2\>java Primes
Enter an integer value: 20
Prime numbers:
2
3
5
7
11
13
17
19
C:\cosc1046\a2\>java Primes
Enter an integer value: 100
Prime numbers:
2
3
5
7
… more numbers not displayed here
983
991
997
Two things for these marks. 1. Take two integers as inputs and check only for the prime numbers between
the two digits. 2. The pseudo code above works but is VERY inefficient. Make your program more efficient –
Don’t use primeFlag at all and you don’t need to check all of the possible divisors so have your inner loop end
immediately when you’ve checked enough of them to ensure the number is NOT prime.
C:\cosc1046\a2\>java Primes
Enter two integer values: 10 20
Prime numbers:
11
13
17
19