1 / 22

Day 9

Day 9. Encapsulation. 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.

tan
Download Presentation

Day 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Day 9 Encapsulation

  2. 4.1 Class and Method Definitions • Objects can represent objects in the real world, like automobiles, houses, employee records, and so forth

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

  4. A Class as an Outline Class Name: AutomobileData: amount of fuel ____________speed ___________license plate ____________Methods (actions) increase Speed: How: Press on gas pedal. stop: How: Press on brake pedal.

  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.

  6. A Class as an Outline Class Name: AutomobileData: amount of fuel ____________speed ___________license plate ____________Methods (actions) increase Speed: How: Press on gas pedal. stop: How: Press on brake pedal.

  7. Instantiations of the Class Automobile: Object Name: SamsCarData: amount of fuel: 10 gallons speed: 55 mphlicense plate: “135 XJK” First Instantiation

  8. Instantiations of the Class Automobile: Object Name: SamsCarData: amount of fuel: 10 gallons speed: 55 mphlicense plate: “135 XJK”Object Name: WillsCarData: amount of fuel: 12 gallons speed: 51 mphlicense plate: “153 ZSA” First Instantiation Second Instantiation

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

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

  11. A Class Outline as a UML Class Diagram Class Name Data Methods (actions)

  12. Instance Variables • The data is referred to as instance variables • We will call the methods methods

  13. 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;

  14. Let’s write out the class Automobile

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

  16. In File #2 To create our first object, we write in the body Automobile SamsCar = new Automobile();

  17. In File #2 To create our first object, we write in the body Automobile SamsCar = new Automobile();

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

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

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

  21. Finish the main file together

  22. Write out the class and its methods .. Give it a shotwe’ll take it up later

More Related