Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
COS240 O-O Languages
AUBG, COS dept
Lecture 51
Title:
Thinking in Objects
(to model the world in terms of
separate unique objects/classes)
Reference: COS240 Syllabus
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
1
Lecture Contents:
To
apply class abstraction to develop
software.
To
explore the differences btw procedural
paradigm and object-oriented paradigm.
To
design programs using OO paradigm.
To
design classes that follow the classdesign guidelines.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Motivations
You
already know in general the basics of objectoriented programming from the preceding lectures
that were more or less focused on introducing Java
syntax (1st part) and C# syntax (2nd part).
This lecture will demonstrate how to solve
problems using the object-oriented paradigm.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
3
Class Abstraction and Encapsulation
Class
abstraction means to separate class implementation
from the use of the class.
The creator of the class provides a description of the
class and lets the user know how the class can be used.
The user of the class does not need to know how the
class is implemented. The detail of implementation is
encapsulated and hidden from the user.
Class implementation
is like a black box
hidden from the clients
Class
Class Contract
(Signatures of
public methods and
public constants)
Clients use the
class through the
contract of the class
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
4
Object-Oriented Thinking
There exist various imperative programming practices:
spaghetti code programming
structured /Thinking in Functions/ programming;
object oriented /Thinking in Classes/ programming
Java as well as C# may use to explore all the three practices
mentioned above.
OOP approach has absolute advantages because Classes provide
more flexibility and modularity for building reusable software.
This section illustrates problem solving using different practices.
Comparing the problem solutions, you will mark the differences
between structured programming and OO programming and see the
benefits of developing reusable code using objects and classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
5
Problem 1: Computing Body Mass Index
Body Mass Index (BMI) is a measure of health on
weight. It can be calculated by taking your weight
in kilograms and dividing by the square of your
height in meters. The interpretation of BMI for
people aged below 16 years or older is as follows:
BMI
Interpretation
below 16
16-18
18-24
24-29
29-35
above 35
serious underweight
underweight
normal weight
overweight
seriously overweight
gravely overweight
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
6
Problem: Computing Body Mass Index
Solution
in spaghetti code programming style
– Java source text file: SpaghettiCodeBMI.java
Solution
in structured programming style
– Java source text file: StructuredBMI.java
Solution
in OO style
– Java source text file: ObjectOrientedBMI.java
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
7
Problem: SpaghettiCodeBMI.java
public static void main(String[] args)
double height, weight, bmi;
{
System.out.print("Enter the weight in kgs: ");
weight = console.nextDouble();
System.out.print("Enter the height in meters: "); height = console.nextDouble();
bmi = weight / ( height * height);
}
System.out.print("Your BMI is " + bmi + "\n");
if (bmi < 16)
System.out.println("You are seriously underweight");
else if (bmi < 18)
System.out.println("You are underweight");
else if (bmi < 24)
System.out.println("You are normal weight");
else if (bmi < 29)
System.out.println("You are overweight");
else if (bmi < 35)
System.out.println("You are seriously overweight");
else
System.out.println("You are gravely overweight");
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
8
Problem: Computing Body Mass Index
Solution
in spaghetti code programming style:
Drawback:
– The code cannot be reused in other programs
To
make it reusable switch to structured
programming style:
define a method to compute BMI as follows:
double getBMI(double weight, double height)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
9
Problem: StructuredBMI.java
import java.util.*;
public class StructuredBMI {
static Scanner console = new Scanner(System.in);
public static double getBMI(double paramweight, double paramheight) {
return paramweight / (paramheight * paramheight);
}
public static void main(String[] args) {
double height, weight, bmi;
System.out.print("Enter the weight in kgs: ");
System.out.print("Enter the height in meters: ");
weight = console.nextDouble();
height = console.nextDouble();
bmi = getBMI(weight, height);
System.out.print("Your BMI is " + bmi + "\n");
if (bmi < 16)
System.out.println("You are seriously underweight");
else
if (bmi < 18)
System.out.println("You are underweight");
else
if (bmi < 24)
System.out.println("You are normal weight");
else
if (bmi < 29)
System.out.println("You are overweight");
else
if (bmi < 35)
System.out.println("You are seriously overweight");
else System.out.println("You are gravely overweight");
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
10
Problem: Computing Body Mass Index
Solution in structured programming style:
OK, but new limitations appear: suppose you want to
associate weight/height data with person’s name and
age (day of birth). You need separate variables to
store these values. BUT these values are not tightly
coupled.
The ideal way to couple them is to create an object
that contains them using instance data fields.
You can define a user class BMI (see next slide).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
11
The BMI Class
BMI
The get methods for these data fields are
provided in the class, but omitted in the
UML diagram for brevity.
-name: String
The name of the person.
-age: int
The age of the person.
-weight: double
The weight of the person in pounds.
-height: double
The height of the person in inches.
+BMI(name: String, age: int, weight:
double, height: double)
Creates a BMI object with the specified
name, age, weight, and height.
Creates a BMI object with the specified
name, weight, height, and a default age
20.
+BMI(name: String, weight: double,
height: double)
+getBMI(): double
Returns the BMI
+getStatus(): String
Returns the BMI status (e.g., normal,
overweight, etc.)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
12
Problem:ObjectOrientedBMI.java
class BMI {
private String name;
private int age;
private double height; // in kgs
private double weight; // in meters
public BMI(String name, int age, double weight, double height) {
this.name = name;
this.age = age;
this.weight = weight;
this.height = height;
}
public BMI(String name, double weight, double height) {
this(name, 20, weight, height);
}
public double getBMI() { return weight / ( height * height);
}
public String getStatus() {
double bmi = getBMI();
if (bmi < 16)
return ("You are seriously underweight");
else
if (bmi < 18)
return ("You are underweight");
else
if (bmi < 24)
return ("You are normal weight");
else
if (bmi < 29)
return ("You are overweight");
else
if (bmi < 35)
return ("You are seriously overweight");
else
return ("You are gravely overweight");
}
public String getName() { return name; }
public int getAge() { return age; }
public double getWeight() { return weight; }
public double getHeight() { return height; }
} // end of classLiang,
BMIIntroduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
13
Problem:ObjectOrientedBMI.java
public class ObjectOrientedBMI
{
public static void main(String[] args)
{
BMI bmi1 = new BMI("Stoyan Bonev", 60, 73.5, 1.72);
System.out.println("The BMI for " + bmi1.getName() + " is " +
bmi1.getBMI() + " - " + bmi1.getStatus() );
BMI bmi2 = new BMI("Stoyan Bonev2", 72.0, 1.71);
System.out.println("The BMI for " + bmi2.getName() + " is " +
bmi2.getBMI() + " - " + bmi2.getStatus() );
} // end of main()
} // end of class main
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
14
Advantages of OOP approach
Structured Programming approach focuses
on designing member functions (methods).
OO approach couples data and methods into
objects.
OO approach focuses on objects and
operations on objects.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
15
Advantages of OOP approach
In
structured programming data and operations on
data are separate, and this requires sending data to
methods through parameter passing mechanism
/actual arguments and formal parameters/.
OOP
places data and the operations in an object.
OOP
organizes programs in a way that mirrors the
real world, in which all objects are associated with
both attributes and activities.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
16
Advantages of OOP approach
Using
objects improves SW reusability and makes
programs easier to develop and easier to maintain
Programming
in Java/C# stimulates thinking in
objects
Java/C#
program can be viewed as a collection of
cooperating objects (to discuss in next lecture).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
17
Problem 1: Computing Body Mass Index
Rewrite the Java versions solutions into C#
SpaghettiCodeBMI.java >> SpaghettiCodeBMI.cs
StructuredBMI.java >> StructuredBMI.cs
ObjectOrientedBMI.java>>ObjectOrientedBMI.cs
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
18
Teaching by example
and
Learning by doing
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
19
Designing the Course class
Problem:
to process course information
Each course has name and has students enrolled.
You should be able to add/drop students to/from
the course.
See the UML class diagram
Write the Java source text and test the
functionality of the Course class
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
20
Example: The Course Class
Course
-name: String
The name of the course.
-students: String[]
The students who take the course.
-numberOfStudents: int
The number of students (default: 0).
+Course(name: String)
Creates a Course with the specified name.
+getName(): String
Returns the course name.
+addStudent(student: String): void Adds a new student to the course list.
+getStudents(): String[]
Returns the students for the course.
+getNumberOfStudents(): int
Returns the number of students for the course.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
21
Designing a Class
A class should describe a
single entity, and all the class operations should
(Coherence)
logically fit together to support a coherent purpose.
You can use a class for students, for example,
BUT
You should not combine students and staff in the same
class, because students and staff have different entities.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
22
Designing a Class, cont.
single entity
with too many responsibilities can be
broken into several classes to
separate responsibilities. The classes
(Separating
responsibilities) A
String, StringBuilder, all deal with strings, for
example, but have different responsibilities. The
String class deals with immutable strings, the
StringBuilder class is for creating mutable strings,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
23
Designing a Class, cont.
Classes
are designed for reuse. Users can
incorporate classes in many different combinations,
orders, and environments.
Therefore,
you should design a class that imposes no
restrictions on what or when the user can do with it,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
24
Designing a Class, cont.
Classes
are designed for reuse. Users can
incorporate classes in many different combinations,
orders, and environments.
Therefore,
you should design a class that imposes no
restrictions on what or when the user can do with it,
Design
the properties to ensure that the user can set
properties in any order, with any combination of
values.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
25
Designing a Class, cont.
Classes
are designed for reuse. Users can
incorporate classes in many different combinations,
orders, and environments.
Therefore,
you should design a class that imposes no
restrictions on what or when the user can do with it,
Design
the properties to ensure that the user can set
properties in any order, with any combination of
values, and
Design
methods to function independently of their
order of occurrence.
26
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Designing a Class, cont.
Provide
a public no-arg constructor and override the
equals() method and the toString()
method defined in the Object class whenever
possible.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
27
Designing a Class, cont.
Follow
standard Java or C# programming style
and naming conventions.
Choose
informative names for classes, data
fields, and methods.
Always
place the data declaration before the
constructor, and place constructors before
methods.
Always
provide a constructor and initialize
variables to avoid programming errors.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
28
Thank You
for
Your attention!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
29