Transcript L5
Intro. To Object Oriented
Design
What is an object oriented design?
Object oriented design (OOD) is about creating modules/objects that
contain data and methods that can exist independent of whatever else
the program is doing.
Object oriented programming (OOP) is about using/creating objects
and using these objects rather than a structured/linear design.
Structure/Linear programming
void main()
{
1.
2.
3.
4.
5.
Step 1
Step 2
Step 3
Step 4
Step 5
}
This method has very little code reusability. All code is located in a singular
construct that follows steps. Modifying one step can affect another step
greatly.
OOP
void main()
{
1.
2.
3.
4.
5.
Call object 1 to accomplish a task
Call object 2 to accomplish a task
Call object 3 to accomplish a task
Call object 2 to accomplish a task
Call object 1 to accomplish a task
}
In OOP, objects are independent from one another. You can modify one
object without affecting the other ones. The codes also has tremendous
reusability. You can repeatedly call objects over and over again without
rewriting code.
What is an Object?
An object in Java is an abstract collection of data and methods.
Object X
{
// has data
// has methods
}
We can call this object as many times as we want to perform a certain task.
What is an Object?
What is data encapsulation?
Object X
{
// has data
// has methods
}
Data encapsulation is the collection of different data types within one object that acts in unison to
perform a particular task. The data inside Object X cannot be modified by outside sources. It can
only be modified within its own body.
Data encapsulation is one of four building blocks of OOP.
What is an Object?
What is information hiding?
Object X
{
// has data
// has methods
}
By encapsulating the data within an object, you are hiding this information
from your users.
Online Demo
Review
OOP is the idea that parts of the program can be broken down to
smaller objects with each object accomplishing a particular task and
being independent of each other.
• OOP is superior to linear design because of the following properties:
• Shareable Code
• Superior formal structure
• Data encapsulation provides data integrity as data inside an object cannot be
modified by outside forces.
• Information hiding provides security. This is linked to data encapsulation.
Declaring an Object
Reserved word class / name of your object
class myobject
{
int x;
double y;
String s;
}
Objects need to be declared outside of your main class method
class myobject
{
int x;
double y;
String s;
}
public class filename
{
public static void main(String [ ] args)
{
}
}
Declaring and accessing objects
class myobject
{
public int x;
public double y;
public String s;
}
public static void main(String [ ] args)
{
myobject name = new myobject();
name.x = 10;
name.y = 10.5;
name.s = “Hello World”;
}
Exercise 1
Create an object called SportsTeam. SportsTeam is to have five
variables:
•
•
•
•
•
String TeamName
String TeamMascot
String city
int Wins
int Losses
In void main, declare an instance of SportsTeam and ask user for input
for those values. Assign these values to their respective data types.
Now consider this:
class myobject
{
public int x;
public double y;
public String s;
}
class myobject
{
private int x;
private double y;
private String s;
}
Private vs Public
A public variable can be manipulated by other forces outside the
object. Basically, its any method anywhere in the program can access
and modify that variable.
A private variable can only be manipulated by the object itself. The
object must write in methods and functions that access and change
these private variables.
Example
class myobject
{
private int x;
private double y;
}
public static void main(String [ ] args)
{
myobject name = new myobject();
name.x = 10; // this is illegal
name.y = 10.5; // this is illegal
}
For the most part, an object’s variables need to remain private as much
as possible. It is considered bad programming practice to leave too
many variable public, since they can be easily modified.
So how do we change a private variable’s values?
The object must have methods that do so.
class myobject
{
private int x;
private double y;
void method1()
{
x = 10;
y = 10.5;
}
}
public static void main(String [ ] args)
{
myobject name = new myobject();
name.method1();
}
Exercise 2
Declare a class called Teachers. Create 3 variables inside Teachers:
• private String name
• private int ID
• private String subject
Now create a method called AssignValues. AssignValues will ask for values to
all private data declared and assign the input to those values.
Now create a method called DisplayValues. DisplayValues will display all the
values for that teacher.
Create an instance of this class in void main and call your methods.
Exercise 3
Declare a class called Teachers. Create 3 variables inside Students:
• private String[ ] classes = new String[5]
• private int ID
• private String name
Create a method that will ask for the student name and ID and set those values.
Now create a method called SetClasses. SetClasses will ask for five classes taken by this student and
set them equal to that array.
Now create a method called DisplayValues. DisplayValues will display all the values for that student.
Create an instance of this class in void main and call your methods.
Exercise 4
Declare a class called Animals with the following data types:
•
•
•
•
private String bird
private String mammal
private String reptile
private String fish
Now create a method called AssignValues. AssignValues will ask for values to all
private data declared and assign the input to those values.
Now create a method called DisplayValues. DisplayValues will ask which animal
value is to be shown (bird, mammal, reptile, fish) and show the appropriate values..
Create an instance of this class in void main and call your methods.
Exercise 5
Create an object called myArray. It is to have one private integer array of size
20 as its data.
Create a method called makeArray. This array is to initialize all values of the
array created from 0-100 random values.
Create a method called displayArray. This method will display the values of
this array.
In void main, declare an instance of myArray, then call makeArray and then
call displayArray.