CODERECTANGLE
  • Home
  • React.js
  • Java Tutorial
    • Introduction
    • Setting up the environment : Netbeans
    • Basics
    • FirstApp
    • Classes and Objects
    • Commonly Used Packages
    • Celsius to Fahrenheit Converter App
    • Celsius to Fahrenheit Using JOptionPane
    • Exception Handling
    • TestGradeApp
  • Objective-C
  • Python-Django
  • Contact

Testgrade app

5/31/2015

1 Comment

 
Program Specification : Create a program that will ask the students to enter their exam score and show their letter grades accordingly. Also, handle the possible Exceptions that can be thrown because of input errors.

First we write an Algorithm to explain stuff that we are going to code later on.

Algorithm :
                     Ask the user to enter a test score.
Determine the grade such as :
                          if the score is less than 60, then the grade is F.
                              Otherwise, if the score is less than 70, the grade is D.
                               Otherwise, if the score is less than 80, the grade is C.
                                 Otherwise, if the score is less than 90, the grade is B
                                    Otherwise the grade is A.


If we create a simple flowchart for this algorithm, then it would look like this.

Picture
Now its time to design a class for out program. Let name the class, TestGrade.java

Before we go into the coding part, we can write the structure and required elements in english.

we need :

score  :  A private int type field to hold the score that is numeric.
Constructor  : Accepts an argument for the score and store it in score variable.
setScore : Mutator method that accepts an argument for the score field.
getScore  : Accessor method that returns the value of the score field.
getLetterGrade  : A method that uses the nested decision structure above to determine a letter grade for the score.

The above structure of a class can be explained more formally using a diagram, which is called UML diagram.
Picture

Now, we will write the actual code following the above diagram.

TestGrade.java

/**
* The TestGrade class will determine a letter grade
*  based on a numeric testscore
*
*/
//This is how we write the class

public class TestGrade
{
           private   int   score;

         /**
           * The constructor accepts an argument
           * for the score field
           */

          public TestGrade(int s )
          {
                score =  s ;
          }

          /**
            * The setScore method accepts an argument
            * for the score field
            */


            public void SetScore(int s)
            {
              score = s;
            }

           /**
            * The getScore  method returns the score field.
            *
            */

           public int getScore()
           {
            return score;
           }

             /**
            * The getLetterGrade returns the letter
            * grade for the test score.
            */

 
             public char getLetterGrade()
             {
                    char grade;

                    if (score < 60)
                     {
                          grade =  'F';
                     }
                          else
                            {
                                   if(score < 70)
                                       {
                                          grade = 'D' ;
                                       }
                                       else
                                           {
                                                    if (score < 80)
                                                        {
                                                        grade = 'C' ;
                                                        }
                                                       else
                                                             {
                                                               if (score < 90)
                                                                     {
                                                                        grade = 'B';
                                                                     }
                                                                  else 
                                                                       {
                                                                       grade = 'A';
                                                                      }     
                                                      }
                                               }
               return grade ;
                        }
             }  
}

The above code is a very basic approach to code our class, but there are better ways to implement this code. I am going to stay with this anyway and will move to the driver class or the class which contains the main method.

TestResults.java

        import java.util.Scanner

            /**
             *  This program uses the TestGrade class to determine
             * a letter grade for a numeric test grade
             */


    public class TestResults
        {
                public static void main(String[]  args)
                    {
                       int testScore ;                       //To hold a test score
                       char letterGrade;              //To hold a letter grade

                     //Create a Scanner object to read input
                      Scanner keyboard = new Scanner(System.in);

                      //Get the numeric test score
                      System.out.print("ENter your numeric test score and" + "I will show you the grade");
                   
                       testScore = keyboard.nextInt();

                      //Create a TestGrade object with the numeric score
                     TestGrade test =  new TestGrade(testScore);
                    
                     //Now get the letter grade.
                     letterGrade  = test.getLetterGrade();


                      //Display the grade.
                      System.out.print("Your grade is " +test.getLetterGrade());

                    }

        }

1 Comment

    Author

    -Dipro
    SF State University
    Computer Science

    Archives

    August 2015
    May 2015

    RSS Feed

Powered by Create your own unique website with customizable templates.
  • Home
  • React.js
  • Java Tutorial
    • Introduction
    • Setting up the environment : Netbeans
    • Basics
    • FirstApp
    • Classes and Objects
    • Commonly Used Packages
    • Celsius to Fahrenheit Converter App
    • Celsius to Fahrenheit Using JOptionPane
    • Exception Handling
    • TestGradeApp
  • Objective-C
  • Python-Django
  • Contact