DOC PREVIEW
UNF COP 2551 - Lecture Notes

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 6 2 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 6 3 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 6 4 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 6 5 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 These are all abstract methods because they are declared within an interface They could be preceded with abstract but this 2004 is not necessary Pearson Addison Wesley All rights reserved A semicolon immediately follows each method header 6 6 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 6 7 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 6 8 Interfaces public class CanDo implements Doable public void doThis implements is a reserved word whatever public void doThat whatever Each method listed in Doable is given a definition etc 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 6 9 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 6 10 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 6 11 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 2004 Pearson Addison Wesley All rights reserved 6 12 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 possible scan nextLine 2004 Pearson Addison Wesley All rights reserved 6 13 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 6 14 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


View Full Document

UNF COP 2551 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?