Write A Program In A File Called MeanNumbers.java ✓ Solved
Write a program, in a file called MeanNumbers.java, that col
Write a program, in a file called MeanNumbers.java, that collect integer variables from the user until the user enters a non-positive value. If the user has entered less than two values, display a message "Insufficient data." and stop. If the user enters more than 10 values, display "Too many values" and stop. Otherwise, call the following methods and display the results: double arithmeticMean(int [] d), double harmonicMean(int [] d), and double geometricMean(int [] d). arithmeticMean should calculate the arithmetic mean: am= ∑i=0 N d[i] / N, harmonicMean should calculate the harmonic mean: hm= N / ∑i=0 N 1/d[i], and geometricMean should calculate the geometric mean: gm= N√(∏i=0 N d[i]).
Paper For Above Instructions
In today’s data-driven world, the ability to process and analyze numerical data is critical, making Java a popular programming language for such tasks. The following Java program, MeanNumbers.java, collects integers from the user, performs statistical calculations, and manages input conditions effectively. Below are the implementation details and functionality of the program.
Program Implementation
The first step in developing MeanNumbers.java is to create the necessary structure for capturing user input. The program utilizes a list to store the input numbers and leverages the functionality of Java's Scanner class to facilitate input collection.
Here’s the implementation of the program:
import java.util.ArrayList;
import java.util.Scanner;
public class MeanNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList
numbers = new ArrayList(); int input;
System.out.println("Enter positive integers (non-positive to stop):");
while (true) {
input = scanner.nextInt();
if (input
break;
}
numbers.add(input);
}
if (numbers.size()
System.out.println("Insufficient data.");
return;
} else if (numbers.size() > 10) {
System.out.println("Too many values.");
return;
}
int[] data = new int[numbers.size()];
for (int i = 0; i
data[i] = numbers.get(i);
}
System.out.println("Arithmetic Mean: " + arithmeticMean(data));
System.out.println("Harmonic Mean: " + harmonicMean(data));
System.out.println("Geometric Mean: " + geometricMean(data));
}
public static double arithmeticMean(int[] d) {
double sum = 0.0;
for (int num : d) {
sum += num;
}
return sum / d.length;
}
public static double harmonicMean(int[] d) {
double sum = 0.0;
for (int num : d) {
sum += 1.0 / num;
}
return d.length / sum;
}
public static double geometricMean(int[] d) {
double product = 1.0;
for (int num : d) {
product *= num;
}
return Math.pow(product, 1.0 / d.length);
}
}
Explanation of the Code
This Java program begins by importing the necessary packages: ArrayList for storing integers and Scanner for user input. The main method is defined to execute the program’s core functionality.
Firstly, it prompts the user to enter positive integers in an indefinite loop. The input loop continues until a non-positive integer is entered to terminate it. When this occurs, the program checks the size of the entered numbers:
- If less than two values are collected, it outputs "Insufficient data."
- If more than ten values are collected, it outputs "Too many values."
The program then converts the ArrayList of numbers into a traditional integer array since the mean functions require an array parameter.
Statistical Calculations
The program incorporates three methods to calculate the different types of means: arithmetic, harmonic, and geometric. Each method computes as follows:
- Arithmetic Mean: It sums all the numbers and divides by the count.
- Harmonic Mean: It sums the reciprocals of the numbers and divides the count by this sum.
- Geometric Mean: It multiplies all numbers together and takes the nth root (where n is the count of numbers).
Testing the Program
To ensure the functionality of the program, various test cases should be executed:
- Inputting fewer than two numbers (e.g., just one number) should trigger the "Insufficient data" message.
- Inputting more than ten numbers should trigger the "Too many values" message.
- Typical cases with two to ten numbers should correctly output all three means.
Conclusion
In conclusion, MeanNumbers.java effectively demonstrates how to process user input, apply condition checks, and perform mathematical calculations using Java. This program serves as a foundation for further development in statistical data analysis and can be expanded to handle various data types, interfaces, and more advanced statistical methodologies.
References
- Horstmann, C. S., & Cornell, G. (2013). Core Java Volume I: Fundamentals. Prentice Hall.
- Deitel, P. J., & Deitel, H. M. (2016). Java: How to Program. Pearson Education.
- Oracle Corporation. (2023). Java SE Documentation. Retrieved from https://docs.oracle.com/javase/8/docs/api/
- Martin, R. C. (2008). Clean Code: A Handbook of Agile Software Craftsmanship. Prentice Hall.
- Bloch, J. (2008). Effective Java. Addison-Wesley.
- Kollig, T., & Natarajan, P. (2015). Java Programming: Program Design Including Data Structures. Cengage Learning.
- McNaughton, S. (2019). Java Programming for Beginners: Comprehensive Approach to Learn Java Programming. Independently Published.
- GeeksforGeeks. (2023). Java Collection Framework. Retrieved from https://www.geeksforgeeks.org/collection-framework-java/
- Oracle. (n.d.). Java Tutorials. Retrieved from https://docs.oracle.com/javase/tutorial/
- Goodrich, M. T., & Tamassia, R. (2011). Data Structures and Algorithms in Java. Wiley.