Download presentation
Presentation is loading. Please wait.
Published byChristiana Watkins Modified over 8 years ago
1
Day 5
2
Task: Implement your vending machine code into the program with JOptionPane I/O (modify it) E-mail it to me
3
4.1 Class and Method Definitions Objects can represent objects in the real world, like automobiles, houses, employee records, and so forth
4
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.
5
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.
6
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.
7
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.
8
Instantiations of the Class Automobile: Object Name: SamsCar Data: amount of fuel: 10 gallons speed: 55 mph license plate: “135 XJK” First Instantiation
9
Instantiations of the Class Automobile: Object Name: SamsCar Data: amount of fuel: 10 gallons speed: 55 mph license plate: “135 XJK” Object Name: WillsCar Data: amount of fuel: 12 gallons speed: 51 mph license plate: “153 ZSA” First Instantiation Second Instantiation
10
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.
11
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.
12
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)
13
Instance Variables The data is referred to as instance variables We will call the methods methods
14
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;
15
Let’s write out the class Automobile
16
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
17
In File #2 To create our first object, we write in the body Automobile SamsCar = new Automobile();
18
In File #2 To create our first object, we write in the body Automobile SamsCar = new Automobile();
19
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.
20
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
21
Quotation “There are two kinds of people in the world, those that divide the world into two and those that don’t”
22
Finish the main file together
23
Write out the class and its methods.. Give it a shot we’ll take it up later
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.