Quick Hands-On Primer to OOP in JAVA

JAVA Programming Language LogoAs a quick hands-on primer to object oriented programming in JAVA, I am going to work with my students to create a basic program that asks the user for their first name and then to select a language they want to be greeted in. The program will then produce the appropriate greeting in the language selected.

In this primer, we will be working with two files (primary.java and secondary.java). The primary.java file will contain our main class of the application while the secondary.java file will be handling all of our active “processing”.

//Program Name: OOP Primer (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 primary{
    public static void main (String[] args){
        System.out.println("** Greetings Generator **");
        Scanner firstName = new Scanner(System.in);
        System.out.print("What Is Your First Name?_ ");
        String userFirstName = firstName.next();
        System.out.println("Thank you, " + userFirstName);
        Scanner language = new Scanner(System.in);
        System.out.println("Select the Language for Your Greeting");
        System.out.println("1 - English");
        System.out.println("2 - Spanish");
        System.out.println("3 - German");
        System.out.println("4 - Pig Latin");
        System.out.print("Language Selection: _ ");
        int greetingLanguage = language.nextInt();
        System.out.println("");
        secondary myGreeting = new secondary (userFirstName, greetingLanguage);
        System.out.println(myGreeting.greeting);
    }
}

In lines 13 – 16, we are working with receiving the first name of the user.

In lines 17 – 25, we are working with receiving the language the user would like to be greeted in.

Line 26 is the one line that confuses my students to most. This is the heart of Object Oriented Programming. This line is creating a new object named “myGreeting” which is based upon the variables userFirstName and greetingLanguage that is processed in the secondary.java file.

Line 27 is the output of a method we created in the secondary.java file named “greeting”. You can see what that method is by reading the secondary.java file.

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

public class secondary{
    public String greeting;
    public secondary (String userFirstName, int greetingLanguage)
    {
        switch (greetingLanguage) {
            case 1:
                greeting = "Hello " + userFirstName;
                break;
            case 2:
                greeting = "Hola " + userFirstName;
                break;
            case 3:
                greeting = "Hallo " + userFirstName;
                break;
            case 4:
                greeting = "Ellohay " + userFirstName;
                break;
            default:
                greeting = "Invalid Selection";
                break;
        }
    }
}

In line 7, we created a public string variable named “greeting”. A public variable is one that can be passed between files within the application. Think of it as a “global” variable.

In line 8, we are establishing which variables we should be receiving from any files. In this case, that is the string variable “userFirstName” and the integer variable “greetingLanguage”.

Lines 11 – 26 utilize a switch for a conditional statement based on the integer value of “greetingLanguage” variable. In those lines, you can see that we declare the value of the variable “greeting” for each case. This is the method that is called-up in line 27 of the primary.java file.

For the record, I require my students to encase all of the applications within a loop to ask the user if they wish to run the application again or to exit. As this requirement would only impact the primary.java file, here is the completed code for that file:

//Program Name: OOP Primer (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 primary{
    public static void main (String[] args){
        int runAgain = 1;
        while (runAgain == 1){
            System.out.println("** Greetings Generator **");
            Scanner firstName = new Scanner(System.in);
            System.out.print("What Is Your First Name?_ ");
            String userFirstName = firstName.next();
            System.out.println("Thank you, " + userFirstName);
            Scanner language = new Scanner(System.in);
            System.out.println("Select the Language for Your Greeting");
            System.out.println("1 - English");
            System.out.println("2 - Spanish");
            System.out.println("3 - German");
            System.out.println("4 - Pig Latin");
            System.out.print("Language Selection: _ ");
            int greetingLanguage = language.nextInt();
            System.out.println("");
            secondary myGreeting = new secondary (userFirstName, greetingLanguage);
            System.out.println(myGreeting.greeting);
            System.out.println("");
            Scanner runItAgain = new Scanner(System.in);
            System.out.println("Would You Like to Run Again");
            System.out.print("1 - Yes  /  0 - No : _ ");
            runAgain = runItAgain.nextInt();
        }
        System.out.println("");
        System.out.println("Program Complete - Exiting");
    }
}

As you can see, the code to run the application again, adds lines 12 to 13 & lines 30 to 37.

Following is brief video of what the output should look like with the program compiled and executed.

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.