Object-Oriented Design Part 2

Slides:



Advertisements
Similar presentations
Interfaces A Java interface is a collection
Advertisements

Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
Java Software Solutions
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
Chapter 5: Enhancing Classes
Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Chapter Day 16. © 2007 Pearson Addison-Wesley. All rights reserved6-2 Agenda Day 16 Problem set 3 posted  10 problems from chapters 5 & 6  Due in one.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Classes, Encapsulation, Methods and Constructors
Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”
Interfaces A Java interface is a collection of constants and abstract methods with a name that looks like a class name, i.e. first letter is capitalized.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Object Oriented Design and UML
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
Programming in Java (COP 2250) Lecture 14 & 15 Chengyong Yang Fall, 2005.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 6: Object-Oriented Design.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Designing Classes Chapter 3. Contents Encapsulation Specifying Methods Java Interfaces – Writing an Interface – Implementing an Interface – An Interface.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Object-Oriented Design. Static Class Members Recall that a static method is one that can be invoked through its class name For example, the methods of.
Chapter 7 Object-Oriented Design
Chapter 5: Enhancing Classes
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Chapter 5: Enhancing Classes
Creating Objects & String Class
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Passing Objects to Methods
More Object Oriented Programming
Object Oriented Programming
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
CSI 1102 Introduction to Software Design
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Lecture 16 - Interfaces Professor Adams.
Chapter 5: Enhancing Classes
Chapter 4 Writing Classes.
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Chapter 8 Inheritance.
Object Oriented Programming Review
Classes, Objects and Methods
Chapter 8 Inheritance Part 2.
2009 Test Key.
Presentation transcript:

Object-Oriented Design Part 2 Chapter 6 Object-Oriented Design Part 2

The this Reference The this reference allows an object to refer to itself  That is, the this reference, used inside a method, refers to the object through which the method is being executed Suppose the this reference is used in a method called tryMe, which is invoked as follows: obj1.tryMe(); obj2.tryMe(); In the first invocation, the this reference refers to obj1; in the second it refers to obj2 © 2004 Pearson Addison-Wesley. All rights reserved

The this reference The this reference can be used to distinguish the instance variables of a class from corresponding method parameters with the same names The constructor of the Account class (from Chapter 4) could have been written as follows: public Account (Sring name, long acctNumber, double balance) { this.name = name; this.acctNumber = acctNumber; this.balance = balance; } © 2004 Pearson Addison-Wesley. All rights reserved

Outline Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited Method Design Testing GUI Design and Layout © 2004 Pearson Addison-Wesley. All rights reserved

Interfaces A Java interface is a collection of abstract methods and constants An abstract method is a method header without a method body An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off An interface is used to establish a set of methods that a class will implement. Note: the Java interface is itself NOT a class! © 2004 Pearson Addison-Wesley. All rights reserved

Interfaces interface is a reserved word None of the methods in an interface are given a definition (body) public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); } A semicolon immediately follows each method header These are all abstract methods because they are declared within an ‘interface.’ They could be preceded with ‘abstract’ but this is not necessary! © 2004 Pearson Addison-Wesley. All rights reserved

Note: In defining ‘classes,’ we say: public class whatever In defining an ‘interface,’ we say public interface whatever (we do Not say ‘class.’) © 2004 Pearson Addison-Wesley. All rights reserved

Interfaces An interface cannot be instantiated. Recall, it is NOT a class! Methods in an interface have public visibility by default  A class formally implements an interface by: stating so in the class header providing implementations for each abstract method in the interface If a class asserts that it “implements an interface,” it must define all methods in the interface © 2004 Pearson Addison-Wesley. All rights reserved

Interfaces public class CanDo implements Doable { public void doThis () // whatever } public void doThat () // etc. implements is a reserved word Each method listed in Doable is given a definition Note: this class ‘implements’ the interface. All abstract methods in the interface must be defined here in the class that ‘implements’ the interface. © 2004 Pearson Addison-Wesley. All rights reserved

Interfaces A class that implements an interface can implement other methods as well See Complexity.java (page 310) See Question.java (page 311) See MiniQuiz.java (page 313) In addition to (or instead of) abstract methods, an interface can contain constants When a class implements an interface, it gains access to all its constants © 2004 Pearson Addison-Wesley. All rights reserved

//************************************************ // Complexity.java Author: Lewis/Loftus // // Represents the interface for an object that can be assigned // an explicit complexity. public interface Complexity { public void setComplexity (int complexity); public int getComplexity(); } Note the semicolon after the abstract methods! Note: public  interface… © 2004 Pearson Addison-Wesley. All rights reserved

public class Question implements Complexity { // Question.java Author: Lewis/Loftus // Represents a question (and its answer). public class Question implements Complexity { private String question, answer; private int complexityLevel; // Constructor: Sets up the question with a default complexity. public Question (String query, String result) // Of course, we usually need a Constructor…. question = query; answer = result; complexityLevel = 1; } // Sets the complexity level for this question. public void setComplexity (int level) // Must have this one complexityLevel = level; // Returns the complexity level for this question. public int getComplexity() // Must have this one. return complexityLevel; // Returns the question. public String getQuestion() { return question; } // Do NOT code like I have the next two methods. Only for space here! // Returns the answer to this question. public String getAnswer() { return answer; } // Returns true if the candidate answer matches the answer. public boolean answerCorrect (String candidateAnswer) return answer.equals(candidateAnswer); // Returns this question (and its answer) as a string. public String toString() return question + "\n" + answer; © 2004 Pearson Addison-Wesley. All rights reserved

// MiniQuiz.java Author: Lewis/Loftus // Demonstrates the use of a class that implements an interface. import java.util.Scanner; public class MiniQuiz { // Presents a short quiz. public static void main (String[] args) Question q1, q2; // Creates two references to Question objects. String possible; Scanner scan = new Scanner (System.in); // We use a different approach………… q1 = new Question ("What is the capital of Jamaica?", "Kingston"); // Creates object, q1 q1.setComplexity (4); q2 = new Question ("Which is worse, ignorance or apathy?", “I don't know and I don't care"); q2.setComplexity (10); // Creates a second object, q2. System.out.print (q1.getQuestion()); System.out.println (" (Level: " + q1.getComplexity() + ")"); possible = scan.nextLine(); if (q1.answerCorrect(possible)) System.out.println ("Correct"); else System.out.println ("No, the answer is " + q1.getAnswer()); System.out.println(); System.out.print (q2.getQuestion()); System.out.println (" (Level: " + q2.getComplexity() + ")"); © 2004 Pearson Addison-Wesley. All rights reserved

Interfaces A class can implement multiple interfaces The interfaces are listed in the implements clause The class must implement all methods in all interfaces listed in the header class ManyThings implements interface1, interface2 { // all methods of both interfaces } © 2004 Pearson Addison-Wesley. All rights reserved

Interfaces The Java standard class library contains many helpful interfaces The Comparable interface contains one abstract method called compareTo, which is used to compare two objects We discussed the compareTo method of the String class in Chapter 5 The String class implements Comparable, giving us the ability to put strings in lexicographic order  Note: If the Java API provides the interface (fine), WE must provide the “implementation” of that interface. This means we can tailor it to whatever! © 2004 Pearson Addison-Wesley. All rights reserved

The Comparable Interface Any class can implement Comparable to provide a mechanism for comparing objects of that type. Thus WE can decide exactly how we won’t to determine how two “things” are to be compared! if (obj1.compareTo(obj2) < 0) System.out.println ("obj1 is less than obj2"); The value returned from compareTo should be negative is obj1 is less that obj2, 0 if they are equal, and positive if obj1 is greater than obj2 When a programmer designs a class that implements the Comparable interface, it should follow this intent © 2004 Pearson Addison-Wesley. All rights reserved

The Comparable Interface It's up to the programmer to determine what makes one object less than another If we are comparing two … Strings, (this is already done for us in String), we decide ‘how’ to compare. If we are comparing two, say, air filters on cars, we may decide to compare by comparing the volume of air that each allows through the filter…  The implementation of the method can be as straightforward or as complex as needed for the situation © 2004 Pearson Addison-Wesley. All rights reserved

The Iterator Interface As we discussed in Chapter 5, an iterator is an object that provides a means of processing a collection of objects one at a time  An iterator is created formally by implementing the Iterator interface, which contains three methods The hasNext method returns a boolean result – true if there are items left to process The next method returns the next object in the iteration The remove method removes the object most recently returned by the next method So, WE must define HOW we implement the Iterator! © 2004 Pearson Addison-Wesley. All rights reserved

The Iterator Interface By implementing the Iterator interface, a class formally establishes that objects of that type are iterators (that is, whatever we are iterating thru…, whether the ‘collection’ is a list of Strings, objects, integers, etc.) The programmer must decide how best to implement the iterator functions Once established, the for-each version of the for loop can be used to process the items in the iterator © 2004 Pearson Addison-Wesley. All rights reserved

Interfaces You could write a class that implements certain methods (such as compareTo) without formally implementing the interface (Comparable) However, formally establishing the relationship between a class and an interface allows Java to deal with an object in certain ways Interfaces are a key aspect of object-oriented design in Java We discuss this idea further in Chapter 9 © 2004 Pearson Addison-Wesley. All rights reserved