java code for sample projects loops

4
Loops and Conditional Statements Project Page 1 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; JavaCode for Sample Projects

Upload: jwjablonski

Post on 04-Jul-2015

729 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Code for Sample Projects Loops

Loops and Conditional Statements Project Page 1

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;

JavaCode for Sample Projects

Page 2: Java Code for Sample Projects Loops

Loops and Conditional Statements Project Page 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);

Page 3: Java Code for Sample Projects Loops

Loops and Conditional Statements Project Page 3

} public static double calculateArAverage (double arNumbers []) { doublearAverage = 0; doublearTotal = 0; intnumArElements = 5; int count = 0; for (count = 0 ; count <numArElements; count++) { arTotal = arTotal + arNumbers[count]; } arAverage = (arTotal/numArElements); returnarAverage; }

Page 4: Java Code for Sample Projects Loops

Loops and Conditional Statements Project Page 4

public static String calculateGrade(double testScore) { String letterGrade; if (testScore< 60) { letterGrade = "F"; } else if (testScore< 70) { letterGrade = "D"; } else if (testScore< 80) { letterGrade = "C"; } else if (testScore< 90) { letterGrade = "B"; } else { letterGrade = "A"; } returnletterGrade; } }