• Divide your program into interacting parts (modules) Each module is somewhat separate from the rest of the program • Easier to read, write, and modify • Easier to find errors and fix them
what you are designing • Divide your device into interacting parts (modules) • For example, there are separate parts in ice maker to chill the water, make ice cubes, and chop up the ice
the rest of the device • For example, it is easier to modify and fix an ice machine, if the broken part can be taken out and replaced, separate from the other parts
is a collection of data and operations (methods) for this data • An abstract data type is a fancy name for class • ADTs use procedural abstraction, data abstraction, and information hiding
the details of the data structure • Data structure is a construct used to store a collection of data • For example, arrays are a very simple kind of data structure
data is hidden and inaccessible • Usually data fields are “private”, so we cannot access them outside of the class • Of course, we can access data fields within the class, because they are global variables
machine Procedural abstraction: We don’t know how ice is created or crushed Data abstraction: We don’t know how the water and ice is stored Information hiding: Not only does a panel prevent us from seeing the inner machinery, but the panel is also hard to take off!
ADT’s operations (methods) do, but not how they work • Java uses an interface, which is a list of method prototypes • In our ice machine example, this is the machine’s outside panel with its labels and buttons
that is used in the ADT and the operations (methods) of the ADT • Java uses the class definition • In our ice machine example, this is the parts within the machine
on the outside • Buttons with names that identify specific functions • Java Program • Interface • List of method prototypes that enforce the naming of methods in a class
machine • Separate parts with machinery for storing ice, crushing ice, etc. • Java Program • Class definition • Data fields and code for the methods corresponding to the method prototypes in the interface
machine • Some person pressing the buttons and hopefully not making a mess • Java Program • Client class • Instantiates (creates) objects of the class and calls methods on these objects
simple windows that we can use for user input and output • Two static methods of class JOptionPane are used in the client program FractionCalculator.java 1. showInputDialog() 2. showMessageDialog()
a window with a text box for user input • "m" is the message that you want to display for the user • s is the String that is returned from the method, which is what the user typed in the text box
a window only for an output message • null will cause the window to be placed in the center of the monitor • "m" is the message that you want to display for the user
• We want to model objects in the real world, but we can never model something perfectly • Instead, we select certain attributes and behaviors to represent in our model for an object
current data about an object, or current values of an object • For example, the attributes of a person are that person’s name, age, weight, height, nationality, eye color, languages spoken, hobbies, etc.
the object is capable of doing • For example, the behaviors of a person are to give a name, increase age, increase weight, decrease weight, grow, become a citizen, give eye color, etc.
to model words and sentences • The attributes of class String are the characters and the length, which are stored in data fields String name=new String("Nami"); //"Nami" is a String attribute //4 is a String attribute
stores both the numerator and denominator for a Fraction • Attributes (data fields) • Instantiate (create) two (2) Integer variables for numerator and denominator
create objects and initialize data fields • Method toString() to display the data fields • And many more methods, such as add, subtract, multiply, divide, etc.
numbers cannot be exactly expressed as a float (decimal), so this is a very useful class for mathematicians • For example, 1/3 cannot be exactly expressed as a decimal number, because it is 0.33333…
program, we have to look at that class’s definition to see what code gets executed • A class definition defines a set of methods and data fields for the purpose of instantiating (creating) and manipulating an object of that class
within a class definition does not matter with respect to program execution • However, within each method of a class definition, the order of the statements does matter
classes public class Fraction{ . . . } • Must use delimiters, which are the symbols { and } • The delimiters are used to set the beginning and end of class definitions and method definitions
tests) the class’s data fields and methods • This program instantiates (creates) objects of the new class, calls the class methods, and displays appropriate output • FractionCalculator.java is an example client program
declaration 3.Constructor definition 4.Method definitions • Client (driver) 1.At top, class FractionCalculator 2.In main() method, instantiate (create) objects of the class 3.In main() method, call the class methods and display output
f1=new Fraction(3, 4); • Variable f1 is the label for a box, which points to a balloon, listing the data fields • The numerator and denominator data fields, in turn, label boxes, which contain the address to another object
a constructor and the toString() method for a simple class President • Data fields: String first, middle, last • See the main() method for the use of constructors and toString() method
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture 4.Admire some artwork!