Question: Hi there! I am having a problem with my assignment. The description/details of the assignment are listed below. When I execute my code, it prints
Hi there! I am having a problem with my assignment. The description/details of the assignment are listed below. When I execute my code, it prints the min, max, sum etc everytime I enter a number. I only want to print the info once. My other problem is have 3 decimal points after average. I tried .3%f but I couldn't get it to work. I look forward to hearing from you, thank you!
The number 1 is a prime number
The maximum positive number is: 1
The minimum positive number is: 1
The sum is: 1
The count of number(s) is: 1
The average is: 1
The number 2 is a prime number
The maximum positive number is: 2
The minimum positive number is: 2
The sum is: 3
The count of number(s) is: 2
The average is: 1.
Write a complete Java program called Assignment4 (saved in a file Assignment4.java) that reads in a sequence of positive integers until a user enters 0 to quit (we do not know how many numbers your program needs to read). Until a user enters 0, keep prompting:
Enter a positive integer. Enter 0 to quit.
For each positive integer, check if it is a prime number or not. If it is, print out:
The number X is a prime number.
If it is not, print out:
The number X is not a prime number.
Note that X is a positive number read from user input.
Once user enters 0, then stop asking for more positive integers, and compute the maximum and minimum among the read integers, and the sum, the count, and the average of these integers. These computations DO NOT include 0 -- the last read number. (note that max, min, sum, and count will be computed through iterations of a loop)
Your average should be formatted so that exactly 3 digits appears to the right of the decimal point.
Hint:
There are two parts to this program. The one reads a sequence of positive numbers and finds the maximum, minimum, sum, count, and average.
The other part is, for each positive integer, we need to determine if it is a prime number or not. A prime number (integer) is an integer that can be divided only by itself and 1. For example, 7 is a prime number and 6 is not (6 can be divided by 2 and 3). We can check if an integer can be divided by another integer using the remainder operator (%). If the remainder is 0, it can be divided. And we need to check this for all integers between 2 and n-1 for a positive integer n. You can write a program to compute this for one integer. Once you finish it, you can put it together with the first part mentioned above using another loop. (Thus, you will have nested loops).
My code so far:
import javax.swing.*;
class Assignment4 {
public static void main(String [] args) {
int sum=0;
int count=0;
int max=0;
int min=0;
double avg=0;
//begin loop, -Allows for multiple numbers to be stored.
for ( ; ; ) {
//Take input(s) from user
String userNumber = JOptionPane.showInputDialog("Enter a positive integer. Enter a 0 to quit.");
int number= Integer.parseInt(userNumber);
//end loop if 0 entered
if (number==0){
break;}
while(number>0) {
count++;
min=number;
if(number>max)
max=number;
if(number min=number; sum+=number; break; } int i=0; int prime=0; for(i=2; i<=(number/2); ++i) { if (number%1==0) prime=1; break;} if (prime==0) System.out.println("The number " + number + " is a prime number"); else System.out.println("The number "+number+" is not a prime number"); System.out.println("The maximum positive number is: " + max); System.out.println("The minimum positive number is: " + min); System.out.println("The sum is: " + sum); System.out.println("The count of number(s) is: " + count); System.out.println("The average is: " + sum/count); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
