please rewrite this code as Pseudo-Code,.. basically rewrite the code but in eng
ID: 3833413 • Letter: P
Question
please rewrite this code as Pseudo-Code,.. basically rewrite the code but in english language , Thank you so much!
for example if(5>x), should be if(5 is greater than x)
***************************************************************************
public class InvalidTestScore extends Exception
{
//Constructor that takes double type score
//calls the super class Exception class
public InvalidTestScore(double badScores)
{
super("Error: Number cannot be less than 0 and greater than 100 i.e" + badScores);
}
}
**********************************************************************************************
public class TestScores
{
//Modify the data type of scores to double since
//from main method of the Chp12PC02 class , the passing
//array type is double
private final double scores[] = new double [5];
/**
* constructor
* @param badScores
* @throws chp12pc02.InvalidTestScore
* @throws InvalidTestScores
*/
//Modify the passing array type as double
public TestScores(double[] badScores) throws InvalidTestScore
{
// input for test score
for (int index = 0; index < 5; index++)
{
if (badScores[index] < 0 || badScores[index] > 100)
{
//throws an InvalidTestScore exception if score is less than
//0 or greater than 100
throw new InvalidTestScore(badScores[index]);
}
else
{
scores[index] = badScores[index];
//Do not call the average method from this position
}
}
}
//Modify the name of the method getAverage since from
//main , the program calling as getAverage
public double getAverage()
{
//Modify the sum as double type
double sum = 0;
for(int index = 0; index < 5; index++)
{
sum += scores[index];
}
//return the average of the scores
return sum / 5;
}
}
******************************************************************************************
/**Modified java program TestScoresDemo to throws an InvalidTestScore
* exception if the score of the array is less than zero or greater
* than 100.*/
public class TestScoresDemo
{
public static void main(String[] args)
{
/**
* an array with test scores. notice that element 3 contains an
* invalid score
*/
//This array badScores contains an invalid score 101
double[] badScores = {97.5, 66.7, 88.0, 101.0, 99.0};
/**
* create a TestScores object initialized with badScores
*/
try
{
//The constructor in the class TestScores throws invalid Test score exception
//since 101 is greater than 100.
TestScores tBad = new TestScores(badScores);
// the following statement should not execute
System.out.println("The average of the bad scores is " +
tBad.getAverage());
}
catch (InvalidTestScore e)
{
System.out.println("Invalid score found. " + e.getMessage());
}
/**
* array with test scores. all of these scores are good
*/
double[] goodScores = {97.5, 66.7, 88.0, 100.0, 99.0};
/**
* create a TestScores object initialized with goodScores
*/
try
{
//This constructor do not throw InvalidTestScore exception
//since the array goodScores do not contain any invalid score
TestScores tGood = new TestScores(goodScores);
System.out.println("The average of the good scores is " +
tGood.getAverage());
}
catch (InvalidTestScore e)
{
System.out.println("Invalid score found. " + e.getMessage());
}
}
}
Explanation / Answer
Below is the pseudo Code for Calculating Average:
// Declare an array containing invalid scores also
double[] A = {97.5, 66.7, 88.0, 101.0, 99.0};
Try:
While(for Each Score, in array A above):
If(Score is InValid):
Throw Exception("Error: Score cannot be less than 0 and greater than 100")
// No Score is Invalid, SO Now try to calculate the average
sum = 0, average = 0; // declare variables
While(for Each Score, in array A above):
sum += score // add score to sum
average = sum/count
Output("Average of the Scores is: " + average)
Catch:
If any Exception Occurs, Print that Some of the scores are invalid.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.