Upload
morna
View
35
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Day 5. Task:. Implement your vending machine code into the program with JOptionPane I/O (modify it) E-mail it to me. 4.1 Class and Method Definitions. Objects can represent objects in the real world, like automobiles, houses, employee records, and so forth. - PowerPoint PPT Presentation
Citation preview
Day 5
Task:
• Implement your vending machine code into the program with JOptionPane I/O (modify it)
• E-mail it to me
4.1 Class and Method Definitions
• Objects can represent objects in the real world, like automobiles, houses, employee records, and so forth
4.1 Class and Method Definitions
• Objects can represent objects in the real world, like automobiles, houses, student records, and so forth.
• A class is the definition of a kind of object. The class is a general description of what an automobile (or object) can do.
A Class as an Outline
Class Name: Automobile
Data: amount of fuel ____________speed ___________license plate ____________
Methods (actions) increase Speed: How: Press on gas pedal. stop: How: Press on brake pedal.
• An object that satisfies the class definition of an Automobile is said to instantiate the Automobile class.
• Thus, objects are individual automobiles, while the Automobile class is a description of what an automobile is and does.
A Class as an Outline
Class Name: Automobile
Data: amount of fuel ____________speed ___________license plate ____________
Methods (actions) increase Speed: How: Press on gas pedal. stop: How: Press on brake pedal.
Instantiations of the Class Automobile:
Object Name: SamsCar
Data: amount of fuel: 10 gallons speed: 55 mphlicense plate: “135 XJK”
First Instantiation
Instantiations of the Class Automobile:
Object Name: SamsCar
Data: amount of fuel: 10 gallons speed: 55 mphlicense plate: “135 XJK”
Object Name: WillsCar
Data: amount of fuel: 12 gallons speed: 51 mphlicense plate: “153 ZSA”
First Instantiation
Second Instantiation
• In a program that uses the class Automobile, the only actions an Automobile can take are increaseSpeed and stop.
• These actions are called methods. All objects of the class Automobile have the same methods.
UML class diagram
• There’s no way we are gonna keep writing out large descriptions of Automobile as we just did. There’s quick way of describing the task to the programmer:
• UML stands for Universal Modeling Language, or we can simply call it a class diagram.
A Class Outline as a UML Class Diagram
Automobile
- fuel: double- speed: double- License: String
+ increaseSpeed(double howHardPress): void+ stop(double howHardPress): void
Class Name
Data
Methods (actions)
Instance Variables
• The data is referred to as instance variables
• We will call the methods methods
Instance Variables
The following lines from the start of the class definition define three instance variables:
public double fuel;public double speed;public String license;
Let’s write out the class Automobile
Now let’s write the main program
In a new file with a different class name, which includes the line public static void main (String [] args)
we will create an object of Automobile
In File #2
To create our first object, we write in the body
Automobile SamsCar = new Automobile();
In File #2
To create our first object, we write in the body
Automobile SamsCar = new Automobile();
Using Methods
• When you use a method, you are said to invoke or call it.
• You have already invoked methods. For example, your programs have invoked nextInt() with objects of the class Scanner.
Types of Methods
• There are two kinds of methods:
(1) those that return a single value
(2) those that perform some action other than returning a single value
Quotation
“There are two kinds of people in the world, those that divide the world into two and those that don’t”
Finish the main file together
• Write out the class and its methods
.. Give it a shot
we’ll take it up later