+ All Categories

Day 5

Date post: 07-Jan-2016
Category:
Upload: morna
View: 35 times
Download: 0 times
Share this document with a friend
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
Popular Tags:
23
Day 5
Transcript
Page 1: Day 5

Day 5

Page 2: Day 5

Task:

• Implement your vending machine code into the program with JOptionPane I/O (modify it)

• E-mail it to me

Page 3: Day 5

4.1 Class and Method Definitions

• Objects can represent objects in the real world, like automobiles, houses, employee records, and so forth

Page 4: Day 5

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.

Page 5: Day 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.

Page 6: Day 5

• 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.

Page 7: Day 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.

Page 8: Day 5

Instantiations of the Class Automobile:

Object Name: SamsCar

Data: amount of fuel: 10 gallons speed: 55 mphlicense plate: “135 XJK”

First Instantiation

Page 9: Day 5

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

Page 10: Day 5

• 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.

Page 11: Day 5

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.

Page 12: Day 5

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)

Page 13: Day 5

Instance Variables

• The data is referred to as instance variables

• We will call the methods methods

Page 14: Day 5

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;

Page 15: Day 5

Let’s write out the class Automobile

Page 16: Day 5

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

Page 17: Day 5

In File #2

To create our first object, we write in the body

Automobile SamsCar = new Automobile();

Page 18: Day 5

In File #2

To create our first object, we write in the body

Automobile SamsCar = new Automobile();

Page 19: Day 5

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.

Page 20: Day 5

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

Page 21: Day 5

Quotation

“There are two kinds of people in the world, those that divide the world into two and those that don’t”

Page 22: Day 5

Finish the main file together

Page 23: Day 5

• Write out the class and its methods

.. Give it a shot

we’ll take it up later


Recommended