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.

QR Robotics Classification

Upon returning from Thanksgiving break, we’ll be looking at standard classification systems for robotic and automation systems. To start with, we’ll be doing a QR knowledge hunt.

Robot Classification Knowledge Hunt Cards

Classification Knowledge Hunt Card #1
Classification Knowledge Hunt Card #1

The 17 cards are placed on the walls around random parts of the main hallway. Unlike in my larger classes, everyone will be able to start at a set question and will not have to work in groups. Each student must move through all 17 cards in any order they would like as long as there is no unnecessary “congregating” around any single card.

This particular knowledge hunt requires the students to utilize a QR scanner to “read” the question.

They must scan the QR code and then either use their existing knowledge or research skills to answer the question. The answers are then recorded on a provided answer document, which is submitted for a grade.

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.

Semester Grade Calculator

JAVA Programming Language LogoAs we are coming out of Thanksgiving break, I have given my students a “review” project that covers many of the topics that we have covered up to this point in the year.

The goal of the project is to allow for students to enter their Q1 report card grade and their current Q2 in-progress grade and calculate the minimum semester exam grade needed to achieve a final grade of a “C”, a “B”, and an “A”.

The students were given the following “starter” code to work from:

//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();
            //Copy line 15 and modify for PR4 grade
            //Copy line 16 and modify for PR4 grade
            //Copy line 17 and modify for PR4 grade
            double c = ((69.5 - ((myQ1Grade * 0.45) + (myPR4Grade * 0.45)))/0.10);
            //Copy line 21 and modify for a grade of a b
            //Copy line 22 and modify for a grade of an a
            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.");
            //Copy line 26 and modify for a grade of a b
            //Copy line 27 and modify for a grade of an a
            System.out.pringln("");
            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");
    }
}

This particular project requires students to utilize their knowledge of working with integer and double variables, scanner keyboard inputs, Boolean logic operators, and iteration loops.

Following this review, students will receive their first exposure to object oriented programming with a multi-class application and methods.

Following is a possible completed solution for the application:

//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");
    }
}

 

QR Electromagnetism

Upon returning from Thanksgiving Break, we’ll be starting a study of electromagnetism in my Principles of Technology class. To start with, we’ll be doing a QR Knowledge Hunt.

Electromagnetism Knowledge Hunt Cards

The 15 cards are placed on the walls around random parts of the main hallway. The class is broken into 12 teams of 2 and each team is assigned a starting number. Teams must move through all 15 cards in any order they would like as long as there is no unnecessary “congregating” around any single card.

Electromagnetism Knowledge Hunt Card #1
Electromagnetism Knowledge Hunt Card #1

This particular knowledge hunt requires the students to utilize a QR scanner to “read” the question.

They must scan the QR code and then either use their existing knowledge or research skills to answer the question. The answers are then recorded on a provided answer document, which is submitted for a grade from each team. Only one answer document is needed from each team as long as both team members names are on the answer document.

Here are the questions they are having to answer (in no particular order):

  1. In electromagnetism, electric current is measured using which SI unit?
  2. In electromagnetism, what does the SI unit of a coulomb measure?
  3. In electromagnetism, what does the SI unit of a farad measure?
  4. In electromagnetism, what does the SI unit of a tesla measure?
  5. In electromagnetism, what does the SI unit of a volt measure?
  6. In electromagnetism, what does the SI unit of a watt measure?
  7. In electromagnetism, what does the SI unit of an ohm measure?
  8. Nikola Tesla is best known for his work on what?
  9. The electromagnetic force is one of the 4 forces of nature.
  10. What are the names of the poles of a magnet?
  11. What electrical charge discovery is Thomas Edison credited with that was the “opposite” of the work of Nikola Tesla?
  12. What happens when identical poles of a magnet are placed near each other?
  13. What happens when opposite poles of a magnet are placed near each other?
  14. Who is credited with the discovery of induction (production of an electromotive force (ie voltage) as a result of the interaction between two magnetic fields)?
  15. Who is the French mathematician and physicist, considered to be the father of electrodynamics?

Following the delivery of the exercise, I will be posting an answer key to this exercise.

Trebuchet Assault

Recently, in Principles of Technology we wrapped-up motion and acceleration with a project involving construction of a small-scale trebuchet made of popsicle sticks, rubber bands, and any other materials that could be secured to defend an assigned army.

Soldiers Guarding the Trebuchet
Soldiers Guarding the Trebuchet

We competed in a bracket elimination system. Each team started with their trebuchet and 20 soldiers. Each team took one turn shooting at the other. Any soldiers who are hit were removed from play.

At least 2 soldiers were required to be standing to fire the trebuchet. Students asked during the competition if they could use their “dead” soldiers as ammunition. I agreed and it turned out to be surprisingly effective.

If the trebuchet itself was hit, it was unable to return fire for one round to undergo “repairs”. At least 5 soldiers were required to complete repairs to the trebuchet.

If a team dropped below the minimum number of required soldiers for their next move, they were eliminated.

The students found the entire game very interesting and understood the physics behind all of it.

For a final battle, we broke everyone into 2 large groups and fought against each other in a rapid open-fire exercise.

Slipping Pinion Gear

We are using the AM-2964 NeveRest motors on our FTC robot this year. They have worked great for us until yesterday. One of our drive motors would only work going forward. Upon further examination, we realized that the motor would freely spin in reverse and would engage with the motor when going forward.

NeveRest AM-2964 with Gearbox Removed
NeveRest AM-2964 with Gearbox Removed

When the motor was stripped down, the pinion gear was found to be detached from the drive axle. The pinion gear only mounts to the axle in one direction. There is no flat surface or groove on the drive axle and no set screw on the pinion gear. It appears the the pinion gear on these motors is compression mounted on the drive axle.

Following the failure of this motor, we wrote it off as an isolated event. We mounted a new motor onto the robot and resumed testing. During the testing, our other drive motor suffered an identical failure. While the first motor that failed had nearly 20 hours of drive-time on it, the other motor had less than 5 minutes on it!

Short of using Loctite, I’m at a loss on how to secure this pinion gear to the drive axle. Has anyone else has this issue with the NeveRest motors from AndyMark? If so, how did you resolve it?

Almost Ready to Start!

We’re almost ready to start our robotics competition season. This Saturday, our first and only scrimmage of the season is set to be held out in Kaufman, TX.

Our season is full of 3 different league meets followed by a league championship and then a regional qualifier. All of these events carry us through 11-February-2017!

The regional qualifier is the first elimination event where our season can come to an end. If we advance, the next level will be the regional competition (Texas) which is then followed by the super-regional competition (11 southern states).

Most first-year coaches who have advanced to that level have compared it to that of a bug and a windshield. They have all said that the level of competition is so much greater than that at lower levels.

The final level of the contest is the world championship. Given that we are a rookie team, the statistics are not stacked in our favor for us to advance to this level. However, we will be pushing as hard as we can for as long as we can for the entire season.

Odd or Even Number

I recently gave a single-class project to my Computer Science I students with the following guidelines:

  • The user is asked to enter a valid integer between -2,147,483,647 and 2,147,483,647
  • The program will then clearly output whether the entered number is an odd number or an even number
  • Following the calculation, the user will be asked if they want to run the program again or exit

The odd/even calculation can be run similar to the following:

//Program Name: Blue Pelican Java Lesson 09 (Project)
//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 project9
{
    public static void main (String[] args){
        Scanner numberInput = new Scanner(System.in);
        System.out.print("Enter an integer up to 2,147,483,647: _ ");
        int numberOddEven = numberInput.nextInt();
        double check = numberOddEven % 2;
        if (check == 0){
            System.out.println("The integer " + numberOddEven + " is even");
        }else{
            System.out.println("The integer " + numberOddEven + " is odd");
        }
    }
}

This will run the calculation only once. It is using a conditional statement based upon modulus division. If the number entered is an even number, when it is divided by 2 the modulus (remainder) is 0. If the number entered is an odd number, when it is divided by 2, the modulus (remainder) is 1. From here, this is a simple boolean statement (line 18).

Now, we’ll look at creating the loop:

//Program Name: Blue Pelican Java Lesson 09 (Project)
//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 project9
{
    public static void main (String[] args){
        int runAgain = 1;
        while (runAgain == 1){
            //* * * * * * * * * * * * * * * * * * * * *
            // LINES 13 TO 21 FROM ABOVE BLOCK OF CODE
            //* * * * * * * * * * * * * * * * * * * * *
            Scanner runItAgain = new Scanner(System.in);
            System.out.println("");
            System.out.println("Do You Want to Run Again?");
            System.out.println("1 - Yes, Run Again");
            System.out.println("2 - No, Exit Program");
            runAgain = runItAgain.nextInt();
        }
        System.out.println("Exiting Program. Thank you.");
    }
}

As you can see, we chose to employ a while loop. The initial variable was declared on line 13, which allows us to enter the loop on line 14.

Lines 18 through 22 are asking the user is the want to run the program again. Line 23 assigns the value that was entered to the “runAgain” variable. If the value entered is 1, the while loop runs again. If it is anything other than 1, the loop is exited and line 25 runs.

The final program could look similar to the following:

//Program Name: Blue Pelican Java Lesson 09 (Project)
//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 project9
{
    public static void main (String[] args){
        int runAgain = 1;
        while (runAgain == 1){
            Scanner numberInput = new Scanner(System.in);
            System.out.print("Enter an integer up to 2,147,483,647: _ ");
            int numberOddEven = numberInput.nextInt();
            double check = numberOddEven % 2;
            if (check == 0){
                System.out.println("The integer " + numberOddEven + " is even");
            }else{
                System.out.println("The integer " + numberOddEven + " is odd");
            }
            Scanner runItAgain = new Scanner(System.in);
            System.out.println("");
            System.out.println("Do You Want to Run Again?");
            System.out.println("1 - Yes, Run Again");
            System.out.println("2 - No, Exit Program");
            runAgain = runItAgain.nextInt();
        }
        System.out.println("Exiting Program. Thank you.");
    }
}

 

Making Progress on Robot

We’re making progress on our robot! Speaking as a teacher, it was great seeing the look on my students’ faces when the robot jumped to life.

Our first scrimmage meet of the season is set for Saturday, 19-November-2016 at 8:00am at Kaufman High School.

While it may not yet be much to look at, it is driving in both a teleoperated and autonomous mode! Now, we can get to our actual build.

Our robot has made it for almost 8 hours of off/on driving over 3 school days on a single battery charge. During that same time, we dropped only one set screw and that was early on in the testing. Once it was tightened down, it has held with no problems.

We have had a few issues with the OTG cable between the core distribution module and the on-board phone. We’re in the process of replacing that with a micro-USB (male) to full-USB (female) that will then mate with a full-USB (male) to mini-USB (male). This will allow us to interface with the phone only having to interact with a full-size USB which we hope is more robust than the micro-USB we are working with now.