































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
































Objects and Object Oriented Programming^ Objects and Object Oriented Programming^
Using Classes to Create Program Objects^ Using Classes to Create Program Objects
Using Classes to Create Program Objects^ Using Classes to Create Program Objects
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
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(); } }
{ class-body}
−^
Two types:
Static initializers and
Instance instializers
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);
} }
Actor
bestActor
=
new
Actor("Smith",
"John");
firstNamelastName
Smith
bestActor
John
firstNamelastName
Allgood
bestSupportingActor
Brad
Actor
bestSupportingActor
=^
new
Actor("Allgood",
"Brad");