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.

Iterations Exam 1

In Computer Science I, we have now completed our first exam over iterations. This was a high-level look at basic iterative structures such as:

  • for loops
  • while loops
  • nested while loops
  • do while loops

The test was administered in a single 90-minute class and then reviewed for corrections which were submitted during the next class session.

Computer Science Unit 2 Test Grade Distribution
Computer Science Unit 2 Test Grade Distribution

The raw (uncurved) exam grades came in with a MEDIAN of 53% and a MEAN of 60.4%. The curved exam grades had a final MEDIAN of 74.05% and a MEAN of 77.8%.

Following validation of the 25 questions, it was determined that only 1 question (#6) was a bad question and was discarded. The remaining 24 questions were considered valid and correct.