Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 COS240 O-O Languages AUBG,

Similar presentations


Presentation on theme: "Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 COS240 O-O Languages AUBG,"— Presentation transcript:

1 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 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

2 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 2 Lecture Contents: F To apply class abstraction to develop software. F To explore the differences btw procedural paradigm and object-oriented paradigm. F To design programs using OO paradigm. F To design classes that follow the class- design guidelines.

3 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 3 Motivations F You already know in general the basics of object- oriented programming from the preceding lectures that were more or less focused on introducing Java syntax (1 st part) and C# syntax (2 nd part). F This lecture will demonstrate how to solve problems using the object-oriented paradigm.

4 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 4 Class Abstraction and Encapsulation F 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.

5 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 5 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.

6 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 6 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:

7 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 7 Problem: Computing Body Mass Index F Solution in spaghetti code programming style –Java source text file: SpaghettiCodeBMI.java F Solution in structured programming style –Java source text file: StructuredBMI.java F Solution in OO style –Java source text file: ObjectOrientedBMI.java

8 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 8 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"); }

9 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 9 Problem: Computing Body Mass Index F Solution in spaghetti code programming style: F Drawback: –The code cannot be reused in other programs F To make it reusable switch to structured programming style: F define a method to compute BMI as follows: double getBMI(double weight, double height)

10 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 10 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: "); weight = console.nextDouble(); System.out.print("Enter the height in meters: "); 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"); }

11 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 11 Problem: Computing Body Mass Index F Solution in structured programming style: F 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. F The ideal way to couple them is to create an object that contains them using instance data fields. F You can define a user class BMI (see next slide).

12 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 12 The BMI Class

13 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 13 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 class BMI

14 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 14 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

15 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 15 Advantages of OOP approach F Structured Programming approach focuses on designing member functions (methods). F OO approach couples data and methods into objects. F OO approach focuses on objects and operations on objects.

16 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 16 Advantages of OOP approach F 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/. F OOP places data and the operations in an object. F OOP organizes programs in a way that mirrors the real world, in which all objects are associated with both attributes and activities.

17 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 17 Advantages of OOP approach F Using objects improves SW reusability and makes programs easier to develop and easier to maintain F Programming in Java/C# stimulates thinking in objects F Java/C# program can be viewed as a collection of cooperating objects (to discuss in next lecture).

18 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 18 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

19 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 19 Teaching by example and Learning by doing

20 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 20 Designing the Course class F Problem: to process course information F Each course has name and has students enrolled. F You should be able to add/drop students to/from the course. F See the UML class diagram  Write the Java source text and test the functionality of the Course class

21 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 21 Example: The Course Class

22 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 22 Designing a Class  (Coherence) A class should describe a single entity, and all the class operations should logically fit together to support a coherent purpose. F You can use a class for students, for example, F BUT F You should not combine students and staff in the same class, because students and staff have different entities.

23 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 23 Designing a Class, cont.  (Separating responsibilities) A single entity with too many responsibilities can be broken into several classes to separate responsibilities. The classes 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,

24 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 24 Designing a Class, cont. F Classes are designed for reuse. Users can incorporate classes in many different combinations, orders, and environments. F Therefore, you should design a class that imposes no restrictions on what or when the user can do with it,

25 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 25 Designing a Class, cont. F Classes are designed for reuse. Users can incorporate classes in many different combinations, orders, and environments. F Therefore, you should design a class that imposes no restrictions on what or when the user can do with it, F Design the properties to ensure that the user can set properties in any order, with any combination of values.

26 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 26 Designing a Class, cont. F Classes are designed for reuse. Users can incorporate classes in many different combinations, orders, and environments. F Therefore, you should design a class that imposes no restrictions on what or when the user can do with it, F Design the properties to ensure that the user can set properties in any order, with any combination of values, and F Design methods to function independently of their order of occurrence.

27 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 27 Designing a Class, cont. F Provide a public no-arg constructor and override the equals() method and the toString() method defined in the Object class whenever possible.

28 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 28 Designing a Class, cont. F Follow standard Java or C# programming style and naming conventions. F Choose informative names for classes, data fields, and methods. F Always place the data declaration before the constructor, and place constructors before methods. F Always provide a constructor and initialize variables to avoid programming errors.

29 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080729 Thank You for Your attention!


Download ppt "Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 COS240 O-O Languages AUBG,"

Similar presentations


Ads by Google