Computer Science Notes Chapter 9 Page 1 of 5

Chapter 9: Thinking in Objects These notes are meant to accompany Introduction to Java Programming: Brief Version, seventh edition by Y. Daniel Lang.

Programming Skills in a Nutshell: At the end of this chapter you should have the following programming skills: 1. To use the “this” operator. 2. To understand immutable objects. 3. To understand the scope of variables. 4. To improve your ability to think of problems in terms of objects.

Book’s Statement of Skills: 1. To create immutable objects from immutable classes to protect the contents of objects. (9.2) 2. To determine the scope of variables in the context of a class. (9.3) 3. To use the keyword this to refer to the object calling itself. (9.4) 4. To apply class abstraction to develop software. (9.5) 5. To explore the differences between the procedural paradigm and the object-oriented paradigm. (9.6) 6. To design programs using the object-oriented paradigm. (9.7 – 9.9) Computer Science Notes Chapter 9 Page 2 of 5

Group Project: Write a program that “does” fractions.

1. When you are presented with a programming problem like this, you first need to decide what it is the program should do. a. Here is a list of what a fraction program should do: i. Ds ii. Ds iii. ds

2. Next, you should think of how information and processes can be divided up into classes, and how to separate what the user sees as an interface from what each class should be responsible for doing. (Hint: Output to a console window should not be done by an object…) a. Here is a list of the classes in the fraction program: i. Ds ii. Ds iii. ds

b. Here is how the functionality of the fraction program should be split up between the classes. i. Ds ii. Ds iii. ds

3. Next, you should “design” the class by generating a UML diagram for its data fields, constructors, and methods. a. Here are UML diagrams for the fraction program classes:

ClassName DataField1 DataField2 DataFieldEtc Constructor1 Constructor2 ConstructorEtc Method1 Method2 Method3 MethodEtc

ClassName DataField1 DataField2 DataFieldEtc Constructor1 Constructor2 ConstructorEtc Method1 Method2 Computer Science Notes Chapter 9 Page 3 of 5

Method3 MethodEtc

4. Lastly, you implement your design as source code. a. (Do this for the fraction program in eclipse now…) Computer Science Notes Chapter 9 Page 4 of 5

Section 9.1: Introduction This chapter focuses on designing and implementing your own classes as opposed to using classes that have already been implemented.

Section 9.2: Immutable Objects and Classes In an immutable object, the data fields never change once the object has been created. Example: The String class is an immutable class.  Every method of the Strin g class returns a new String object.  String str1 = “hello”;  str1= str1.toUpper(); // str1 now “points to” a new String object with the letters “HELLO”  //the old String containing “hello” is lost forever, because no variable is referencing it.  “hello” sits unused in memory until the “garbage collector” deletes it permanently

Section 8.2.11 Problem: Checking Palindromes  See www.cs.armstrong.edu/liang/intro7e/book/CheckPalindrome.java

Section 9.3: The Scope of Variables Variables declared in a method of an object only have visibility in that method Variable declared in a loop are visible only in the loop. Variables declared outside any method in a class are data fields and are visible to every method.  Objects are visible inside their braces and braces inside those braces

Section 9.4 The this Reference You are allowed to create variable with the same name as a data field To specify which variable you mean, use this.dataField… Can also use this.methodName() to be explicit about methods when dealing with reference variables for classes.

Section 9.5 Class Abstraction and Encapsulation  See www.cs.armstrong.edu/liang/intro7e/book/TestLoanClass.java  See www.cs.armstrong.edu/liang/intro7e/book/Loan.java

Section 9.6 Object-Oriented Thinking  See www.cs.armstrong.edu/liang/intro7e/book/BMI.java

Section 9.7 Designing the Course Class  See www.cs.armstrong.edu/liang/intro7e/book/TestCourse.java  See www.cs.armstrong.edu/liang/intro7e/book/Course.java

Section 9.8 Designing a Class for Stacks  See www.cs.armstrong.edu/liang/intro7e/book/TestStackOfIntegers.java  See www.cs.armstrong.edu/liang/intro7e/book/StackOfIntegers.java

Section 9.9 Designing the GuessDate Class  See www.cs.armstrong.edu/liang/intro7e/book/UseGuessDateClass.java Computer Science Notes Chapter 9 Page 5 of 5

 See www.cs.armstrong.edu/liang/intro7e/book/GuessDate.java