Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prepare a Class and a Test Program. Prepare a Class using an object-oriented pro

ID: 3847840 • Letter: P

Question

Prepare a Class and a Test Program.

Prepare a Class using an object-oriented programming language of your choice for computing the average age of SWE 3643 students found either in a text file or in a data structure of your choice such as an array. You will also need to prepare a test program that uses the Class you designed and uses the inputs listed below to test whether the class calculates the average age correctly. See below for a description of the various text files the program should read. Come up with meaningful error messages for each input file case that does not allow the program to calculate the correct average age. The minimum and maximum age for a student are 15 and 70 respectively. The maximum number of students in a class is 11. Make sure appropriate messages are displayed.

Input file 1 will be an empty file.

Input file 2 will only have one valid age value.

Input file 3 will have the following invalid values: 0, -5, 1,000,000, R, @, and }

Input file 4 will have the following values: 13, 17, 19, 30, 45, 55

Input file 5 will have the following values 18, 19, 20, 21, 23, 18, 25, 32, 45, 66.

Input file 6 will have the following values 15, 16, 18, 16, 17, 18, 19, 20, 22, 21, 33, 34, 23, 15

A sample error message received when using input file 1 is Input file 1: File is empty

Make sure the program is well commented. Run all the text cases in just one pass. User keying-in of test values will not be allowed.

Explanation / Answer

public class Student{
  
static void validateAverageAge(int Ages[])
{
//check if array is empty
if(Ages.length==0)
{
System.out.println("Given file is empty");
}
else if(Ages.lengh>11)
{
System.out.println("Max number of students should not exceed 11");
}
else
{
int avg=0;
for(int i=0;Ages.length;i++)
{
int age=Ages[i];
if(age<15 || age>70)
{
System.out.println("Age of students should be in the range 15-70");
return;
}
avg +=age;
}
avg /=Ages.length;
System.out.println("Avg age of students is :"+avg);
}
}
  
  

public static void main(String []args){
int arr1[] = {};
int arr2[] ={20};
int arr3[] ={0,-5,1000000,'R','@','}'};
int arr4[] ={13,17,19,30,45,55};
int arr5[] ={15,16,18,16,17,18,19,20,22,21,33,34,23,15};
Student.validateAverageAge(arr1);
Student.validateAverageAge(arr2);
Student.validateAverageAge(arr3);
Student.validateAverageAge(arr4);
Student.validateAverageAge(arr5);
}
}