In: Computer Science
Please can I get a flowchart and pseudocode for this java code. Thank you.
TestScore.java
import java.util.Scanner; ;//import Scanner to take input from
user
public class TestScore {
@SuppressWarnings("resource")
public static void main(String[] args) throws
ScoreException {//main method may throw Score exception
int [] arr = new int [5];
//creating an integer array for student id
arr[0] = 20025; //assigning id for
each student
arr[1] = 20026;
arr[2] = 20027;
arr[3] = 20031;
arr[4] = 20024;
int [] stuScore = new
int[arr.length]; //Creating an array of stuScore to store each stu
score
Scanner sc = new Scanner
(System.in); //Initializing Scanner input
for (int i = 0; i < arr.length;
i++) {//iterating through each student
System.out.print("enter score for
stu id " + arr[i] + ":");//prompting user to enter score
try {
stuScore[i] = sc.nextInt();//taking
user input
if (stuScore[i] <= 0) {//if
student score is less than or equal to 0
//throwing Score exception
throw new ScoreException(
"Score cannot be negative, so we
are assigning 0 by default to your score");
}
}
catch (ScoreException e) {
System.out.println(e.getMessage());//print only the message
stuScore[i] = 0; //assign the
value to 0
}
}
for (int i = 0; i < arr.length;
i++) {
System.out.println("stu id =" +
arr[i] + " score for stu:" + stuScore[i]); //printing stu id and
their corresponding score
}
}
}
ScoreException.java
public class ScoreException extends Exception {
ScoreException (String s) {
super(s);
}
}
Answer.
Step 1
Objective: We need to draw a flowchart and depict pseudocode for the given Java program. A flowchart, algorithm, or pseudocode should always be free from the syntax of any particular programming language. Hence, the overall processes are shown here.
Step 2
Pseudocode:
Start
Step 1: Create an integer array, arr to store student id of capacity 5.
Step 2: Assign id for each student as,
arr[0] := 20025
arr[1] := 20026
arr[2] := 20027
arr[3] := 20031
arr[4] := 20024
Step 3: Creating an array, stuScore to store score for each student
Step 4: for i := 0 to arr.length, i++
loop
Print a message to the user
Enter into the try block
Read a score and store in stuScore[i].
If stuScore[i] is less than or equal to 0 then,
throw ScoreException printing an error message
Assign stuScore[i] := 0
End if
End try block
End loop
Step 5: For int i := 0 to i arr.length, i++
loop
Print stu id and score
End loop
Step 6: End
Step 3
Flowchart:
Thank you.