The document describes a Java code sample that uses loops and conditional statements to calculate test scores and grades. It contains code to:
1) Take user input for 5 test scores and store them in an array.
2) Calculate a letter grade for each score using an if/else statement block.
3) Calculate the average of all scores by summing the array elements and dividing by the length.
4) Print a report with each test number, score, and grade along with the overall average.
1 of 4
Downloaded 12 times
More Related Content
Java Code for Sample Projects Loops
1. JavaCode for Sample Projects
Loops and Conditional StatementsProject
TestAverageAndGrade.java File
importjavax.swing.*;
public class TestAverageAndGrade
{
public static void main(String[] args)
{
String strScore;
double score;
doubleaverageTestScore = 0;
String grade = "";
doublearTestScores[] = new double [5] ;
String arLetterGrades[] = new String [5];
inttotalNumberOfTests = 5;
inttestIdentifier = 1;
Loops and Conditional Statements Project Page 1
2. while (testIdentifier<= totalNumberOfTests)
{
//take string input and convert to double
strScore = JOptionPane.showInputDialog("Enter the score for Test #" + testIdentifier );
score = Integer.parseInt(strScore);
//assign input score to an array of scores
arTestScores[(testIdentifier - 1)] = score;
//convert score to letter grade using calculateGrade method and assign to array of grades
grade = calculateGrade (score);
arLetterGrades[(testIdentifier - 1)] = grade;
//increment testIdentifier counter
testIdentifier = testIdentifier + 1;
}
//Reset testIdentifier to 1 for use in reporting
testIdentifier = 1;
//calculate average of scores using calculateArAverage method
averageTestScore = calculateArAverage (arTestScores );
//Print Report
System.out.println("TEST_____SCORE_____GRADE");
while (testIdentifier<= totalNumberOfTests)
{
System.out.println("# " + testIdentifier + " "+ arTestScores [(testIdentifier - 1)] + " "
+ arLetterGrades[(testIdentifier - 1)]);
testIdentifier = testIdentifier + 1;
}
System.out.println("The average score for all the tests taken is: " + averageTestScore);
Loops and Conditional Statements Project Page 2