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 Chapter 10 Thinking in Objects.

Similar presentations


Presentation on theme: "Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Chapter 10 Thinking in Objects."— Presentation transcript:

1 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Chapter 10 Thinking in Objects

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

3 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 3 Objectives F To create immutable objects from immutable classes to protect the contents of objects (§10.2). F To determine the scope of variables in the context of a class (§10.3). F To use the keyword this to refer to the calling object itself (§10.4). F To apply class abstraction to develop software (§10.5). F To explore the differences between the procedural paradigm and object-oriented paradigm (§10.6). F To develop classes for modeling composition relationships (§10.7). F To design programs using the object-oriented paradigm (§§10.8- 10.10). F To design classes that follow the class-design guidelines (§10.11).

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

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

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

7 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 7 Scope of Variables F The scope of instance and static variables is the entire class. They can be declared anywhere inside a class. F 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.

8 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 8 The this Keyword F 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. F Another common use of the this keyword to enable a constructor to invoke another constructor of the same class.

9 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 this keyword F Sometimes a method will need to refer to the object that is calling object itself. F 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. F 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( ): 9 // A redundant use of this. Box(double w, double h, double d) { this.width = w; this.height = h; this.depth = d; } The Java TutorialsThe Java Tutorials – this keyword

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

11 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 11 Calling Overloaded Constructor

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

13 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 13 Designing the Loan Class TestLoanClass Run Loan Video – Loan and TestLoanClass –(Main Method)

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

15 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 15 The BMI Class UseBMIClass Run BMI Video – body mass index (BMI) program

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

17 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 Case Study: Designing the Course Class TestCourse & Course 17 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 } } 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()); } }

18 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 18 Example: The StackOfIntegers Class Run TestStackOfIntegers Video – Designing a Class for Stacks

19 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 19 Designing the StackOfIntegers Class

20 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 20 Implementing StackOfIntegers Class StackOfIntegers

21 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 21 Designing the GuessDate Class Run 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); } } 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]; } }

22 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 10.4 End of the chapter Program (The MyPoint class) 22 10.4 – Design a class named MyPoint to represent a point with x- and y-coordinates. Please refer to textbook. Video – The MyPoint class


Download ppt "Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Chapter 10 Thinking in Objects."

Similar presentations


Ads by Google