944,159 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2340
  • Java RSS
Oct 9th, 2004
0

Help w/ constructors

Expand Post »
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!!!!!!!!!!!!!!!!!!!!!!!!!!!
Similar Threads
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Oct 9th, 2004
0

Re: Help w/ constructors

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:
Java Syntax (Toggle Plain Text)
  1. myAC.turnOn();
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 9th, 2004
0

Re: Help w/ constructors

CORRECT!

Thanks
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Oct 10th, 2004
0

Re: Help w/ constructors

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!
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Oct 10th, 2004
0

Re: Help w/ constructors

P.S. I have 2 books - none deal w/ messages
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Oct 10th, 2004
0

Re: Help w/ constructors

>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:
Java Syntax (Toggle Plain Text)
  1. officeAC = new AirConditioner();
  2.  
  3. 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 10th, 2004
0

Re: Help w/ constructors

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.
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Oct 10th, 2004
0

Re: Help w/ constructors

>this.diff;
This doesn't do very much, maybe you should assign diff to it:
Java Syntax (Toggle Plain Text)
  1. public Clock ( int hours, boolean isTicking, Integer diff )
  2. {
  3. this.hours = hours;
  4. this.isTicking = isTicking;
  5. this.diff = diff;
  6. }
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:
Java Syntax (Toggle Plain Text)
  1. public Clock ( int hours, boolean isTicking, Integer diff )
  2. {
  3. this.hours = hours;
  4. this.isTicking = isTicking;
  5. this.diff = diff.clone();
  6. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 10th, 2004
0

Re: Help w/ constructors

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!!
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Oct 10th, 2004
0

Re: Help w/ constructors

Show us the class, I'm tired of playing this guessing game.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Random Java Info
Next Thread in Java Forum Timeline: Don't know how to proceed with my program.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC