3 Write a program that: a. reads from the terminal a sequence of numbers (intege
ID: 667165 • Letter: 3
Question
3 Write a program that: a. reads from the terminal a sequence of numbers (integers) b. saves them to a file with the name given from the command line c. calculates,then displays on the terminal, and also saves to that file the maximum, minimum, and average. Additional requirements: Store the numbers in a LinkedList<Integer>. Define a class DataAnalyzer that * has a constructor that stores the list of numbers: public DataAnalyzer(LinkedList<Integer> numList) {...} * has a method each for computing min(), max() and average(): public int min() {...}, etc. Define a class DataAnalyzerTester that reads the numbers from System.in, builds the number list, creates the DataAnalyzer object, and displays the min, max, and average using the DataAnalyzer instance. The DataAnalyzerTester class implements the main() method. Your code needs to handle invalid input and I/O exceptions. Write javadoc comments. Include both java files in your solution document.
Explanation / Answer
public class PrintNumberInWord { // saved as "PrintNumberInWord.java"
public static void main(String[] args) {
int number = 5;
// Using nested-if
if (number == 1) {
System.out.println("ONE");
} else if (......) {
......
} else if (......) {
......
......
} else {
......
}
// Using switch-case
switch(number) {
case 1: System.out.println("ONE"); break;
case 2: ......
......
......
default: System.out.println("OTHER");
}
}
}
public class AddInts {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
int sum = 0;
for (int i = 0; i < N; i++)
sum += StdIn.readInt();
StdOut.println("Sum is " + sum);
}
}
public class SumAndAverage { // saved as "SumAndAverage.java"
public static void main (String[] args) {
int sum = 0; // store the accumulated sum, init to 0
double average; // average in double
int lowerbound = 1; // the lower bound to sum
int upperbound = 100; // the upper bound to sum
for (int number = lowerbound; number <= upperbound; ++number) { // for loop
sum += number; // same as "sum = sum + number"
}
// Compute average in double. Beware that int/int produces int.
......
// Print sum and average.
......
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.