Results 1 to 12 of 12
  1. #1
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Post Help with constructor methods

    can anyone show me how you would use a constructor method in this code:
    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);
        }
    }
    ive been thinking of a way but i cant figure out how to put a "main()" it.
    here is what ive come up with:
    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);
        }
    im just stuck if anyone can help that would be great!:confused:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 01:07 AM.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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();
        }
    }

  4. #4
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    oh great!:D
    sorry im just beginning so im still learning

  5. #5
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    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:
    Java Code:
    public class newVolcanoRobot
    {
    	String status;
    	int speed;
    	float temperature;
    	
    	newVolcanoRobot(String in1, int in2, float in3)
    	{
    		status = "exploring";
    		speed = 12;
                              temperature = 600;
    	}
    maybe I just dont really know what a constructor method is?
    im sorry im asking things like this but if anyone could answer it would be great

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mustachMan View Post
    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:
    Java Code:
    public class newVolcanoRobot
    {
    	String status;
    	int speed;
    	float temperature;
    	
    	newVolcanoRobot(String in1, int in2, float in3)
    	{
    		status = "exploring";
    		speed = 12;
                              temperature = 600;
    	}
    maybe I just dont really know what a constructor method is?
    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:
    Java Code:
    	void newVolcanoRobot(String in1, int in2, float in3)
    	{
    		status = "exploring";
    		speed = 12;
                    temperature = 600;
    	}
    It wouldn't be a constructor since it returns something (even though that something is void).

    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;
    	}

  7. #7
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Thumbs up

    forget about the post before this one
    i figured it out thank you!

  8. #8
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    nevermind what i just said
    i tried what you said but i still have problems whit defining the new object
    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();
    		
    	  
    		
    		
    	}
    	
    }
    i cant figure out what to put at the certain spot:
    Java Code:
    newVolcanoRobot carl = new newVolcanoRobot(String, int, double);

  9. #9
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    sorry for all the trouble im just trying to learn

  10. #10
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    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.

    Java Code:
    void newVolcanoRobot(String myStatus, int mySpeed, float temp)
    	{
    		status = myStatus;
    		speed = mySpeed;
                    temperature = temp;
    	}
    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.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rdtindsm View Post
    The constructor should be public.
    This is often the case, but not always true, and in any case, in his situation it's not causing his errors.

    My experience is that, while a constructor is default void return type, ...
    No. A constructor has no return type, not void, not anything.

    To the original poster, you're calling your constructor wrong.

    This:
    Java Code:
    newVolcanoRobot carl = new newVolcanoRobot(String, int, double);
    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("Foo", 40, 500.0);

  12. #12
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Talking

    thank you i tried putting the "exploring, 12, 600.0"
    earlier before i fixed the constructor method so i thought it was wrong
    anyways thanks for all your help!! )

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Replies: 4
    Last Post: 04-03-2009, 07:36 AM
  3. Replies: 1
    Last Post: 07-20-2008, 05:42 PM
  4. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •