| | |
Help w/ constructors
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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!!!!!!!!!!!!!!!!!!!!!!!!!!!
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!!!!!!!!!!!!!!!!!!!!!!!!!!!
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)
myAC.turnOn();
New members chased away this month: 5
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!
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!
>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:
>P.S. I have 2 books
p.s. I don't know of any Java book that doesn't cover basic constructor usage.
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)
officeAC = new AirConditioner(); officeAC.turnOn();
p.s. I don't know of any Java book that doesn't cover basic constructor usage.
New members chased away this month: 5
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.
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.
>this.diff;
This doesn't do very much, maybe you should assign diff to it:
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:
This doesn't do very much, maybe you should assign diff to it:
Java Syntax (Toggle Plain Text)
public Clock ( int hours, boolean isTicking, Integer diff ) { this.hours = hours; this.isTicking = isTicking; this.diff = diff; }
Java Syntax (Toggle Plain Text)
public Clock ( int hours, boolean isTicking, Integer diff ) { this.hours = hours; this.isTicking = isTicking; this.diff = diff.clone(); }
New members chased away this month: 5
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!!
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!!
![]() |
Similar Threads
- Why constructors don't have return types (C++)
- Taking address of constructors?? (C++)
- C++ Tic Tac Toe using classes & operator overloading (C++)
- Confusion with constructors (Java)
- what is the effect of using constructors in the program? (Java)
- How to do constructors (Java)
Other Threads in the Java Forum
- Previous Thread: Random Java Info
- Next Thread: Don't know how to proceed with my program.
Views: 2081 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool keyword linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project projectideas read recursion reflection rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






