Interfaces ICS 111 Introduction to Computer Science II





















- Slides: 21
Interfaces • ICS 111: Introduction to Computer Science II – William Albritton • Information and Computer Sciences Department at the University of Hawai‘i at Mānoa • “The term spaghetti code is universally understood as an insult. All good computer scientist worship the god of modularity, since modularity brings many benefits, including the all-powerful benefit of not having to understand all parts of a problem at the same time in order to solve it. ” – David Clark • Massachusetts Institute of Technology 9/18/2020 © 2007 William Albritton 1
Last Assignment • Go over last assignment – General steps for writing the main() method of the client (the program that uses the class) 1. Instantiate (create) objects of the new class 2. Call the class methods & display appropriate output – General steps for writing a class definition 1. 2. 3. 4. 9/18/2020 Write class name & { } Write the data fields at top Write the constructor Write the methods © 2007 William Albritton 2
Modular Programs • You should practice modularity when writing programs – A modular program is divided into many parts • For example, if you keep using the same code several times in a program, you should write a method for it & call the method in the program • As another example, if you are writing a program that manipulates people’s names, then you should write a Name class & create Name objects – As a general rule, you do not want to put one big, long program in the main method • This is known as “spaghetti code” 9/18/2020 © 2007 William Albritton 3
Why Use Modular Programs? • In modular programs, each method & class is somewhat separate from the rest of the program – Easier to read, write, & modify – Less repeated code – Easier to find errors & fix them • In non-modular programs, everything runs together, like spaghetti, ramen, yakisoba, etc. – More difficult to read, write, & modify – More repeated code – More difficult to find errors & fix them 9/18/2020 © 2007 William Albritton 4
Ice Machines • You should practice modularity no matter what you are designing – Divide your device into interacting parts (modules) • Separate parts in ice maker to chill the water, make ice cubes, or chop up the ice – Each module is somewhat separate from the rest of the device • Easier to modify & fix an ice machine, if broken part can be taken out & replaced, separate from the other parts 9/18/2020 © 2007 William Albritton 5
Classes • Collection of data & operations on this data – Data fields are private, so information is hidden • Information hiding = not only hiding the details, but making the details inaccessible • For example, since we do not have direct access to the characters & length & other data fields of String objects, we have to use methods to access the String data – Methods are public, so anyone can use them • A user can call a method without understanding exactly how the code inside the method works • For example, we can use the to. Upper. Case() method of class String without knowing exactly how it works 9/18/2020 © 2007 William Albritton 6
Ice Machines & Classes • A class is like an ice machine – Both have information hiding • We cannot access the private data fields • The panel prevent us from seeing the water & ice that is stored – Both have functions that we use, but don’t know exactly how they work • We can use a method without understanding the code inside the method • We can press the “crush ice” button without understanding the machinery that is crushing the ice 9/18/2020 © 2007 William Albritton 7
Interfaces & Classes • Interface – Only describes what the class methods do, but not how they work • An interface is a list of method prototypes • A class that implements the interface must have methods with the same method prototype as the interface • Class definition – Describes the class data fields and contains the code for the methods • A class definition implements an interface by writing the code for the methods required by the interface 9/18/2020 © 2007 William Albritton 8
Ice Machine Parallels • Ice machine 1. Panel of buttons on the outside – Buttons have names that identify specific functions 2. Parts within the machine – Separate parts with machinery for storing ice, crushing ice, etc. 3. Person using the machine – Some person pressing the buttons & hopefully not making a mess 9/18/2020 • Computer program 1. Interface – List of method prototypes that enforce the method prototypes of a class 2. Class definition – Data fields & code for the methods corresponding to the method prototypes in the interface 3. Client class – Instantiates (creates) objects of the class & calls methods on these objects 9
Terminology • Interface – A list of method prototypes used to enforce the naming of method prototypes of a Class • When a Class implements a particular Interface, this ensures that designers of the Class & users of the Class are using the same method prototypes – For example, the panel of buttons on an ice machine is the ice machine’s interface – Syntax for Interface 9/18/2020 • public interface Interface. Name{ //list of method prototypes } © 2007 William Albritton 10
Terminology • Interface – Another way to think of an interface is as a “contract” for methods • The class that implements the interface must fulfill the interface “contract” by defining methods with the same method prototype as the interface – Syntax for Class definition • public Class. Name implements Interface. Name{ //must have code for all the //methods in the interface } 9/18/2020 © 2007 William Albritton 11
Using Interfaces • You can use an interface when declaring local variables – Remember that a declaration is on the left side of the equals sign when initializing a variable • Visualize it as just drawing a box & assigning it the value on the right hand side of the assignment operator – Syntax • Interface. Name variable. Name = new Class. Name(); – Example • Name. Interface name 1 = new Name("Nami", "Suzuki"); 9/18/2020 © 2007 William Albritton 12
Using Interfaces II • You can also use an interface when declaring a parameter – Declaring a parameter is almost the same as declaring a local variable • Visualize it as just drawing a box & assigning it the value in the method call – Syntax • method. Name(Interface. Name parameter. Name) – Example • public static void input. Name. And. Display. Outputs(Name. Interface name 1) 9/18/2020 © 2007 William Albritton 13
Code Examples • Code examples 1. Interface – Name. Interface. java 2. Class definition – Name. java 3. Client classes – Name. Client. java – Name. Methods. java • Types of Computer Programs 1. Interface – List of method prototypes that enforce the method prototypes of a class 2. Class definition – Data fields & code for the methods corresponding to the method prototypes in the interface 3. Client class – Instantiates (creates) objects of the class & calls methods on these objects 9/18/2020 14
Class Exercise 1 • What is the output of Exercise 1. java? 9/18/2020 © 2007 William Albritton 15
Terminology • A method prototype is the first line of the method definition – It includes the modifier, return type, name of method, return type of parameters, and name of parameters • The method prototype is all code before the first “{” • A method’s code that is between the first “{” and last “}” is called the method’s body 9/18/2020 © 2007 William Albritton 16
Method Call Terminology name 1. set. First. Name("Nami"); “actual” parameter variable 9/18/2020 method name © 2007 William Albritton 17
Method Definition Terminology public void set. First. Name(String first. Param){ first = first. Param; prototype } body public void set. First. Name(String first. Param){… return type access modifier 9/18/2020 parameter’s Class name of method “formal” parameter original slide by Jan Stelovsky, modified by William Albritton 18
Code Examples II • Code examples 1. Interface – Fraction. Interface. java 2. Class definition – Fraction. java 3. Client classes – Fraction. Client. java – Fraction. Calculator. java • Types of Computer Programs 1. Interface – List of method prototypes that enforce the method prototypes of a class 2. Class definition – Data fields & code for the methods corresponding to the method prototypes in the interface 3. Client class – Instantiates (creates) objects of the class & calls methods on these objects 9/18/2020 19
Code Examples III • Code examples 1. Interface – Drawable. java 2. Class definition – Kurochan. java 3. Client classes – Kurochan. Client. Applet. java – Draw. Methods. java 4. HTML page – kurochan. html. txt • Types of Computer Programs 1. Interface – List of method prototypes that enforce the method prototypes of a class 2. Class definition – Data fields & code for the methods corresponding to the method prototypes in the interface 3. Client class – Instantiates (creates) objects of the class & calls methods on these objects 9/18/2020 20
Class Exercise 2 • Create a class with a constructor & a draw method for a simple figure of two shapes – The class should implement interface Drawable – If you have time, create an Applet that uses the constructor & the static method Draw. Methods. draw 5(. . . ) to draw 5 of these simple figures 9/18/2020 © 2007 William Albritton 21