Lecture Objectives Learn what is an ADT, why is it useful Understand algorithm efficiency Use...
-
Upload
zoe-harmon -
Category
Documents
-
view
212 -
download
0
Transcript of Lecture Objectives Learn what is an ADT, why is it useful Understand algorithm efficiency Use...
CS 340
Lecture Objectives
Learn what is an ADT, why is it useful Understand algorithm efficiency Use Eclipse for coding and testing Understand and use method
overloading, overriding Find out why polymorphism is useful in
software design
1
2
CS 340
ADTs or… Abstract Data Types
Click icon to add picture
3
CS 340
ADTs
Abstract Date Type (ADT)
An encapsulation of data and methods
Allows for reusable code
The user need not know about the implementation of the ADT
A user interacts with the ADT using only public methods
CS 340
ADTs (cont.)
ADTs facilitate storage, organization, and processing of information
Such ADTs often are called data structures
The Java Collections Framework provides implementations of common data structures
Other reasons for the ADTs to exist?
4
CS 340
Interfaces
An interface specifies or describes an ADT to the applications programmer: methods and actions ADTs must perform what arguments, if any, must be passed to
each method what result the method will return
The interface can be viewed as a contract which guarantees how the ADT will function
5
CS 340
Interfaces (cont.)
A class that implements the interface provides code for the ADT
If implementation satisfies the ADT contract, then Programmer may implement it as he or she
chooses The programmer may add:
data fields not in the implementation methods not in the implementation constructors
6
CS 340
Example: smartPhone Interface An smartPhone is...
It must provide operations to: wake up/sleep allow the user to open a particular
application add contact display messages adjust volume
A class that implements an smartPhone must provide a method for each operation
7
8
CS 340
Example: smartPhone Interface (cont.)
It must provide operations to: wake up/sleep allow the user to
open a particular application
add contact display messages adjust volume
public interface smartPhone {
}
Interface ___Code
9
CS 340
Example: smartPhone Interface (cont.)
It must provide operations to: wake up/sleep allow the user to
open a particular application
add contact display messages adjust volume
public interface smartPhone {
}
Interface ___Code
The keyword interface in the header indicates
that an interface is being declared
10
CS 340
Example: smartPhone Interface (cont.)
It must provide operations to: wake up/sleep allow the user to
open a particular application
add contact display messages adjust volume
public interface smartPhone {
/** Wakes up after it verifies a user's PIN.
@param pin The user's PIN
@return success or failure
*/
boolean wakeUp(String pin);
boolean sleep();
}
Interface ___Code
11
CS 340
Example: smartPhone Interface (cont.)
It must provide operations to: wake up/sleep allow the user
to open a particular application
add contact display messages adjust volume
public interface smartPhone {
/** Allows the user to open an application.
@param app The selected application
@param success Whether or not the application opened*/
void openApplication(String app, boolean success);
}
Interface ___Code
12
CS 340
Example: smartPhone Interface (cont.)
It must provide operations to: wake up/sleep allow the user to
open a particular application
add contact display messages adjust volume
public interface smartPhone {
/** Allows the user to add a contact
@param name
@param phone numbers*/
void addContact(String name, int mobile, int home, int office, int other);
}
Interface ___Code
13
CS 340
Example: smartPhone Interface (cont.)
It must provide operations to: wake up/sleep allow the user to
open a particular application
add contact display
messages adjust volume
public interface smartPhone {
/** Allows message to be displayed
@param message to be printed*/
void printMessage(String message);
}
Interface ___Code
14
CS 340
Example: smartPhone Interface (cont.)
It must provide operations to: wake up/sleep allow the user to
open a particular application
add contact display messages adjust volume
public interface smartPhone {
/** Allows the user to change volume
@param message to be printed
@return success or fail*/
boolean changeVolume(int change);
}
Interface ___Code
CS 340
Interfaces (cont.)
Shows only headings for its methods These are considered abstract methods Abstract methods must be defined in a
class that implements the interface
15
16
CS 340
Interface Definition
FORM:
public interface interfaceName { abstract method headings constant declarations}
EXAMPLE:
public interface Payable { public abstract double calcSalary(); public abstract boolean salaried(); public static final double DEDUCTIONS = 25.5;}
CS 340
The implements Clause
Here is a class that implement an interface:
public class smartPhoneiPhone implements smartPhonepublic class smartPhoneDroid implements smartPhone
A class may implement more than one interface—their names are separated by commas
17
CS 340
The implements Clause: Pitfalls Syntax error:
Class smartPhoneDroid should be declared abstract; it does not define method printMessage(String) in interface smartPhone
If a class contains an undefined abstract method, then It needs to be declared as an abstract class
18
CS 340
The implements Clause: Pitfalls (cont.)
You cannot instantiate an interface:
smartPhone asmartPhone = new smartPhone();
// invalid statement
Doing so will cause a syntax error:
interface smartPhone is abstract; cannot be instantiated
19
CS 340
Declaring a Variable of an Interface Type
You can declare a variable that has an interface type
/* expected type */smartPhoneDroid ATM0 = new smartPhoneDroid();
/* interface type */smartPhone ATM1 = new smartPhoneDroid();smartPhone ATM2 = new smartPhoneiPhone();
The reason for wanting to do this will become clear when we discuss polymorphism
20
21
Create a music library for myTunes store
Include ratings and prices
Eclipse
CS 340
22
CS 340
myStore (we sell songs online)
23Method Overriding, Method Overloading, Polymorphism
CS 340
24
Method Overriding
Returning to the Robot example, if we declare and then run:
Robot myRobot = new Computer("Asimo", "Intel", 5, 16000, 200.4);
Cylon yourRobot = new Cylon(“Toaster", "AMD", 5, 24000, 1000.8, 5000.0, 7.5);
System.out.println("My computer is:\n" + myRobot.toString());
System.out.println("Your computer is:\n" + yourRobot.toString());
What is the output?
Cylon
double pixelsdouble battleSpeed
Robot
String manufacturerString processorint diskSizeint numberOfPartsdouble processorSpeed
int getDiskSize()double getProcessorSpeed()int getParts()String toString()
CS 340
25
Method Overriding (cont.)
The output would be:My Robot is:Manufacturer: AsimoCPU: IntelBody parts: 5Disk: 16000 gigabytesSpeed: 200.4 gigahertz
Your Robot is:Manufacturer: ToasterCPU: AMDBody parts: 5Disk: 24000 gigabytesSpeed: 1000.8 gigahertz The pixels and battleSpeed
variables are not printed because Cylon has not defined a toString() method
Cylon
double pixelsdouble battleSpeed
Robot
String manufacturerString processorint diskSizeint numberOfPartsdouble processorSpeed
int getDiskSize()double getProcessorSpeed()int getParts()String toString()
CS 340
26
Method Overriding (cont.)
To define a toString() for Cylon:public String toString() { String = result = super.toString() + "\nPixels: " + pixels + "\nSpeed in battle: " +
battleSpeed + " shots/sec"; return result; }
Now Cylon's toString() method will override Robot's inherited toString()method and will be called for all Cylon objects Cylon
double pixelsdouble battleSpeed
Robot
String manufacturerString processorint diskSizeint numberOfPartsdouble processorSpeed
int getDiskSize()double getProcessorSpeed()int getParts()String toString()
CS 340
27
To define a toString() for Cylon:public String toString() { String = result = super.toString() + "\nPixels: " + pixels + "\nSpeed in battle: " +
battleSpeed + " shots/sec"; return result; }
Now Cylon's toString() method will override Robot's inherited toString()method and will be called for all Cylon objects
Method Overriding (cont.)
super.methodName()
Using the prefix super in a call to a method methodName calls the method with that name in the superclass of the current class
CS 340
Cylon
double pixelsdouble battleSpeed
Robot
String manufacturerString processorint diskSizeint numberOfPartsdouble processorSpeed
int getDiskSize()double getProcessorSpeed()int getParts()String toString()
CS 340
Method Overloading
Methods with the same name, return type, and parameters override corresponding inherited methods
Methods with the same name but different parameters are overloaded
27
Method Overloading (cont.) Take, for example, our Cylon constructor: public Cylon(String man, String processor, int parts, int
disk, double procSpeed, int pixels, int battleSpeed) {
. . .
}
If we want to have a default manufacturer for a Cylon, we can create a constructor with six parameters instead of seven
public Cylon(String processor, double ram, int disk, double procSpeed, double screen, double wei) {
this(DEFAULT_NB_MAN, double ram, int disk, double procSpeed, int pixels, int battleSpeed);
}
28
This call invokes the seven-parameter constructor passing on
the six parameters in this constructor, plus the default
manufacturer constant DEFAULT_NB_MAN
CS 340
Method Overloading: Pitfall
When overriding a method, the method must have: same name the same number and types of parameters in the same order
If not, the method will overload This error is common
SOLUTION: the annotation @Override @Overridepublic String toString() { . . .}
29
CS 340
Polymorphism
Polymorphism means having many shapes
Polymorphism is a central feature of OOP
30
CS 340
Polymorphism (cont.)
For example: You write a program to reference robots, You need a variable to reference a Robot or
a Cylon Easy!
Robot theRobot; can reference either a Robot or a Cylon—because a Cylon is-a Robot!
31
CS 340
Polymorphism (cont.)
Example:theRobot = new Cylon(“Centurion", "Intel", 4, 240, 6, 15000, 5000);
System.out.println(theRobot.toString());
Which toString() method will be called, Robot's or Cylon's?
32
Cylon
double pixelsdouble battleSpeed
Robot
String manufacturerString processorint diskSizeint numberOfPartsdouble processorSpeed
int getDiskSize()double getProcessorSpeed()int getParts()String toString()