Chapter 10 Thinking in Objects

Slides:



Advertisements
Similar presentations
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 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 Immutable Objects and Classes.
1 Chapter 2 Objects and Classes. 2 Objectives F To understand objects and classes and use classes to model objects. F To learn how to declare a class.
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, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Object-Oriented Programming.
Chapter 9 Objects and Classes
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 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 COS240 O-O Languages AUBG,
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 10 Thinking in Objects.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Chapter 10 Object-Oriented Thinking. 2 Class Abstraction and Encapsulation Class abstraction means to separate class implementation details from the.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
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 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 C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
CS 112 Programming 2 Lecture 02 Objects and Classes (1)
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, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
1 Chapter 9 Objects and Classes. 2 OO Programming in Java Other than primitive data types (byte, short, int, long, float, double, char, boolean), everything.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Chapter 10 Thinking in Objects
Topic: Classes and Objects
Lecture 3: Introduction to Object and Classes
Objects as a programming concept
Chapter 7 Object-Oriented Programming
JAVA MULTIPLE CHOICE QUESTION.
Chapter 10 Thinking in Objects
Reference: COS240 Syllabus
Chapter 10 Thinking in Objects
Chapter 9 More on Objects and Classes
Chapter 10 Thinking in Objects
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Object Based Programming
Chapter 9 Thinking in Objects
Chapter 10 Thinking in Objects
Chapter 9 Objects and Classes
Chapter 9 Objects and Classes
Chapter 9 Thinking in Objects
Chapter 10 Thinking in Objects
Object Oriented Programming in java
Chapter 6 Objects and Classes
Chapter 9 Objects and Classes
Chapter 7 Objects and Classes
OO Programming Concepts
Chapter 10 Thinking in Objects Part 1
Chapter 6 Objects and Classes
Chapter 10 Thinking in Objects
Presentation transcript:

Chapter 10 Thinking in Objects

Motivations You see the advantages of object-oriented programming from the preceding two chapters. This chapter will demonstrate how to solve problems using the object-oriented paradigm. Before studying these examples, we first introduce several language features for supporting these examples.

Objectives To create immutable objects from immutable classes to protect the contents of objects (§10.2). To determine the scope of variables in the context of a class (§10.3). To use the keyword this to refer to the calling object itself (§10.4). To apply class abstraction to develop software (§10.5). To explore the differences between the procedural paradigm and object-oriented paradigm (§10.6). To develop classes for modeling composition relationships (§10.7). To design programs using the object-oriented paradigm (§§10.8-10.10). To design classes that follow the class-design guidelines (§10.11).

Immutable Objects and Classes If the contents of an object cannot be changed once the object is created, the object is called an immutable object and its class is called an immutable class. If you delete the set method in the Circle class in the preceding example, the class would be immutable because radius is private and cannot be changed without a set method. Immutable means unchangeable. In Java, when an object is defined as being immutable it means that once it has been initialized its state cannot be changed. Primitive data types (i.e., int, short, long, byte, char, float, double, boolean) can be made immutable by using the "final" keyword. Once they have been assigned a value it cannot be changed.

Example public class BirthDate { private int year; private int month; private int day; public BirthDate(int newYear, int newMonth, int newDay) { year = newYear; month = newMonth; day = newDay; } public void setYear(int newYear) { year = newYear; } } public class Student { /* if the class is immutable, then data field must be private*/ /* the Student class has private data fields and no set methods, but it is not an immutable class */ private int id; private BirthDate birthDate; public Student(int ssn, int year, int month, int day) { id = ssn; birthDate = new BirthDate(year, month, day); } public int getId() { return id; } public BirthDate getBirthDate() { return birthDate; } } public class Test { public static void main(String[] args) { Student student = new Student(111223333, 1970, 5, 3); //below, dataCreated is retruned using the getDateCreated() //this is a reference to a Date object. dataCreated can not brchanged java.util.Date dataCreated =student.getDateCrreated (); date.setYear(200000); // Now the student birth year is changed! } }

What Class is Immutable? For a class to be immutable, it must mark all data fields private and provide no mutator methods and no accessor methods that would return a reference to a mutable data field object. Immutable objects are simply objects whose state (the object's data) cannot change after construction. Examples of immutable objects from the JDK include String and Integer. The Java Tutorial - Immutable Objects

Scope of Variables The scope of instance and static variables is the entire class. They can be declared anywhere inside a class. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be initialized explicitly before it can be used.

The this Keyword The this keyword is the name of a reference that refers to an object itself. One common use of the this keyword is reference a class’s hidden data fields. Another common use of the this keyword to enable a constructor to invoke another constructor of the same class.

this keyword Sometimes a method will need to refer to the object that is calling object itself. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked. You can use this anywhere a reference to an object of the current class' type is permitted. To better understand what this refers to, consider the following version of Box( ): // A redundant use of this. Box(double w, double h, double d) { this.width = w; this.height = h; this.depth = d; } The Java Tutorials – this keyword

Reference the Hidden Data Fields The this keyword provides a way to refer to the objhect that called an instance method within the code of method. The line this.i=i means assign the value that calls the instances method setI. The Foo.k=k means that the value in parameter k is assigned to the static data field k of the class.

Calling Overloaded Constructor

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 let 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.

Designing the Loan Class TestLoanClass Loan Video – Loan and TestLoanClass –(Main Method) Run

Object-Oriented Thinking Chapters 1-6 introduced fundamental programming techniques for problem solving using loops, methods, and arrays. The studies of these techniques lay a solid foundation for object-oriented programming. Classes provide more flexibility and modularity for building reusable software. This section improves the solution for a problem introduced in Chapter 3 using the object-oriented approach. Problem: Computing Body Mass Index (BMI) – Listing 3.5 From the improvements, you will gain the insight on the differences between the procedural programming and object-oriented programming and see the benefits of developing reusable code using objects and classes.

The BMI Class BMI UseBMIClass Run Video – body mass index (BMI) program

Example: The Course Class TestCource Run

Case Study: Designing the Course Class TestCourse & Course public class TestCourse { public static void main(String[] args) { Course course1 = new Course("Data Structures"); Course course2 = new Course("Database Systems"); /*A Course object can be created using the constructor Course (String name) by passing a course name.*/ course1.addStudent("Peter Jones"); course1.addStudent("Brian Smith"); course1.addStudent("Anne Kennedy"); course2.addStudent("Peter Jones"); course2.addStudent("Steve Smith"); System.out.println("Number of students in course1: " + course1.getNumberOfStudents()); String[] students = course1.getStudents(); for (int i = 0; i < course1.getNumberOfStudents(); i++) System.out.print(students[i] + ", "); System.out.println(); System.out.print("Number of students in course2: " + course2.getNumberOfStudents()); } } public class Course { private String courseName; private String[] students = new String[100]; private int numberOfStudents; public Course(String courseName) { this.courseName = courseName; } public void addStudent(String student) { students[numberOfStudents] = student; numberOfStudents++; } public String[] getStudents() { return students; } public int getNumberOfStudents() { return numberOfStudents; } public String getCourseName() { return courseName; } public void dropStudent(String student) { // Left as an exercise in Exercise 9.9 } }

Example: The StackOfIntegers Class Video – Designing a Class for Stacks TestStackOfIntegers Run

Designing the StackOfIntegers Class

Implementing StackOfIntegers Class

Designing the GuessDate Class public class GuessDate { private final static int[][][] dates = { {{ 1, 3, 5, 7}, { 9, 11, 13, 15}, {17, 19, 21, 23}, {25, 27, 29, 31}}, {{ 2, 3, 6, 7}, {10, 11, 14, 15}, {18, 19, 22, 23}, {26, 27, 30, 31}}, {{ 4, 5, 6, 7}, {12, 13, 14, 15}, {20, 21, 22, 23}, {28, 29, 30, 31}}, {{ 8, 9, 10, 11}, {12, 13, 14, 15}, {24, 25, 26, 27}, {28, 29, 30, 31}}, {{16, 17, 18, 19}, {20, 21, 22, 23}, {24, 25, 26, 27}, {28, 29, 30, 31}}}; /** Prevent the user from creating objects from GuessDate */ private GuessDate() { } /** Return a date at the specified row and column in a given set */ public static int getValue(int setNo, int k, int j) { return dates[setNo][k][j]; } } import java.util.Scanner; public class UseGuessDateClass { public static void main(String[] args) { int date = 0; // Date to be determined int answer; // Create a Scanner Scanner input = new Scanner(System.in); for (int i = 0; i < 5; i++) { System.out.println("Is your birth date in Set" + (i + 1) + "?"); for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) System.out.print(GuessDate.getValue(i, j, k) + " "); System.out.println(); } System.out.print("\nEnter 0 for No and 1 for Yes: "); answer = input.nextInt(); if (answer == 1) date += GuessDate.getValue(i, 0, 0); } System.out.println("Your birth date is " + date); } } Run

10.4 End of the chapter Program (The MyPoint class) 10.4 – Design a class named MyPoint to represent a point with x- and y-coordinates. Please refer to textbook. Video – The MyPoint class