Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP with Java, David J. Barnes Creating and Using Objects1 Why So Much for So Little? We want to use computers to solve complex tasks. Complex problems.

Similar presentations


Presentation on theme: "OOP with Java, David J. Barnes Creating and Using Objects1 Why So Much for So Little? We want to use computers to solve complex tasks. Complex problems."— Presentation transcript:

1 OOP with Java, David J. Barnes Creating and Using Objects1 Why So Much for So Little? We want to use computers to solve complex tasks. Complex problems require powerful programming languages. Powerful programming languages have more rules than simpler languages.

2 OOP with Java, David J. Barnes Creating and Using Objects2 What are Object-Oriented Languages? Support an O-O view of problem solving. Objects categorized into classes. Objects interact with each other. –Objects have behavior. –Objects maintain state. Behavior and state are intimately related. Software reuse is facilitated.

3 OOP with Java, David J. Barnes Creating and Using Objects3 Modeling the Operation of a Port Classes and objects in the scenario. –The port. –Ships moving in and out. –Cargo and associated transportation. –Passengers, vehicles, car parks. –Customs officers, pilots. –Aspects of the natural environment.

4 OOP with Java, David J. Barnes Creating and Using Objects4 Interactions in the Port Transfer of cargo between ship and dock. Passengers embarking and disembarking. A pilot joining or leaving a ship. A ship requesting permission to enter. The weather affecting ship movements.

5 OOP with Java, David J. Barnes Creating and Using Objects5 Classes and Objects A class definition provides a description of a typical object within that class. –CargoShip, PassengerFerry. An individual object is an instance of a class. Definition of behavior and attributes. –The attributes store an object’s state.

6 OOP with Java, David J. Barnes Creating and Using Objects6 Class and Instances Liner might capture the common characteristics of ocean liners. –RMS Titanic and RMS Olympic might be two instances of that class. Class definition is like a blueprint or template. –Color, size, pattern might vary, but instances of the same class come from the same mold.

7 OOP with Java, David J. Barnes Creating and Using Objects7 Creating an Object // An illustration of object creation. class ShipMain1 { public static void main(String[] args){ // Define a method variable to refer to // a Ship object. Ship argo; // Construct a new Ship object. argo = new Ship(); }

8 OOP with Java, David J. Barnes Creating and Using Objects8 Object Interactions Communication between people: –Information: “I am going to a party.” –Question: “What is the time?” –Order/Request: “Please buy me some gum.” Objects communicate in similar ways. –Passing messages to each other. –Actor/Agent, Client/Server relationships.

9 OOP with Java, David J. Barnes Creating and Using Objects9 Sending a Message to a Ship // An illustration of message sending. class ShipMain2 { public static void main(String[] args){ // Define a method variable. // Make it refer to a new Ship object. Ship argo = new Ship(); // Ask the ship to report its position, // course and speed. argo.report(); }

10 OOP with Java, David J. Barnes Creating and Using Objects10 Moving a Ship // An illustration of ship movement. class ShipMain3 { public static void main(String[] args){ Ship argo = new Ship(); // Ask the ship to move. argo.move(); }

11 OOP with Java, David J. Barnes Creating and Using Objects11 Passing Arguments with Messages The report and move messages convey implicit information. –The Ship uses its own internal state to respond. Some messages need to be accompanied by further explicit information. –“Change course to...” –“Reduce speed to...”

12 OOP with Java, David J. Barnes Creating and Using Objects12 Passing Arguments class ShipMain4 { public static void main(String[] args){ Ship argo = new Ship(); argo.report(); argo.move(); // Ask it to change course, move and // report its new settings. argo.setCourse(90); argo.move(); argo.report(); }

13 OOP with Java, David J. Barnes Creating and Using Objects13 Receiving Replies Human communication often requires a reply. –“What is the time?” –“Are there any free berths in the port?” –“What is the square-root of 2?” Similarly, we can request information from a Ship. We often store the answer somewhere.

14 OOP with Java, David J. Barnes Creating and Using Objects14 Requesting a Ship’s Speed class ShipMain5 { public static void main(String[] args){ Ship argo = new Ship(); argo.report(); // Define a variable to hold the speed. double currentSpeed; // Ask the ship what its current speed is. currentSpeed = argo.getSpeed(); // Increase the ship's speed. argo.setSpeed(currentSpeed+1); argo.move(); argo.report(); }

15 OOP with Java, David J. Barnes Creating and Using Objects15 Variables and Objects Variables and objects are different. A variable may refer to a single object. Different variables may refer to the same object - aliases. A variable may be switched from one object to another. A variable may be uninitialized.

16 OOP with Java, David J. Barnes Creating and Using Objects16 The Main Method's Class We choose never to create an object of the main method’s class. This approach is not followed by many other authorities. The main method is a static method. –Static methods have special properties that we shall explore in due course.

17 OOP with Java, David J. Barnes Creating and Using Objects17 Review O-O problem solving involves identifying classes, objects and object interactions. Objects maintain state and exhibit class- defined behavior. Instances of the same class behave in similar ways. Message passing illustrates object interaction.

18 OOP with Java, David J. Barnes Creating and Using Objects18 Review (cont.) Messages may contain additional information: in the form of arguments. A result may be returned as the result of a message. Variables and objects are distinct. –Variables switch between different objects. –An object may be simultaneously referred to by different variables.


Download ppt "OOP with Java, David J. Barnes Creating and Using Objects1 Why So Much for So Little? We want to use computers to solve complex tasks. Complex problems."

Similar presentations


Ads by Google