0% found this document useful (0 votes)
11 views

Section 3 - JAVA Programming The Objected Oriented Approach

The document discusses object oriented programming in Java. It explains some key concepts like classes, objects, properties, behaviors, and how to represent classes and objects using UML diagrams. It also provides an example of creating a student registration system in Java using object oriented principles.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Section 3 - JAVA Programming The Objected Oriented Approach

The document discusses object oriented programming in Java. It explains some key concepts like classes, objects, properties, behaviors, and how to represent classes and objects using UML diagrams. It also provides an example of creating a student registration system in Java using object oriented principles.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Section 3 ‐ JAVA programming

The objected oriented approach

Eng. glmoyo TelOne Centre for Learning


Object orientation of Java
• Long time before object oriented programming – the design Start Find Average
of programmes was centered around a problem e.g find
average would be focused on the formular Get Num

• Average = Total sum/ number of items Total = Total +Num

• A flow chart would be drawn to solve this problem


• This approach is called problem centred approach Counter = Counter + 1

• It has a disadvantage of failing to solve very complex No


End?
problems for example you can draw a flowchart or
algorithm for a problem like: Yes
Average = Total/Counter
• Design of the TelOne Centre for learning online learning
system (https://tcflonline.ac.zw/) Display Total, count
and Average
• It is way too complicated to be defined by flow charts
Stop
Problem solving – through objects
• Programme designers when they encountered such complex problem
started thinking ‐‐‐ but in the real world there so many complex
problems that naturally seem to be solved easily, why?
• This is because the designer of the world the Creator did not think
about a problems but rather about creating many objects or things
(living things and non‐living things)
• The created things (objects) then are used to solve problems for
example there is no algorithm for farming – but rather the existing
objects such as human beings and animals, such as cattle and objects
such as ploughs are organised together to till the land and plant to
solve “production of crops problem”
Merits of object oriented approach
• Starting with natural world Objects, such as claw hammers and knives
– it is easier to see that objects oriented design creates things or
objects that can easily go beyond solving only one problem
• A knife for instance can be used for cutting but in a desperate
situation one can use the knife for removing a screw or opening a
bottle top or as a lever to lift or flip a blocking valve or as a weapon
in self defence – things which it was not designed with them in mind.
• In software object oriented approach – the designer puts the thrust
on designing objects such a virtual(software) “Christian Ronald” – in
the FIFA video soccer game young people play
• This is also true with a virtual (software) Ferreira in the virtual car
racing game
Objects
Christian Ronaldo: object
Ferreira car: object
Properties: height, skin and hair
Properties: shape, colour
colour
Bevaviour: can move, can
Bevaviour: can walk, can run, can
accelerate, can overtake, can speed
dribble, can shoot penalty

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”

Search for “Netbeans”


Click Open project
Choose Java application

Click Next
Choose Java application

Click Finish
Main Java class opened

Main class name

Name of the programme

You type your


main method
programme here
Choose open new file
Choose Java class

Click Next
Type the class name

Finish
class TCFL_student
opened

Type class code


here
Add pre‐written header files that assist in handling dates and time
command

Data for a student object

Marks[10] with ten positions to store Position index


integers

0 1 2 3 4 5 6 7 8 9
Mark1 Mark2 Mark3 Mark4 Mark5 Mark6 Mark7 Mark8 Mark9 Mark10

Actual mark data will


This is how you declare an integer array in Java be stored here e.g.
Here the array data type will be used to store marks 67% stored here
TCFL_Student
Name: string
Surname: String
ID_No: integer
DOB: Date
Marks: Array of integers • These variables have
StuClass: String
testScore: integer been created

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

The second duplicate constructor that creates an


object of type TCFL_Student; differs in that it loads
data to the variables which is provided as
parameters passed to it
TCFL_Student
Name: string
Surname: String
ID_No: integer
DOB: Date
Marks: Array of integers • These variables have
StuClass: String
testScore: integer been created

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

This mutator sets all the data from position from 0 to 9 to be


equal to =‐1. This happens at the beginning only when you
initailise the variable Marks[]. The ‐1 helps to identify
positions that will remain without marks i.e. the ‐1 will
remain unreplaced. so if a position still remain with ‐1 when
marks have been entered it means that test was not done. So
it is excluded from calculating average
0 1 2 3 4 5 6 7 8 9
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
TCFL_Student
Name: string
Surname: String
ID_No: integer
DOB: Date
Marks: Array of integers • These variables have
StuClass: String
testScore: integer been created

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 accessor gets or reads values from


the array list that have been saved by the
student as his/ her mark for tests , 1, 2, 3,
4, …10
j starts from 0 up to 9
0 1 2 3 4 5 6 7 8 9
36 82 66 77 -1 -1 -1 -1 -1 -1

Iterates through, reading and printing theses values


to the screen
This mutator determines the average mark by
reading through the array values summing the
non (‐1) values and dividing by the numbers
counted as marks
0 1 2 3 4 5 6 7 8 9
72 63 88 76 74 -1 -1 -1 -1 -1

The ‐1 values are omitted in calculation of average


because these tests still contain initial ‐1 that was used
to mark positions before the student entered marks. In
this case it means student entered only marks 0‐ to 4 (
i.e. he/she has done five tests only so far)
The design of the main programme class that
acts as interface between the user (or outside
world) and the behind scene object creating
class
So far
• We have defined the members(body parts) of the object creating
class named:
• TCFL_Students.class

• 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

• The main class is named tcfl_studentregistration


• The util contains prewritten code that makes available the commands
for input and outputs e.g. scanner object that scanes values from the
console screen
• The main method is where we type our code to ask the user to input
values and we also call the other classes to run and give us results that
we then use commands to display them. The main method is like the
link or command centre or user interface to the behind scenes classes
that are hidden to the outside word
• These variables are used as temporal
storages for data obtained from the user (the
registering student)
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

• Repeated to read Surname value


from the console and put it into
variable studentSurname

• Reads students ID from screen to


StudentsIDNo variable

• Reads year, , month, and day


of birth into respective
variables as integers
The Main class
• This statement below is the main thing of object oriented programming

• 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

• Repeated to read Surname value


from the console and put it into
variable studentSurname

• Reads students ID from screen to


StudentsIDNo variable

• Reads year, , month, and day


of birth into respective
variables as integers
Forever loop
for (;;) This for loop has no stopping condition hence its called the forever loop (
as it does not exit on its own) – its is helped by the if statement to exit or
{ break out of the for loop as explained below
…do you want to enter or update any marks? enter 'y' for Yes and 'n' for No \n?
if (confirm ==('y') ||(confirm =='Y'))
{
Switch statement block If students wants to enter his her marks he/she
chooses ‘y’
After which the programme comes here
} Other wise if user chooses ‘n” the programme
else comes here and exists (breaks) the for loop

{
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

The main class uses the


object.property notation to access
the members of the instance of an
object and prints them out
The main method references the getAge () accessor and
get average() accessor that in the object class and has been
inherited by the new object student1 object (an instance of
that object)

getMarklist() method is also the attribute of the new instance, student1,


produced or whose properties are copied from the blue print the
TCFL_Student Class. Thus TCFL_Student Class is a blueprint or template for
producing objects of similar kind like a like foroma yezvitina ichi produce
zvitina zvaka fanana kana kutizvinoenderana)

You might also like