Based on the code below, please make a Javadocs(not generated by eclipse) that w
ID: 3828479 • Letter: B
Question
Based on the code below, please make a Javadocs(not generated by eclipse) that will describe each field and method of a class, its diffrent from pseudo code, it should be a list that's written into a word document and numbered. each method, attribute, etc should have a discription underneath it and labled by number, not comments in the code.
class TestScores
{
private int scores[]=new int[5];
public TestScores(int test[])
{
//to validate input test score
try
{
for(int i=0;i<5;i++)
{
if (test[i]<0||test[i]>100) //condition
//Throws an exception when test score is (Score <0 || Score >100)
throw new IllegalArgumentException("Number cannot" +
" be less than 0 and greater than 100");
else
scores[i]=test[i];
}
//function call to print average
System.out.println("Your Average Test Score is:"+Average());
}
catch(IllegalArgumentException e)
{
System.out.println(e);
}
}
//function average
public double Average()
{
//variable initialized to 0
int sum=0;
double avg;
for(int i=0;i<5;i++)
{
sum+=scores[i];
}
avg=sum/5;
return avg;
}//end Average
}//end TestScores class
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import java.util.Scanner;
class TestScoresDemo
{
public static void main(String args[]) throws Exception
{
Scanner kb = new Scanner(System.in);
int array[]=new int[5];//array declaration
//inputting test scores
System.out.println("Please Enter TestScores");
for(int i=0;i<5;i++)
array[i]=kb.nextInt();//Reading array of TestScores
//Creating instance to class TestScore
TestScores t1=new TestScores(array);
System.exit(0);
}
}
Explanation / Answer
Since we can only upload the images over here so i am just copying and pasting the description over here.
Line 1. Declaring a class as TestScores
Line 3. Define and declare an array of scores of data type integer with size of array as 5
Line 4. Make the 1 argument constructor with argument as array of integer type test[]
Line 7. Apply the try catch so that it will handle the exception if occurs
Line 9. Declaring a for loop with initialization from i=0 to I is less than 5 and incrementing value of I by 1
Line 11. Apply if condition that test[]array of I pos is less than 0 or array of test of I is greater than 100
Since it can throw an exception so, throw new IllegalArgumentException(“msg”)
Line 15-16 if the condition is not tru then goes in else part with scores[i]=test[i]
Then priniting the value of Average function declared below
Line 21-24. Catch will handle the excetion and System.out.println will print the exception e
Line 27. It will defined the average function whose return type is double
Line 29-37 these line are calculating the average of array of scores and saving the value in sum by adding the value in it by applying the for loop. It iterates for 5 times and calculates the sum of scores array and then the average is calculated by dividing the sum by 5 and the calculated average value is returned.
Line 42-55
This is the main class which is executed first because it has the main method.
We are taking the input from Scanner class.
Declaring the array of size 5 and type integer.
Now printing the scores by applying the for loop and reading value from user.
Creating instance to class TestScore and passing the array that we have just taking the input from user and after that System.exit(0); will executes and exit from the program.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.