Question
this needs to be in java import java.util.Scanner; import java.io.*; import java.util.NoSuchElementException; public class Numbers { private Float[] numbers; Scanner in = new Scanner(System.in); public
this needs to be in java
import java.util.Scanner; import java.io.*; import java.util.NoSuchElementException;
public class Numbers { private Float[] numbers; Scanner in = new Scanner(System.in);
public Numbers() { /// write code here to intialize a "default" array since this is the default /// constructor numbers = new Float[10]; }
public Numbers(int size) { /// write code here to initialize an "initial" array given the parameter size as /// this is an initial constructor numbers = new Float[size]; }
public void initValuesInArray() { /// write code here to intialize the values in the array System.out.println("Enter the float numbers as values in the array:"); for (int i = 0; i
public String toString() { // write code to return the values in the array in a list format - one value per // line String numString = " ";
for (int i = 0; i
public float calcAverage() { // write code to return the average of the values in the array float total = 0;
for (int i = 0; i
public Boolean readFile() { // code to read numbers from a file String fileName = new String(); Scanner inFile = null;
System.out.println("Enter name of file to import"); fileName = in.next(); File file = new File(fileName);
try { if (file.exists()) { System.out.println("File exists"); inFile = new Scanner(file);
if (!inFile.hasNextInt()) { System.out.println("Invalid data in file - did not process"); inFile.close(); return false; } else { int fileArraySize = inFile.nextInt(); numbers = new Float[fileArraySize];
while (inFile.hasNextInt()) { try { int counter = 0; for (int i = 0; i
} } else { System.out.println("File does not exist"); return false; } } catch (IOException ex) { System.out.println("Could not open file"); return false; } inFile.close(); return true; }
}
import java.util.InputMismatchException; import java.util.Scanner;
public class Lab2Main {
public static void main(String[] args) { // write the code here to implement the menu as specified in Lab 1
Scanner keyboard = new Scanner(System.in); int choice = 0; Numbers num = new Numbers();
while (choice != 7) { System.out.println( "Enter 1 to initialize a default array, 2 to initialize an array of input size, 3 fill array with values, 4 display values in array, 5 to display average of the values in the array, 6 to read from file, 7 to quit"); try { choice = keyboard.nextInt(); } catch (InputMismatchException ex) { keyboard.next(); } switch (choice) { case 1: num = new Numbers(); break; case 2: int size; do { System.out.println("Enter new size of array"); while (!keyboard.hasNextInt()) { System.out.println("Enter a valid number"); keyboard.next(); } size = keyboard.nextInt(); } while (size
}
}
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