Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to Classes and Object Oriented Programming - Slides | CS 140, Study notes of Computer Science

Java Material Type: Notes; Professor: Sleem; Class: Computer Programming in Java; Subject: Computer Science; University: Texas Southern University; Term: Fall 2010;

Typology: Study notes

2009/2010

Uploaded on 12/16/2010

yvvy-2k5
yvvy-2k5 🇺🇸

3 documents

1 / 39

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Introduction to Classes and Object Oriented
Programming
Introduction to Classes and Object Oriented
Introduction to Classes and Object Oriented
Programming
Programming
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download Introduction to Classes and Object Oriented Programming - Slides | CS 140 and more Study notes Computer Science in PDF only on Docsity!

Introduction to Classes and Object OrientedIntroduction to Classes and Object Oriented^ Introduction to Classes and Object OrientedProgrammingProgrammingProgramming

Overview^ OverviewIn these chapters you will learn:^ ‰

What classes, objects, methods and instancevariables are.

How to declare a class and use it to create an object.

How to declare methods in a class to implement theclass’s behaviors.

How to declare instance variables in a class toimplement the class’s attributes.

How to call an object’s method to make that methodperform its task.

The differences between instance variables of a classand local variables of a method.

How to use a constructor to ensure that an object’sdata is initialized when the object is created.

Object Oriented Programming^ Object Oriented Programming^ ‰

Before Object Oriented Programming:^ „

Applications were made of procedures that call eachother.

The application is broken down into several smaller tasksand each task was implemented as a procedure (orsometimes called a function). ^

That’s why this approach is called “ProceduralProgramming”

There used to be a main procedure that starts theapplication and links several procedure together.

In summary, in Procedural programming, the unitfor biulding an application is a “Procedure” or afunction.

In Object Oriented Programming, the focus is onthe objects that exist in the application domain:^ „

What is each object made of?

How the objects interact with each other.

Objects and Object Oriented Programming^ Objects and Object Oriented Programming^ ‰

In object oriented programming, objects areprogram constructs (or units) that are used torepresent things within the software application.^ „

Examples: person, car, air fan, bank account, etc.

Objects have information about themselves.^ „

For example, a person knows his name (data) and knowshow to walk and how to talk (behavior).

An object oriented application will have differentobjects that interact with each other.

How can the application create these object?

Real-^ Real

-Life ObjectsLife Objects

The following is a picture of an air fan. Can you describe it?

Please, make your description as general as possible so that itcan be applied to other air fans as well.

Your descriptions should cover the looks as well asfunctionality of the fan.

Real-^ Real

-Life ObjectsLife Objects

Does your description apply to the following fans?

How about these??!!

Creating Classes^ Creating Classes^ ‰

Classes are abstract data types just as theone we did for the air fan

In Java, classes are defined using a classdeclaration:

[public]

class

ClassName

class-body

Generally a class will have^ „

Variables (data)

Methods (behavior)

Creating Classes^ Creating Classes^ ‰

A public class, must be written in aseparate source file that has its name.

Example: Create a class AirFan thatsimulates an air fan and use it in a smallprogram.

Using Classes to Create Program Objects^ Using Classes to Create Program Objects

Instantiation
Object
classname objectname
= new
classname
( agrs
Scanner input = new Scanner(System.in);

Examples:

Class^ AirFan MyDeskFan = new AirFan();

Using Classes to Create Program Objects^ Using Classes to Create Program Objects

Class

Applications use classes to create objects in memory

public

class

AirFan

{

int

numOfBlades; String

color;

boolean

powerOn;

int

speed; boolean

pluggedIn;

............... }

AirFan

MyBedRoomFan

=^

new

AirFan();

AirFan

MyDeskFan

(^) = new

AirFan();

numOfBlades

colorpoerOnspeed MyDeskFan pluggedIn numofBlades

colorpoerOnspeed MyBedRoomFan pluggedIn

Using the AirFan^ Using the

AirFan Class

Class

public

class

TestFan

{

public

static

void

main(String[]

args)

{

//^

Create

one

object

of

the

AirFan

class

AirFan

myDeskFan

=^

new

AirFan();

myDeskFan.initializeFan();myDeskFan.dispStatus();//^

the

following

steps

will

simulate

the

following:

//^

1-^

Plug

in

the

power

for

first

fan

//^

2-^

turn

it

on

//^

3-^

increase

the

speed

//^

4-^

unplug

the

power

while

it

is

on

myDeskFan.plugInPower();myDeskFan.dispStatus();myDeskFan.turnOn();myDeskFan.dispStatus();myDeskFan.increaseSpeed();myDeskFan.dispStatus();myDeskFan.unPlugPower();myDeskFan.dispStatus(); } }

TestFan.java

Components of a Class Body.^ Components of a Class Body.

Classes are defined using a class declaration:

[public] class ClassName

{ class-body}

The class body can contain the following elements:^ „

Variables
„^
Methods
„^
Constructors: Similar to a method but is run to initialize an objectwhen an instance is created.
−^
must have the same name as the class
−^
it doesn’t have a return type
„^
Initializers: Stand-alone blocks of code that are run only once, whenthe class is initialized.

−^

Two types:

Static initializers and

Instance instializers

Other classes: A class can contain other classes (nested classes)

„^
Interfaces: Abstract methods and final fields. Will talk about it later.

Constructors^ Constructors

Here’s the basic format for coding a constructor:

public

ClassName

parameter-list

statements...

The body of the constructor should have anything youwant to execute when the object is created in memory.

The most common reason of creating a constructor isto provide initial values for the class variables whenyou create an object.

Constructors^ Constructors

Example: The following is the declaration of the class Actor whichhas a constructor.

public

class

Actor

{

String

lastName; String

firstName; public

Actor(String

last,

String

first)

{

lastName

=^

last;

firstName

=^

first;

} public

void

display()

{ System.out.println(firstName+"

"+lastName);

} }

Constructor: - Same name as the class- No return type

Here is how you create objects usingthe constructor.

Actor

bestActor

=

new

Actor("Smith",

"John");

firstNamelastName

Smith

bestActor

John

firstNamelastName

Allgood

bestSupportingActor

Brad

Actor

bestSupportingActor

=^

new

Actor("Allgood",

"Brad");