CSCE 1030 - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

CSCE 1030

Description:

Title: Chapter 1 Author: Parul Chaturvedi Last modified by: Ryan Created Date: 5/4/2004 3:13:55 PM Document presentation format: On-screen Show (4:3) – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 10
Provided by: Parul3
Category:
Tags: csce | chapter | compiler

less

Transcript and Presenter's Notes

Title: CSCE 1030


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

2
The Basic Idea
Let us use a car as an example of an object
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

3
A Car as an Object
  • 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
An object is an instance of a class.
To create an object that is a car in your
program, you must first specify a class for Car.
The same attributes of a car listed on the
previous slides can be used to describe any of a
wide variety of cars.
This class will specify which attributes and
tasks should be included to describe or use a car.
Likewise, the same tasks are also relevant to 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.
Once the Car class is created, you may create
instances of this class.
5
Class Car and Car ObjectsPseudocode
class Car
Creating and using Car objects
  • Variables
  • color
  • manufacturer
  • maximumSpeed
  • currentSpeed
  • isRunning
  • lightsOn
  • Methods
  • startCar()
  • turnOnHeadlights()
  • accelerate()

Create new object of class Car called
martysCar Create new object of class Car called
ryansCar martysCar.startCar() ryansCar.turnOnHead
lights()
  • 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 ObjectsIn 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
Car.java
7
Class Car and Car ObjectsIn 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
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 ObjectsIn 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
ConstructorA 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 ObjectsIn 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()
TestingCar.java
Recall from an earlier slide
Create new object of class Car called
martysCar Create new object of class Car called
ryansCar martysCar.startCar() ryansCar.turnOnHead
lights()
Note We also used class Cars constructor to
initialize the values ofcolor and manufacturer
on both Car objects.
Write a Comment
User Comments (0)
About PowerShow.com