1. Engineering
  2. Computer Science
  3. this needs to be done in java c code mport...

Question: this needs to be done in java c code mport...

Question details

This needs to be done in JAVA
Problem Description: Use your solution for Lab 1 and add a menu option to fill the array with values from a file. The first entry in the file will be the number of values that follow (ie the size of the array). Be sure to handle every possible error condition. Test your code through the Lab 2 test plan below and the Lab 2 Sample Files (although not all error conditions are in the sample files - so you may need to make more files - use Notepad) Menu Testing Condition Menu input of 6 first Menu input of 2 2 (then enter 3 for size of new Input accepted, back to menu Input Result 6, then Prompt for name of File should be processed per testing below Menu input of 4 Menu input of 3 Menu input of 5 Menu input of 6 arra 4 3 (then values -1-2-3 5 6, then Prompt for name of Values should print 0.0 0.0 0.0 User should be prompted to enter 3 numbers Average of -2.0 should be displayed File should be processed per testing below Menu input of 7 Program ends File Testing Condition Bad file name Good file Input lab2 lab2.txt-contains 4 1234 Result Error Message - file does not exist. (Back to menu) Input accepted, data read, back to menu Menu input of 4 Menu input of 5 Bad file -wrong number of entries Bad file -invalid value Lab2bad2.txt -containsError Message - Invalid data in file -did not process for number of entries A 123 Bad file invalid value Lab2bad3.txt-contains Error Message - Invalid data in file did not process for an entr Bad file -contains more values than number of entries Values 1 2 3 4 should display Average 2.5 should display Lab2bad1.txt - contains Error Message - missing values in file - did not process 4123 file (Back to menu) 412 a3 Program processes number of entries and ignore rest ofc

code:

mport java.util.*;

class Numbers {

float [] numbers;

int size=10; // default size

// default constructor
public Numbers() {


numbers = new float[10];

}
//parameterized constructor
public Numbers (int size) {

this.size=size;

}
// A method to take values into an array
public void initValuesInArray()
{

numbers = new float[10];
Scanner s=new Scanner(System.in);
for(int i=0;i<numbers.length;i++)
{
System.out.println("Enter value");
numbers[i]=s.nextFloat();

}


}

public void toStrings() {
for(int i=0;i<numbers.length;i++)
System.out.println(numbers[i]);
}

public float calcAverage() {

float total = 0;

        for(int i=0; i<numbers.length; i++)
        {
       // calculating total of an array
           total = total + numbers[i];
        }

// finding average of given floating point array
        float average = total / numbers.length;
              
return average;

}

}
//main class
class Lab1Main
{
public static void main(String args[])
{

int k=5;
Numbers obj=new Numbers();
Scanner s=new Scanner(System.in);
while(k>0)
{
System.out.println("1 to initialize a default array");
System.out.println("2 to initialize an array of input size");
System.out.println("3 fill array with values,");
System.out.println("4 display values in array");
System.out.println("5 to display average of the values in the array");
System.out.println("6 to quit");

System.out.println("Enter a choice");
int c=s.nextInt();
if(c==6)
break;

switch (c) {
         case 1:
           Numbers n1=new Numbers();
           break;
         case 2:
           Numbers n2=new Numbers(12);
       break;
         case 3:
         
           obj.initValuesInArray();
           break;
         case 4:
         
           obj.toStrings();
           break;
         case 5:
         
           float res=obj.calcAverage();
           System.out.println("Average is"+res);
           break;
       
         default:
           System.out.println("Invalid choice!! Try Again");
           break;
      }
}
}
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution