Using Java The user enters a integer number, say x So long as the number is not
ID: 3847642 • Letter: U
Question
Using Java
The user enters a integer number, say x So long as the number is not 0, the program does the following: Accumulate the value of x Asks for the next number Finally, find the sum and average of the numbers//print average with 2 decimal places//The output should be like//You have entered n integers. Sum = ?? and the average= nnn.nn;//Test your program using a) any 4 integers and b) 0. Use function; Continue from the other day; Write a method called "letterGrade" to assign the letter grade according to the score (use DTCC scoring scheme) The user asks for how many numbers to generate; For every number generate it has to be between 40..100 and call this number score; Call "letterGrade" and print the letter grade for it; Continue this process until the user enters 0 or less for step 2; Use Rand() and Array; User enters n Generate n random integer numbers which are less than 100; Print the content of the array and find max, min, sum, and average; Continue 1 to 3 until n = 5 or less;Explanation / Answer
Question 1.
import java.util.Scanner;
public class CombinedAssignment {
public static void main(String[] args) {
System.out.println("Question 1.");
Scanner sc = new Scanner(System.in); // initialize scanner to take user
// input
int x = -1;
int count = 0;
double sum = 0;
while (x != 0) {
x = Integer.parseInt(sc.next());
if (x != 0) {
sum = sum + x;
count++;
}
}
double average = sum / (double)count;
average = Math.floor(average * 100) / 100;
System.out.println("You have entered " + count + " integers. Sum = " + sum + " and the average=" + average);
}
}
Sample Run: -
Question 1.
132432
31123
3243
1334
0
You have entered 4 integers. Sum = 168132.0 and the average=42033.0
2.
Question 1.
0
You have entered 0 integers. Sum = 0.0 and the average=NaN
Question 2
import java.util.Random;
import java.util.Scanner;
public class CombinedAssignment {
public static void main(String[] args) {
System.out.println("Question 2.");
Scanner sc = new Scanner(System.in); // initialize scanner to take user
// input
System.out.println("How many numbers to generate? ");
int times;
Random r = new Random();
do {
times = Integer.parseInt(sc.next());
for (int i = 0; i < times; i++) {
int score = r.nextInt(60) + 1 + 40;
letterGrade(score);
}
System.out.println();
if (times > 0)
System.out.println("Enter value less than or equal to zero to stop , any other value to continue.");
} while (times > 0);
}
public static void letterGrade(int score) {
char grade;
if (score >= 92 && score <= 100) {
grade = 'A';
} else if (score >= 83 && score <= 91) {
grade = 'B';
} else if (score >= 75 && score <= 82) {
grade = 'C';
} else {
grade = 'F';
}
System.out.println("LetterGrade for score " + score + " is " + grade);
}
}
Sample Run: -
Question 2.
How many numbers to generate?
5
LetterGrade for score 67 is F
LetterGrade for score 73 is F
LetterGrade for score 92 is A
LetterGrade for score 51 is F
LetterGrade for score 88 is B
Enter value less than or equal to zero to stop , any other value to continue.
10
LetterGrade for score 55 is F
LetterGrade for score 87 is B
LetterGrade for score 75 is C
LetterGrade for score 71 is F
LetterGrade for score 82 is C
LetterGrade for score 100 is A
LetterGrade for score 91 is B
LetterGrade for score 89 is B
LetterGrade for score 74 is F
LetterGrade for score 93 is A
Enter value less than or equal to zero to stop , any other value to continue.
13
LetterGrade for score 87 is B
LetterGrade for score 59 is F
LetterGrade for score 93 is A
LetterGrade for score 86 is B
LetterGrade for score 64 is F
LetterGrade for score 55 is F
LetterGrade for score 50 is F
LetterGrade for score 42 is F
LetterGrade for score 92 is A
LetterGrade for score 51 is F
LetterGrade for score 74 is F
LetterGrade for score 55 is F
LetterGrade for score 48 is F
Enter value less than or equal to zero to stop , any other value to continue.
0
Question 3
import java.util.Random;
import java.util.Scanner;
public class CombinedAssignment {
public static void main(String[] args) {
System.out.println("Question 3.");
Scanner sc = new Scanner(System.in); // initialize scanner to take user
// input
int n = 8;
Random r = new Random();
while (n > 5) {
n = Integer.parseInt(sc.next());
if (n > 5) {
int[] array = new int[n];
double sum = 0;
for (int i = 0; i < array.length; i++) {
array[i] = r.nextInt(100);
sum = sum + array[i];
}
System.out.println("Array elements are: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
int min = array[0];
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
System.out.println(
"Max: " + max + ", Min: " + min + ", sum: " + sum + ", Average; " + (sum / (double) n));
}
}
}
}
Sample Run: -
Question 3.
6
Array elements are:
98 13 51 61 69 56
Max: 98, Min: 13, sum: 348.0, Average; 58.0
7
Array elements are:
81 62 71 41 8 82 27
Max: 82, Min: 8, sum: 372.0, Average; 53.142857142857146
8
Array elements are:
50 38 68 49 53 66 94 28
Max: 94, Min: 28, sum: 446.0, Average; 55.75
9
Array elements are:
74 48 18 56 61 19 65 65 72
Max: 74, Min: 18, sum: 478.0, Average; 53.111111111111114
10
Array elements are:
95 20 61 10 63 14 58 30 96 20
Max: 96, Min: 10, sum: 467.0, Average; 46.7
11
Array elements are:
53 95 72 48 76 38 31 79 93 74 8
Max: 95, Min: 8, sum: 667.0, Average; 60.63636363636363
12
Array elements are:
65 64 89 84 80 41 7 27 65 56 32 27
Max: 89, Min: 7, sum: 637.0, Average; 53.083333333333336
5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.