{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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.
Image of page 1

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

View Full Document Right 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 What are two different uses of the term interface in programming? The way in which a user interacts with the program is known as the user interface. A set of methods that a class must provide and an optional set of constants is interface for a set of classes, and is defined using the Java key-word interface .
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern