Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSCE 1030 Computer Science 1 Introduction to Object Oriented Programming.

Similar presentations


Presentation on theme: "1 CSCE 1030 Computer Science 1 Introduction to Object Oriented Programming."— Presentation transcript:

1 1 CSCE 1030 Computer Science 1 Introduction to Object Oriented Programming

2 The Basic Idea 2 In the real world, objects have certain attributes and certain tasks you can perform with or on them. A car may have attributes that describe the car such as: Color Manufacturer Maximum Speed etc … or even attributes that describe the current state of the car such as: Current Speed Is Running Lights On etc You may be able to perform the following tasks with a car: Turn on headlights Start the car Accelerate etc Let us use a car as an example of an object:

3 A Car as an Object 3 Attributes: Color Manufacturer Maximum Speed Current Speed Is Running Lights On etc Tasks: Turn on headlights Start the car Accelerate etc In Object Oriented Programming, we can represent a car as an Object. Attributes such as Color, Current Speed, Manufacturer, etc. may be held by variables. Tasks such as Accelerate or Start the car may be implemented using methods.

4 Object as an Instance of a Class 4 An object is an instance of a class. The same attributes of a car listed on the previous slides can be used to describe any of a wide variety of cars. This is useful because it allows us to reuse much of the same code to create multiple car objects in our programs. To create an object that is a car in your program, you must first specify a class for Car. This class will specify which attributes and tasks should be included to describe or use a car. Once the Car class is created, you may create instances of this class. Likewise, the same tasks are also relevant to any of a wide variety of cars.

5 Class Car and Car Objects Pseudocode 5 Variables: color manufacturer maximumSpeed currentSpeed isRunning lightsOn Methods: startCar() turnOnHeadlights() accelerate() class Car Create new object of class Car called martysCar Create new object of class Car called ryansCar martysCar.startCar() ryansCar.turnOnHeadlights() Creating and using Car objects In this example, two Car objects are created: martysCar ryansCar Next, we call the method startCar() on martysCar. Finally, we call the method turnOnHeadlights() on ryansCar. After the code to the right is executed, martysCar is running and the headlights on ryansCar are turned on.

6 Class Car and Car Objects In Java 6 public class Car { private String color; private String manufacturer; private double currentSpeed; private boolean isRunning; private boolean lightsOn; public Car(String setColor, String setManufacturer) { color = setColor; manufacturer = setManufacturer; isRunning = false; lightsOn = false; currentSpeed = 0; } public void startCar() { isRunning = true; } public void turnOnHeadlights() { lightsOn = true; } public void accelerate(double newSpeed) { currentSpeed = newSpeed; } Car.java

7 Class Car and Car Objects In Java public class Car { private String color; private String manufacturer; private double currentSpeed; private boolean isRunning; private boolean lightsOn; public Car(String setColor, String setManufacturer) { color = setColor; manufacturer = setManufacturer; isRunning = false; lightsOn = false; currentSpeed = 0; } public void startCar() { isRunning = true; } public void turnOnHeadlights() { lightsOn = true; } public void accelerate(double newSpeed) { currentSpeed = newSpeed; } 7 Access Modifiers public – available everywhere private – available only within the class Access Modifiers public – available everywhere private – available only within the class Note: If the class itself is declared as private, then this overrides the public declarations of any variables or methods within that class! Car.java

8 Class Car and Car Objects In Java public class Car { private String color; private String manufacturer; private double currentSpeed; private boolean isRunning; private boolean lightsOn; public Car(String setColor, String setManufacturer) { color = setColor; manufacturer = setManufacturer; isRunning = false; lightsOn = false; currentSpeed = 0; } public void startCar() { isRunning = true; } public void turnOnHeadlights() { lightsOn = true; } public void accelerate(double newSpeed) { currentSpeed = newSpeed; } 8 Constructor A classs constructor is called when creating a new object of that class. Note: In this example, the constructor accepts two variables as arguments ( setColor and setManufacturer ). When this constructor is called with these two arguments, it uses them to set the values of color and manufacturer. It also then initializes the values of isRunning, lightsOn, and currentSpeed. Also: If you do not provide a constructor in your class, the Java compiler will provide a default constructor at compile time that includes no arguments. Car.java

9 Class Car and Car Objects In Java public class TestingCar { public static void main(String args[]) { Car martysCar = new Car(blue, Hyundai); Car ryansCar = new Car(red, Cadillac); martysCar.startCar(); ryansCar.turnOnHeadlights(); } 9 TestingCar.java Create new object of class Car called martysCar Create new object of class Car called ryansCar martysCar.startCar() ryansCar.turnOnHeadlights() Recall from an earlier slide: Note: We also used class Cars constructor to initialize the values of color and manufacturer on both Car objects.


Download ppt "1 CSCE 1030 Computer Science 1 Introduction to Object Oriented Programming."

Similar presentations


Ads by Google