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

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Introduction to Object Oriented Programming Java.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
1 Object-Oriented Design. 2 Objectives F To become familiar with the process of program development. F To the relationship types: association, aggregation,
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 9 Thinking in Objects.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Object-Oriented Programming.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Introduction to Object-oriented programming and software development Lecture 1.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Introduction to Information and Computer Science Computer Programming Lecture d This material (Comp4_Unit5d) was developed by Oregon Health and Science.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 10 Thinking in Objects.
1 Chapter 10 Object-Oriented Thinking. 2 Class Abstraction and Encapsulation Class abstraction means to separate class implementation details from the.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 4.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Programming Fundamentals I (COSC- 1336), Lecture 10 (prepared after Chapter 10 of Liang’s 2011 textbook) Stefan Andrei 12/18/20151 COSC-1336, Lecture 10.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Chapter 10 Thinking in Objects
Chapter 7 Object-Oriented Programming
Inheritance ITI1121 Nour El Kadri.
Chapter 10 Thinking in Objects
Reference: COS240 Syllabus
Chapter 10 Thinking in Objects
Chapter 11 Object-Oriented Design
Reference: COS240 Syllabus
Chapter 10 Thinking in Objects
Chapter 9 Thinking in Objects
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Chapter 9 Thinking in Objects
Week 4 Lecture-2 Chapter 6 (Methods).
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
OO Programming Concepts
Chapter 10 Thinking in Objects Part 1
Chapter 10 Thinking in Objects
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved 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 Teaching by example and Learning by doing

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

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

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

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

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

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

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

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

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

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