Section 3 - JAVA Programming The Objected Oriented Approach
Section 3 - JAVA Programming The Objected Oriented Approach
Instead of focusing on
writing an algorithm of
TCFL Student: object
how to score the ball
Properties: has ID No, has Name,
(problem oriented
has class, has surname
approach)
Bevaviour: can submit homework,
Just define (create)
can write test, can register, can
Christiano Ronaldo
deregister (exit)
virtually (in software) –
an object that is able to
play soccer and score
the ball (object
oriented approach)
JAVA OBJECT ORIENTED APPROACH
• In Java object oriented approach we programme by creating objects (a purely
object oriented programming approach)
• First step: represent your object in terms of data and methods required to define
its characteristic behaviour and group is as class behaviour for similar objects
giving it a name like students or triangles as shown below
• We use the word class because the behaviour and attributes we define fit “Jane”,
“John”, “Melissa”, “Tatenda” etc (i.e. it is common behavour and data
requirements shared by any entity classified as a “student”)
• “Jane” or “Tatenda” is called an instance (example of a student) of an object
Class of object Name: Student
Data: has ID No, has Name, has
class, has surname, has date of Class of object Name: Triangle
birth, has test marks Data: has side1, has side2, has side3,
Methods: can log in, can answer has Angle1, has Angle2, has Angle3
questions, can register, can Methods: can find area, find type
determine age, can take find average
mark
OBJECT Diagrammatical representation using
Unified Modeling Language (UML) notation
• The illustration of class templates and objects can be standardized
using Unified Modeling Language (UML) notation.
• This notation, as shown on the next slide, is called a UML class
diagram, or simply a class diagram.
• In the class diagram, the data field is denoted as
• dataFieldName: dataFieldType
• The constructor is denoted as
• ClassName(parameterName: parameterType)
JAVA OBJECT STUDENT
• In Java object oriented approach we programme by creating objects
(a purely object oriented programming approach)
Student Class name
Name: string
Surname: String
ID_No: integer
DOB: Date
Data fields ‐ data variables belonging to a
Marks: Array of integers general object of type student
StuClass: String
testScore: integer Constructors – these are programmes that are invoked
Student () to create instances of students
Student (stuName:string, stuSurname: string, e.g. to create Jane, Moyo, 0712, 12/07/84, TTT1_2019
stuID: integer, stuDOB: Date, stuClass: string)
setMarks(): vooid Methods
setInitialMarks(): void
Mutators – programmes that change or update
getAverage() : double
getAge() : integer the modifiable or updatable data of the object
getMarkList(): void Accessors – programmes that retrieve data
belonging to an object
Notes about Constructors
• Constructors have same name as the class name
• Constructors create instances of an object
• An instance of an object is one particular sample of an object e.g. student Jane with name,
surname, DOB, student ID … all filled up with actual values for one particular student
creating one instance of a student,
• we can create many instances of students by filling up values for particulars of say Tatenda
and others and saving each separately (creating a 2nd, 3rd , 4th etc instances of a student)
• Some times you can create a student without knowing some of the values, these values you
want to be added latter when the student registers, thus you can have many types of
constructors (maforoma ezvitina aka wanda),
• In our example we chose to have two constructors
• Student () ‐ this one if chosen creates a student without filling in name, surname , dob , etc for later use
• Student (stuName:string, stuSurname: string, stuID: integer, stuDOB: Date, stuClass: string) – this one if
selected fills most of the fields though it leaves out marks, which may be filled in later
• The two constructors have the same name but use different input data inside the ‐‐‐>()
• They construct two kinds of students (1) one student can be created using constructor 1 –
and will be created without name or any other data (name and other data fields are “”)
(2)while second constructor when chosen will create a student with the other details filled in
such as name, surname etc this is called polymorphism (many forms of the same object)
Programming using OOP: Student Registration
• First step we show steps in opening up Netbeans programme
• NETBEANS is one of the popular JAVA programme development
environment
Click to open “Netbeans”
Click Next
Choose Java application
Click Finish
Main Java class opened
Click Next
Type the class name
Finish
class TCFL_student
opened
0 1 2 3 4 5 6 7 8 9
Mark1 Mark2 Mark3 Mark4 Mark5 Mark6 Mark7 Mark8 Mark9 Mark10
Student ()
Student (stuName:string, stuSurname: string, stuID: integer, stuDOB:
• Next we look at creating the
Date, stuClass: string) Constructors (maforoma ka
awa a no creator objects)
setMarks(): vooid
setInitialMarks(): void
getAverage() : double
getAge() : integer
getMarkList(): void
The constructor that creates an object of type
TCFL_Student and loads data to the variables as
shown (all blanks) as data may not yet be known
for example if a student is to log in later to enter
his/her details
Student ()
Student (stuName:string, stuSurname: string, stuID: integer, stuDOB: • The two constructors
Date, stuClass: string) have been created
setMarks(): vooid
setInitialMarks(): void
• Next we look at creating the
codes for mutators (change
getAverage() : double or sets data values)
getAge() : integer
getMarkList(): void
This mutator sets or enters marks into the array
Marks, given the test number the student want to
save in the array
e.g. setMark (76, 3)
0 1 2 3 4 5 6 7 8 9
Mark1 Mark2 Mark3 76 Mark5 Mark6 Mark7 Mark8 Mark9 Mark10
Student ()
Student (stuName:string, stuSurname: string, stuID: integer, stuDOB: • The two constructors
Date, stuClass: string) have been created
setMarks(): vooid • The two mutators have
setInitialMarks(): void
been completed above
getAverage() : double
getAge() : integer • Next we look at creating the
getMarkList(): void codes for accessors (the read
data values from variables)
This accessor accesses or gets the
age of the student using date
subtraction function provided by the
import java.time.*;
• This class has so far no link with the outside word and its data
member (the variables are hidden inside it) thus they are said to be
enclosed or encapsulated
• We have created methods that could access or mutates the values.
• However these methods need to be called or invoked to run.
• We will now create the main programme to invoke or call for the
creation of instances of objects and call for invocation of the methods
The Main class
• By loading these values entered by user upon calling the Constructor we are
choosing a constructor that accepts input value i.e. constructor no II. Thus
constructor 2 will receive these values and create an object and pass the
• The • name of reference (address) to this new instance of an object and it will be passed on to
variable to the student1 variable
stores the • It is this call to the constructor that runs code in the other class under
The name of the reference • A keyword construct constructor 2.
class we created or address used to or found
before (used to of the create a in calss • After this there is a new instance of an object referred to as student1
depict that the reference TCFL_Stud • Student1 has now properties assigned to by this constructor code i.e.
data type object of an ent studentname , Surname, studentID etc. And sets Marks to ‐1 all values
referenced to object from
here an object of a class
type that is
defined in
TCFL_Student
class
0 1 2 3 4 5 6 7 8 9
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
The Main class
• Print line – command prints a message on the
console or screen, asking user to input his/her
name
• Scan line command reads a value from the
console typed in by the user (student) and
puts it in variable StudentName
{
Break
}
}
Switch
Executed when the user choose ‘y’
statement
Based on the Test number that needs to be recorded (remember test results are to be stored in Array marks which
has 10 positions running from 0 to 9). Thus test No is any value between 1 to 10 or in array list 0 to 9). Thus the user
chooses the test number that he wants to update the mark
switch (testNo)
{
case 0: User choose not to enter data
break;
case 1: User choose to save the Course work Mark on
student1.setMark(testScore, 0); position 0 in the array ( the 1st mark according to
break; him/her but position 0 according to array
case 2: numbering)
student1.setMark(testScore, 1);
break;
………
case 10:
student1.setMark(testScore, 9);
break; If user enters any other value besides thenumbers
default: give e.g. entering a letter instead of a number
break;
}
Outputs
Executed when the user choose ‘y’
of main programme