User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 391,771 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,257 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1768 | Replies: 9
Reply
Join Date: Aug 2004
Location: Los Angeles
Posts: 343
Reputation: Ghost is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Help Help w/ constructors

  #1  
Oct 9th, 2004
I need urgent help with the following problem:

Suppose  there is a class  AirConditioner . The class  supports the following behaviors : turning the air conditioner on and off. The following methods  are provided for these behaviors : turnOn and turnOff . Both methods  accept no arguments  and return no value .
There is a reference variable  myAC to an object  of this class , which has already  been created. Send a message  to this object  using the reference variable , telling it to turn the air conditioner on.

I think the answer is:

sendMessage(turnOn);

but i'm worng.

PLEASE HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!
CRD
Join the CRD forums! If you're one of the first 4 people to post in a topic, you get FREE web hosting with:
Apache WWW Host
Unlimited (within reason) storage
Unlimited (within reason) bandwith
PHP
mySQL
FTP
Unlimited Subdomains Hosted
Unlimited Domains Hosted

ALL FREE!!

Click here to visit
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Help w/ constructors

  #2  
Oct 9th, 2004
If the object is myAC and the interface consists of turnOn and turnOff then there are only two things you can do with an existing object: call turnOn and call turnOff. The syntax to call a method of an object is a simple dot hierarchy <object>.<method>(<arguments>). Because turnOn and turnOff don't take arguments, you can leave the list empty and replacing object with myAC and method with turnOn, you get this:
myAC.turnOn();
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Aug 2004
Location: Los Angeles
Posts: 343
Reputation: Ghost is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Help w/ constructors

  #3  
Oct 9th, 2004
CORRECT!

Thanks
CRD
Join the CRD forums! If you're one of the first 4 people to post in a topic, you get FREE web hosting with:
Apache WWW Host
Unlimited (within reason) storage
Unlimited (within reason) bandwith
PHP
mySQL
FTP
Unlimited Subdomains Hosted
Unlimited Domains Hosted

ALL FREE!!

Click here to visit
Reply With Quote  
Join Date: Aug 2004
Location: Los Angeles
Posts: 343
Reputation: Ghost is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Help w/ constructors

  #4  
Oct 10th, 2004
Can u help me w/ the next problem:

Suppose there is a class AirConditioner . The class supports the following behaviors : turning the air conditioner on and off. The following methods are provided for these behaviors : turnOn and turnOff . Both methods accept no arguments and return no value .
There is a reference variable officeAC of type AirConditioner . Create a new object of type AirConditioner using the officeACreference variable . After that, turn the air conditioner on using the reference to the new object .

I think the answer is:

AirConditioner of=new AirConditioner();
officeAC.turnOn();

Thanks!
CRD
Join the CRD forums! If you're one of the first 4 people to post in a topic, you get FREE web hosting with:
Apache WWW Host
Unlimited (within reason) storage
Unlimited (within reason) bandwith
PHP
mySQL
FTP
Unlimited Subdomains Hosted
Unlimited Domains Hosted

ALL FREE!!

Click here to visit
Reply With Quote  
Join Date: Aug 2004
Location: Los Angeles
Posts: 343
Reputation: Ghost is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Help w/ constructors

  #5  
Oct 10th, 2004
P.S. I have 2 books - none deal w/ messages
CRD
Join the CRD forums! If you're one of the first 4 people to post in a topic, you get FREE web hosting with:
Apache WWW Host
Unlimited (within reason) storage
Unlimited (within reason) bandwith
PHP
mySQL
FTP
Unlimited Subdomains Hosted
Unlimited Domains Hosted

ALL FREE!!

Click here to visit
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Help w/ constructors

  #6  
Oct 10th, 2004
>none deal w/ messages
You aren't up to a point where messages are meaningful, your instructor is just using weird terminology to confuse you.

Because the problem suggests that officeAC is a variable that already exists, you can simply do this:
officeAC = new AirConditioner();

officeAC.turnOn();
>P.S. I have 2 books
p.s. I don't know of any Java book that doesn't cover basic constructor usage.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Aug 2004
Location: Los Angeles
Posts: 343
Reputation: Ghost is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Help w/ constructors

  #7  
Oct 10th, 2004
Correct!!!
Thanks!

One last question:

You are given  a class named  Clock that has three instance variables : One of type  int  called hours , another of type  boolean  called isTicking , and the last one of type  Integer  called diff . Write a constructor  for the class  Clock that takes three parameters  -- an int  , a boolean  , and another int  . The constructor  should set the instance variables  to the values  provided.

I think the answer is:

public Clock(int hours, boolean isTicking, Integer diff){
this.hours=hours;
this.isTicking=isTicking;
this.diff;
}

but I get this error message:

CTest.java:9: not a statement
this.diff;
^
1 error

Thanks.
CRD
Join the CRD forums! If you're one of the first 4 people to post in a topic, you get FREE web hosting with:
Apache WWW Host
Unlimited (within reason) storage
Unlimited (within reason) bandwith
PHP
mySQL
FTP
Unlimited Subdomains Hosted
Unlimited Domains Hosted

ALL FREE!!

Click here to visit
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Help w/ constructors

  #8  
Oct 10th, 2004
>this.diff;
This doesn't do very much, maybe you should assign diff to it:
public Clock ( int hours, boolean isTicking, Integer diff )
{
  this.hours = hours;
  this.isTicking = isTicking;
  this.diff = diff;
}
Though that might not be such a good idea because Integer is an object reference, and this.diff would then be a reference to the object that diff refers to. You might consider cloning diff and assigning it to this.diff:
public Clock ( int hours, boolean isTicking, Integer diff )
{
  this.hours = hours;
  this.isTicking = isTicking;
  this.diff = diff.clone();
}
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Aug 2004
Location: Los Angeles
Posts: 343
Reputation: Ghost is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Help w/ constructors

  #9  
Oct 10th, 2004
that isn't correct.

The error messages are:

To create an Integer  object  you would have to use the new keyword .

CTest.java:10: clone() has protected access in java.lang.Object
this.diff = diff.clone();
^
CTest.java:10: incompatible types
found : java.lang.Object
required: java.lang.Integer
this.diff = diff.clone();
^
2 errors

Thanks!!
CRD
Join the CRD forums! If you're one of the first 4 people to post in a topic, you get FREE web hosting with:
Apache WWW Host
Unlimited (within reason) storage
Unlimited (within reason) bandwith
PHP
mySQL
FTP
Unlimited Subdomains Hosted
Unlimited Domains Hosted

ALL FREE!!

Click here to visit
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Help w/ constructors

  #10  
Oct 10th, 2004
Show us the class, I'm tired of playing this guessing game.
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 4:43 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC