ch01 - 1.1 What are the two parts of an ADT? Which part is...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1.1 What are the two parts of an ADT? Which part is accessible to a user and which is not? Explain the relationships between an ADT and a class; between an ADT and an interface; and between an interface and classes that implement the interface. An ADT consists of data elements and methods that operate on that data. The user can access the operations, but cannot access the internal structure of the data elements. A class provides one way to implement an ADT in Java. A Java interface is a way to specify or describe an ADT. An interface defines a set of methods, and a class that implements an interface must implement these methods and define any necessary data fields. 1.2 Assume there is an interface named Comparable with the following definition: public interface Comparable { int compareTo(Object obj); } Do you think class String implements interface Comparable ? Provide a reason for your answer. The String class implements the interface Comparable because in defines the method compareTo . 1.3 Correct each of the following statements that is incorrect, assuming that class PDGUI and class PDConsoleUI implement interface PDUserInterface . a. PDGUI p1 = new PDConsoleUI(); b. PDGUI p2 = new PDUserInterface(); c. PDUserInterface p3 = new PDUserInterface(); d. PDUserInterface p4 = new PDConsoleUI(); e. PDGUI p5 = new PDUserInterface(); PDUserInterface p6 = p5; f. PDUserInterface p7; p7 = new PDConsoleUI(); a. PDGUI p1 = new PDConsoleUI(); These are not the same class, even though they implement the same interface. PDIGUI p1 = new PDIGUI(); or PDConsoleUI p1 = PDConsoleUI() b. PDGUI p2 = new PDUserInterface(); PDIGUI is a PDUserInterface not the other way around. PDUserInterface p2 = new PDGUI(); is more appropriate. c. PDUserInterface p3 = new PDUserInterface(); You cannot instantiate an interface.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
d. PDUserInterface p4 = new PDConsoleUI(); This is a valid statement e. PDGUI p5 = new PDUserInterface(); PDUserInterface p6 = p5; The first statement is not valid (see b above). If the first statement read PDUserniterface p5 = new PDGUI(); then the second statement would be valid and p6 and p5 would refer to the same object in memory. f. PDUserInterface p7; p7 = new PDConsoleUI(); These statements are valid. 1.4 Explain how an interface is like a contract. An interface specifies methods that a class implementing that interface must provide. For each method specified, the name, return type, and the parameters are specified. For each parameter the type is specified. A user of a class implementing the interface can be assured that the methods are as specified and the developer of class must provide the specified methods. Thus, the interface defines a contract between the user of a class and its developer. 1.5
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 11/01/2011 for the course COMPSCI 225 at Boise State.

Page1 / 11

ch01 - 1.1 What are the two parts of an ADT? Which part is...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online