Two Plus Two Older Archives

Two Plus Two Older Archives (http://archives2.twoplustwo.com/index.php)
-   Internet Gambling (http://archives2.twoplustwo.com/forumdisplay.php?f=26)
-   -   Java Help Needed (http://archives2.twoplustwo.com/showthread.php?t=192545)

Shilly 02-08-2005 11:54 PM

Java Help Needed
 
This is not applicable to poker, but I figured that a lot of people here would be able to help me out with this.

I'm in an Intro to Programming course that uses Java. For my Codelab assignments (little exercises online where you write a few lines of code, and they tell you if it is correct or not), I'm struggling with object creation. I'll list the instructions here:

Suppose there is a class AirConditioner . The class supports the following behaviors: turning the air conditioner on, off, and setting the desired temperature. The following methods are provided for these behaviors: turnOn and turnOff , which accept no arguments and return no value, and setTemp , which accepts an int argument and returns no value.

There is a reference variable officeAC of type AirConditioner . Create a new object of type AirConditioner using the officeAC reference variable. After that, turn the air conditioner on using the reference to the new object, and set the desired temperature to 69 degrees.


I know this will seem incredibly easy to someone with Java experience, but I'm having a mental block. I can't find anyting about it in this garbled textbook, and Codelab's suggestions are unhelpful.

Thanks in advance.

Prod1gy 02-09-2005 12:00 AM

Re: Java Help Needed
 
Little off topic but here goes. This is how I would do it.

private AirConditioner officeAC;
.
.
.
officeAC = new AirConditioner();
officeAC.turnOn();
officeAC.setTemp(69);
.
.
.

or you could do it like this. both do the same thing. Just the first example assumes that officeAC might be not be a local variable to a method.

AirConditioner officeAC = new AirConditioner();
officeAC.turnOn();
officeAC.setTemp(69);
.
.
.

moondogg 02-09-2005 12:06 AM

Re: Java Help Needed
 
[ QUOTE ]
Little off topic but here goes. This is how I would do it.

private AirConditioner officeAC;

.
.
.

officeAC = new AirConditioner();
officeAC.turnOn();
officeAC.setTemp(69);

.
.
.

[/ QUOTE ]

One slight difference in how I read the question
[ QUOTE ]
There is a reference variable officeAC of type AirConditioner . Create a new object of type AirConditioner using the officeAC reference variable.

[/ QUOTE ]

//Assumed officeAC has already been defined and initialized

//Not sure this is valid Java syntax, but they do ask for a "new" object
AirConditioner myAC = new AirConditioner(officeAC);
//If this isn't valid, use AirConditioner myAC = officeAC;

//The rest is the same
officeAC.turnOn();
officeAC.setTemp(69);

Edit: P.S. I hate Java. Learn C++ or C#

RED_RAIN 02-09-2005 12:19 AM

Re: Java Help Needed
 
Not to be a dick.

Just getting the answer won't help you in future java courses.

Especially if you are Csci major.

The stuff you are asking is extremely basic, if your book truely sucks that much, find a different one. I would suggest feeling comfortable with this stuff as it doesn't get easier than this.

Bytestream 02-09-2005 12:36 AM

Re: Java Help Needed
 
[ QUOTE ]
AirConditioner myAC = new AirConditioner(officeAC);

[/ QUOTE ]

I doubt his AirConditioner constructor is overloaded to take objects of type AirConditioner...

NoTalent 02-09-2005 02:55 AM

Re: Java Help Needed
 
There is no way your textbook is this bad. Please post the title and author.

This is the first thing you learn in OO programming. Search for "java object tutorial" or something on google and you can figure this out in 3 seconds...

Al P 02-09-2005 03:04 AM

Re: Java Help Needed
 
I forget java but in VB.NET

OfficeAC is a type of AirConditioner.

Dim OfficeAC as new AirConditioner()

Turn on Air

OfficeAC.turnOn()

Set Temp

OfficeAC.setTemp(69)

Turn Off

OfficeAC.turnOff()

Destroy it ala Office Space Style

OfficeAC = Nothing

It should look similar in java. It's not that hard really.

SamJack 02-09-2005 03:35 PM

Re: Java Help Needed
 
[ QUOTE ]
[ QUOTE ]
Little off topic but here goes. This is how I would do it.

private AirConditioner officeAC;

.
.
.

officeAC = new AirConditioner();
officeAC.turnOn();
officeAC.setTemp(69);

.
.
.

[/ QUOTE ]

One slight difference in how I read the question
[ QUOTE ]
There is a reference variable officeAC of type AirConditioner . Create a new object of type AirConditioner using the officeAC reference variable.

[/ QUOTE ]

//Assumed officeAC has already been defined and initialized

//Not sure this is valid Java syntax, but they do ask for a "new" object
AirConditioner myAC = new AirConditioner(officeAC);
//If this isn't valid, use AirConditioner myAC = officeAC;

//The rest is the same
officeAC.turnOn();
officeAC.setTemp(69);

Edit: P.S. I hate Java. Learn C++ or C#

[/ QUOTE ]

If we are entertaining toughts that AirConditioner class has contructors other than the default one.

officeAC = new AirConditioner(true); // constructor determines if the airconditioner is on or off at creation time.
officeAC.setTemp(69);

OR

officeAC = new AirConditioner(69); // creates the air conditioner with the temperature already set @ 69

moondogg 02-09-2005 03:54 PM

Re: Java Help Needed
 
Yeah, I thought Java may have that built-in as a copy constructor syntax. Apparently they don't, in which case I the alternate constructor code I put their should be used (IMHO)


All times are GMT -4. The time now is 02:37 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.