step 1 step 2 step 3 step 4 step 5 step 6 Task

Object Oriented Programming

modeling the real world with computers
OOP

Modeling the real world with computer programs

man

Check out the cool student. He looks pretty cool. He has assets eg:

  1. a hat
  2. a moustache
  3. a name
  4. an age
  5. a gpa

and behaviours eg:

  1. he can talk
  2. he can chew gum
  3. he can give us information

Imagine if we could model this student in a computer program... if only we could define his assets and his behaviour. If only we could change his assets and his behaviour. I guess he doesn't sleep wearing sunglasses!

We can - using the magic of Object Oriented Programming.


The Student class

designing a family of code to define the funky student

Step 1 - Create a class file

We are going to model the student by designing a new class to describe his assets and behaviour.

First we need to decide on the name of the class file. I am going to call mine Student

public class Student{

//definition code will go here

}

Step 2 - Determine class variables

Next we will consider what data our digital object will store when it is constructed by a computer program. What is his name? How old is he? What is his GPA?

public class Student{

//information about assets will go here. Declare class variables

String name;

int age;

double gpa;

}

Step 3 - Design accessor methods

Next, let's write some methods that will allow a computer program to access the data stored in the object. Not every computer programming language requires this. But Java does!

Methods that allow us to access data stored in a digital object are called accessor methods.

public class Student{

//information about assets will go here. Declare class variables

String name;

int age;

double gpa;


//define methods to allow access to the data stored in the class variables

public String getName(){

return name;

}


public int getAge(){

return age;

}


public boolean getGPA(){

return gpa;

}


}


Step 4 - Design mutator methods

We also need some methods that will allow a computer program to change the data stored in a digital object. For example, the student won't always be 19 years old and I am guessing his gpa will fluctuate over time.

Methods that allow us to change the data stored in an object's variables are called mutator methods

public class Student{

//information about assets will go here. Declare class variables

String name;

int age;

double gpa;


//accessor methods wil go here

public String getName(){

return name;

}


public int getAge(){

return age;

}


public boolean getGPA(){

return gpa;

}



//define methods to allow data stored in the class variables to be changed

public void setName(String newName){

name = newName;

}


public void setAge(int newAge){

age = newAge;

}


public void setGPA(double newGPA){

gpa = newGPA;

}



}


WOW!

We did it! We have just defined what a Student object would store (class variables) in a computer program. We have also defined a few behaviours (methods).

But where is our computer program - oh here it is!

man

public class myProgram{

public static void main(String[ ] args){

System.out.println("Hello World!");

}

}



Oh - great! But where is our digital object of a Student?!

We just didn't construct it yet. Remember how we can construct a new String object:

String name = new String("Jackson Ramires");


The reference variable name references a String object which stores some data, Jackson Ramires.

So, we should be able to do this:

public class myProgram{

public static void main(String[ ] args){

Student bill = new Student("Bill Jones", 19, 3.2);

}

}

where Bill Jones is his name, 21 is his age, and 3.2 is his gpa.

Importantly, bill is a reference variable to the digital object that exists in computer memory when the program runs.

Step 5 - Design constructor method(s)

WAIT A MOMENT!

Something is missing. I know that we are passing data into the object we construct it. That's what this code does:

Student bill = new Student("Bill Jones", 19, 3.2);

But in our definition class file we never described how the data would be stored! We are calling a method called Student. Note, this has the same name as the data type. We are passing 3 parameters into the method for processing:

  • a reference to a String object
  • an integer
  • a double

How does this data get processed and/or stored in the object?

We need to define a constructor method in the Student class file:

public class Student{

//information about assets will go here. Declare class variables

String name;

int age;

double gpa;


//define constructor method(s)

public FunkyMan(String name, int age, double gpa){

this.name = name;

this.age = age;

this.gpa = gpa;

}


//define accessor methods


//define mutator methods

}


So there it is, the Student class. It defines Student objects. Think of it as a recipe. It contains the instructions for how to construct and store data in a Student object, how to access the data and how to change/mutate the data.

In a computer program we can now construct 1 or more Student objects and then mess around with the data stored in the objects!

Student bill = new Student("Bill", 18, 3.2);

Luv ♥ hi! Whassup?! Coffee? bill Student(String, int, double) String getName() int getAge() void setGPA(double newGPA) name "Bill" length() substring(int) equals() age 18 3.2 gpa

Step 6 - a computer program - The Salutatorian

Students in a school's database are stored as digital objects. There are only 2 students at the school and they are arguing about who will be the salutatorian at the graduation ceremony next week.

This computer program will determine the answer and end the argument!

Note: when a Student is first constructed, the gpa is set to 0.

public static void main(String[ ] args){

Scanner kbin = new Scanner(System.in);

//construct 2 Student objects

Student bill = new Student("Bill Nguyen", 18, 0);

Student betty = new Student("Betty Murtle", 18, 0);


//declare and initialise 2 variables to store credit hour and grade point data

int totalCreditHours = 0;

int totalGradePoints = 0;


//a loop to allow the school secretary to input credit hours and grade points for the 5 courses Bill studied.

for(int i = 0; i<5; i++){

totalCreditHours += kbin.nextInt();

totalGradePoints += kbin.nextInt();

}


//use that data to update Bill's GPA. A Student class method, updateGPA, will do this.

bill.updateGPA(totalCreditHours, totalGradePoints);


//reset totalCreditHours and totalGradePoints for Betty

totalCreditHours = 0;

totalGradePoints = 0;


//a loop to allow the school secretary to input credit hours and grade points for the 5 courses Betty studied.

for(int i = 0; i<5; i++){

totalCreditHours += kbin.nextInt();

totalGradePoints += kbin.nextInt();

}


//now we will update Betty's GPA

betty.updateGPA(totalCreditHours, totalGradePoints);


//finally we can determine who has the higher GPA:

if(bill.getGPA( ) > betty.getGPA( ))

System.out.println(bill.getName( ) + " will be the salutatorian!");

else if(betty.getGPA( ) > bill.getGPA ())

System.out.println(betty.getName( ) + " will be the salutatorian!");

else

System.out.println(betty.getName( ) + " and " + bill.getName( ) + " will both be salutatorians!");

}


   Task

In your IDE eg replit, create a new file called Student.java

Paste the definition (class) code for Student objects - you can find it here.

The code above shows us that the Student class has a method called updateGPA. The signature is:

public void updateGPA(int totalCreditHours, int totalGradePoints);

This method will update a Student object's GPA by using this simple formula:

gpa = the total credit hours divided by the total grade points achieved.

Implement (write the code/solve) this method in the Student class.

Test your solution using the main program code above.

Welcome to the hard stuff

OOP may be very unfamiliar to you. This will make it seem very difficult.

Experience will help you.

Struggling will help you.

Asking questions will help you.

Developing your English skills will help you!

Understanding the Data Type you are working with will help you - eg Student, Elephant[], ArrayList Hammer, String...