This site requires JavaScript, please enable it in your browser!
Greenfoot back
2woodsway
2woodsway wrote ...

2011/11/14

Last Question - I am trying to find a tutor

2woodsway 2woodsway

2011/11/14

#
Assume there is a class AirConditioner that supports the following behaviors: turning the air conditioner on and off. The following methods are provide this behavior: turnOn and turnOff . Both methods take no arguments and return no value. Assume there is a reference variable myAC to an object of this class, which has already been created. Using the reference variable, invoke a method to tell the air conditioner object to turn on. So, as a result of your code executing, the turnOn() method of the AirCondition object that myAC refers to will be invoked. I've tried this and got lost, I am sorry to be a pest, but can someone please save me from drowning? public class AirConditioner { public static final int OFF = 0; public static final int ON = 1; int temperature; int status; public AirConditioner() { status = AirConditioner.OFF; temperature = 0; } public void turnOn() { status = AirConditioner.ON; } public void turnOff() { status = AirConditioner.OFF; } public void setTemp(int temp) { temperature = temp; } pulic static void main(String args) { AirConditioner officeAC = new AirConditioner(); } }
danpost danpost

2011/11/15

#
The setup of the class AirConditioner is pretty well laid out. Two things though, I do not think the last method 'public static void main(String args)' should be included here (if you are trying to create another instance of AirConditioner, do it in the World class); and within a few of the other methods you have 'AirConditioner.' (with the period, followed by a value), that should just be the value: not 'AirConditioner.OFF', but just 'OFF' (or just 'ON'). Now, in your World class where you have a reference to the AC in question ('myAC'), you can call its methods as such: myAC.turnOn(); or myAC.setTemp(20); You could have more than one AirConditioner, referencing both in the World class, and work with each one separately. Let's say you created another one and reference it with the name 'officeAC'. Then, 'officeAC.setTemp(18);' would only affect the instance of the officeAC, and 'myAC.turnOff();' would turn the instance of myAC off.
You need to login to post a reply.