Results 1 to 12 of 12
Thread: Help with constructor methods
- 12-09-2009, 11:37 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
Help with constructor methods
can anyone show me how you would use a constructor method in this code:
ive been thinking of a way but i cant figure out how to put a "main()" it.Java Code:class NewVolcanoRobot { String status; int speed; float temperature; void checkTemperature() { if (temperature > 660) { status = "returning home"; speed = 5; } } void showAttributes() { System.out.println("Status: " + status); System.out.println("Speed: " + speed); System.out.println("Temperature: " + temperature); } }
here is what ive come up with:
im just stuck if anyone can help that would be great!:confused:Java Code:class NewVolcanoRobot { String status; int speed; float temperature; /* void checkTemperature() { if (temperature > 660) { status = "returning home"; speed = 5; } } */ NewVolcanoRobot(String in1, int in2, float in3) { status = in1; speed = in2; temperature = in3; } //then make an object to set the variables NewVolcanoRobot carl = new NewVolcanoRobot("roaming", 12, 500); void showAttributes() { System.out.println("Status: " + status); System.out.println("Speed: " + speed); System.out.println("Temperature: " + temperature); }
-
You may get more help if you acknowledge the help you've received in previous threads:
Snake Game
help with program please!
I suggest that you post some acknowledgment in those threads along with some follow up as to whether the suggestions helped you to solve your problems. We would all greatly appreciate that.
Much luck.Last edited by Fubarable; 12-10-2009 at 12:07 AM.
- 12-10-2009, 07:12 PM #3
Java Code:public class NVR { String status; int speed; float temperature; void checkTemperature() { if (temperature > 660) { status = "returning home"; speed = 5; } } void showAttributes() { System.out.println("Status: " + status); System.out.println("Speed: " + speed); System.out.println("Temperature: " + temperature); } // you can add a [i]main[/i] method into any class to test it public static void main(String[] args) { // create an instance of this class (NVR) and save a // reference to it in a local variable (app) which you // can use to access fields and call methods of the class // note: enclosing class needs [i]public[/i] modifier NVR app = new NVR(); // use the variable app to call methods in this instance // of the NVR class - note default values given by the // jvm (java virtual machine) at class construction app.showAttributes(); // set some field values app.status = "temperature is rising"; app.speed = 300; app.temperature = 1000f; // more method calls app.showAttributes(); app.checkTemperature(); } }
- 12-10-2009, 10:38 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
oh great!:D
sorry im just beginning so im still learning
- 12-15-2009, 11:31 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
actually im not really getting it
i know its a stupid question but which part of
the problem is the constructor method?
i thought a constructor method would be something like:
maybe I just dont really know what a constructor method is?Java Code:public class newVolcanoRobot { String status; int speed; float temperature; newVolcanoRobot(String in1, int in2, float in3) { status = "exploring"; speed = 12; temperature = 600; }
im sorry im asking things like this but if anyone could answer it would be great
-
Actually, there's no such animal as a "constructor method" because they are two separate things. Instead there are constructors and there are methods -- two distinctly different beasts. Your code above is in fact a constructor since it carries the same name as the class name, and since it doesn't return anything. For instance, if it looked like this:
It wouldn't be a constructor since it returns something (even though that something is void).Java Code:void newVolcanoRobot(String in1, int in2, float in3) { status = "exploring"; speed = 12; temperature = 600; }
The problem that I see with your constructor is that it simply ignores the parameters that are being passed to it: in1, in2, in3. Much better would be to use those parameters to update your class fields. For instance:
Java Code:void newVolcanoRobot(String in1, int in2, float in3) { status = in1; speed = in2; temperature = in3; }
Myself, I like variable and parameter names that have meaning, that give me an idea of what type of information they are holding. So I think that changing your parameter variables to something like this is better still:
Java Code:void newVolcanoRobot(String myStatus, int mySpeed, float temp) { status = myStatus; speed = mySpeed; temperature = temp; }
- 12-16-2009, 12:37 AM #7
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
forget about the post before this one
i figured it out thank you!
- 12-16-2009, 12:48 AM #8
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
nevermind what i just said
i tried what you said but i still have problems whit defining the new object
i cant figure out what to put at the certain spot:Java Code:public class newVolcanoRobot { String status; int speed; double temperature; newVolcanoRobot(String itsStatus, int itsSpeed, float itsTemperature) { status = itsStatus; speed = itsSpeed; temperature = itsTemperature; } void checkTemperature() { if (temperature > 660) { status = "returning home"; speed = 40; } } void increaseTemp() { temperature = 700; } void showAttributes() { System.out.println("\nStatus: " + status); System.out.println("Speed: " + speed); System.out.println("Temperature: " + temperature); } public static void main(String[] arguments) { newVolcanoRobot carl = new newVolcanoRobot(String, int, double); carl.showAttributes(); carl.increaseTemp(); carl.checkTemperature(); carl.showAttributes(); } }
Java Code:newVolcanoRobot carl = new newVolcanoRobot(String, int, double);
- 12-16-2009, 12:49 AM #9
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
sorry for all the trouble im just trying to learn
- 12-16-2009, 01:27 AM #10
Member
- Join Date
- Feb 2009
- Posts
- 92
- Rep Power
- 0
The constructor should be public. I would think that it would work without the public access, since since default visibility is protected and visible to the class and package. But I could very well be wrong, and probably am. Same package and same folder may not be the same thing.
Don't know if the constructor for Foo() would be available in TestFoo() and its main() method. Would look for some clarification from others if convenient. I can code it but not right now.
My experience is that, while a constructor is default void return type, it is a normal method if declared as void. I was having trouble with a constructor that wasn't being called that was declared public void Foo(). The answer I got on this forum was to remove the void. It worked.Java Code:void newVolcanoRobot(String myStatus, int mySpeed, float temp) { status = myStatus; speed = mySpeed; temperature = temp; }
-
This is often the case, but not always true, and in any case, in his situation it's not causing his errors.
No. A constructor has no return type, not void, not anything.My experience is that, while a constructor is default void return type, ...
To the original poster, you're calling your constructor wrong.
This:
makes no sense as String, int, and double are types, not variables or values. Much better would be to call it like so:Java Code:newVolcanoRobot carl = new newVolcanoRobot(String, int, double);
Java Code:newVolcanoRobot carl = new newVolcanoRobot("Foo", 40, 500.0);
- 12-16-2009, 02:28 AM #12
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM -
[SOLVED] Unable to access array defiened in constructor in other methods.
By Shyam Singh in forum New To JavaReplies: 1Last Post: 07-20-2008, 04:42 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
)

Bookmarks