Semester Grade Calculator – Take 2

Blue Pelican Java BookFollowing our review from the Thanksgiving break return, we’ll be covering an introduction to object oriented programming.

The first lesson will be a simple lesson almost verbatim from Blue Pelican Java. however, the second lesson will be to take the semester exam grade calculator created earlier in the week and adapt principles of OOP to it.

The code they are starting with is similar to the following:

//Program Name: Unit 1 & 2 Review
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Fall 2016

import java.util.*;
import java.lang.*;
import java.io.*;

public class gradecalculator{
    public static void main (String[] args){
        int runAgain = 1;
        while (runAgain == 1){
            System.out.println("**Minimum Grade Calculator**");
            Scanner q1 = new Scanner(System.in);
            System.out.print("Enter Your Q1 Report Card Grade_ ");
            double myQ1Grade = q1.nextDouble();
            Scanner pr4 = new Scanner(System.in);
            System.out.print("Enter Your PR4 Report Card Grade_ ");
            double myPR4Grade = pr4.nextDouble();
            double c = ((69.5 - ((myQ1Grade * 0.45) + (myPR4Grade * 0.45)))/0.10);
            double b = ((79.5 - ((myQ1Grade * 0.45) + (myPR4Grade * 0.45)))/0.10);
            double a = ((89.5 - ((myQ1Grade * 0.45) + (myPR4Grade * 0.45)))/0.10);
            System.out.println("With a Q1 grade of " + myQ1Grade + " and PR4 grade of " + myPR4Grade + ":");
            System.out.println("");
            System.out.println("To score a C, you will need a minimum of a " + c + " on the semester exam.");
            System.out.println("To score a B, you will need a minimum of a " + b + " on the semester exam.");
            System.out.println("To score an A, you will need a minimum of a " + a + " on the semester exam.");
            System.out.println("");
            System.out.println("** NOTE: Negative grades indicate the exam could be skipped");
            System.out.println("** NOTE: Grades over 100 indicate this average cannot be achieved");
            System.out.println("");
            Scanner runItAgain = new Scanner(System.in);
            System.out.println("Would You Like to Perform Another Calculation?");
            System.out.print("1 - Yes  /  0 - No : _ ");
            runAgain = runItAgain.nextInt();
        }
        System.out.println("");
        System.out.println("Program Complete - Exiting");
    }
}

They will be breaking the program into two separate parts. The main method will be handling keyboard inputs, screen outputs, and the while loop. The calculation method will be handling the calculations.

Here is a possible solution to the problem:

//Program Name: OOP Concepts (File 1 of 2)
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Fall 2016

import java.util.*;
import java.lang.*;
import java.io.*;

public class gradecalculator{
    public static void main (String[] args){
        int runAgain = 1;
        while (runAgain == 1){
            System.out.println("**Minimum Grade Calculator**");
            Scanner q1 = new Scanner(System.in);
            System.out.print("Enter Your Q1 Report Card Grade_ ");
            double myQ1Grade = q1.nextDouble();
            Scanner pr4 = new Scanner(System.in);
            System.out.print("Enter Your PR4 Report Card Grade_ ");
            double myPR4Grade = pr4.nextDouble();
            System.out.println("With a Q1 grade of " + myQ1Grade + " and PR4 grade of " + myPR4Grade + ":");
            System.out.println("");
            calculation myGrade = new calculation (myQ1Grade, myPR4Grade);
            System.out.println("To score a C, you will need a minimum of a " + myGrade.c + " on the semester exam.");
            System.out.println("To score a B, you will need a minimum of a " + myGrade.b + " on the semester exam.");
            System.out.println("To score an A, you will need a minimum of a " + myGrade.a + " on the semester exam.");
            System.out.println("");
            System.out.println("** NOTE: Negative grades indicate the exam could be skipped");
            System.out.println("** NOTE: Grades over 100 indicate this average cannot be achieved");
            System.out.println("");
            Scanner runItAgain = new Scanner(System.in);
            System.out.println("Would You Like to Perform Another Calculation?");
            System.out.print("1 - Yes  /  0 - No : _ ");
            runAgain = runItAgain.nextInt();
        }
        System.out.println("");
        System.out.println("Program Complete - Exiting");
    }
}
//Program Name: OOP Concepts (File 2 of 2)
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Fall 2016

public class calculation{
    public double a;
    public double b;
    public double c;
    public calculation (double myQ1Grade, double myPR4Grade)
    {
        a = ((89.5 - ((myQ1Grade * 0.45) + (myPR4Grade * 0.45)))/0.10);
        b = ((79.5 - ((myQ1Grade * 0.45) + (myPR4Grade * 0.45)))/0.10);
        c = ((69.5 - ((myQ1Grade * 0.45) + (myPR4Grade * 0.45)))/0.10);
    }
}

As you can see, the calculation.java file is responsible for the calculations and the gradecalculator.java file is handling all of the other functions of the application.

In reality, we would not necessarily break-up a program like this, but this allows the students the opportunity to see how OOP concepts work on an existing application they have developed.

Leave a Reply

Your email address will not be published. Required fields are marked *