Question
i need help in java language Use your solution for Lab 1 and add an option to the main menu to fill the array with
i need help in java language
Use your solution for Lab 1 and add an option to the main menu to fill the array with values from a file the user will enter the filename to use. Note: the file will contain an int on the first line which is the number of floats stored in the file followed by the float values to add into the array (one per line). Be sure to handle every possible error condition. You will also add an option to save the current values to a file in the form of number of floats currently in the array followed by the array contents, one per line the user will enter the filename to use.I would like you to reuse the methods from Lab 1 to read in dataso its okay for this lab that the message display as if you were typing in input. However, you could change the method so that it takes in a boolean (true/false) parameter to specify when it is true that you are reading from a Scanner file (and in this case dont display the messages) or when it is false, that you want the messages displayed. You should create a new method addValues that takes in a scanner object (either standard in or a file) and then uses your addValue method from Lab 1.
also include the exception handle
this is my source code for lab1:
import java.util.Arrays; import java.util.Scanner; public class Numbers { private Float [] numbers; private int numItems; public Numbers() { //constractor numbers = new Float[100]; numItems = 0; } public Numbers(int size) { //methods numbers = new Float[size]; numItems = 0; } public void addValue(Scanner keyboard) { if(numbers.length == numItems){ System.out.println("Array full."); return;} System.out.print("Enter a value: "); Float value = keyboard.nextFloat(); numbers[numItems] = value; numItems++; } public float calcAverage() { Float sum = 0.0F; for(int i=0;i import java.util.Scanner; public class Lab1 { public static void displayMainMenu(){ Scanner sc = new Scanner(System.in); Numbers N = new Numbers(); boolean flag = true; while(flag){ //while loop System.out.println("Please select the following."); System.out.println("1: Initialize a default array."); System.out.println("2: To specify the max size of the array."); System.out.println("3: Add value to the array."); System.out.println("4: Display values in the array."); System.out.println("5: Display average of the values, minimum value, maximum value. "); System.out.println("6: To Exit."); System.out.print("> "); int option = sc.nextInt(); switch (option) { //swithch statements case 1: N = new Numbers(); break; case 2: System.out.print("Enter new size of array:" ); int size = sc.nextInt(); N = new Numbers(size); break; case 3: N.addValue(sc); break; case 4: System.out.println("Numbers are: "+N.toString()); break; case 5: System.out.print("Average is: "+N.calcAverage()); if(N.findMinMax()[1]!=Float.MIN_VALUE) System.out.print(", Maximum value: "+N.findMinMax()[0]); if(N.findMinMax()[1]!=Float.MAX_VALUE) System.out.print(", Minimum value: "+N.findMinMax()[1]); System.out.print(" "); break; case 6: System.out.print("Exiting..."); flag = false; break; default: }} } public static void main(String[] args) { //method the display the main menu displayMainMenu(); }}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started