際際滷

際際滷Share a Scribd company logo
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
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
}

       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;
       }




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

}




Loops and Conditional Statements Project                       Page 4

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
  • 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; } Loops and Conditional Statements Project Page 3
  • 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; } } Loops and Conditional Statements Project Page 4