5 Replies - 7349 Views - Last Post: 27 September 2008 - 09:18 AM Rate Topic: -----

#1 nrp88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 19-September 08

Methods with parameters + set and get methods.

Posted 26 September 2008 - 11:32 AM

I'm frustrated! How can I get this code to work?

The task is to: 1. Add an attribute to the "Mammal" class called "Weight" and also add methods that set and get that weight value.

2. Also, in the "MainApplication" class, invoke the 'set' and 'get' methods.

3. Make sure that the weight I set the attribute to prints out.

Here is the original code:
class MainApplication {

	public static void main(String args[]) {

		Mammal dog = new Mammal();
		dog.setHairColor("Blue");

	}
}

// Class Mammal
public class Mammal {

	private String hairColor;

   	public void growHair(){
	 	   System.out.println("Hair Growing");
	}

   	public void setHairColor(String color){
	 	   hairColor = color;
	}


} 



And here is my attempt:
class MainApplication {

	public static void main(String args[]) {

		Mammal dog = new Mammal();
		dog.setHairColor("Blue");

		dog.setWeight(50);
	}
}

// Class Mammal
public class Mammal {

	private String hairColor;
	
			
   	public void growHair(){
	 	   System.out.println("Hair Growing");
	}

   	public void setHairColor(int color){
	 	   hairColor = color;
	}

	private String Weight;
	
	public void dog.setWeight(int weight){
		   dog.getWeight = weight;
	}

}


:angry: WHAT AM I DOING WRONG! :/ Am I close?
Please explain to me what I need to fix/add. When I compile it (in JCreator LE) , the errors messages I am receiving are:
line 28 : "(" expected
line 28 : invalid method declaration; return type required.


Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Methods with parameters + set and get methods.

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Methods with parameters + set and get methods.

Posted 26 September 2008 - 12:22 PM

Follow the example given to you, it is much easier than you are making it.
private String Weight;
    
    public void dog.setWeight(int weight){
           dog.getWeight = weight;
    }


should be:
//variables should start with lowercase letters, unless a static variables, in which case all letters would be uppercase
private int weight; //you had type string, but the method accepts an int
    
    public void dog.setWeight(int w){ //try not to use the same variable name in the function and the class
           weight = w; //inside the class you have access to the weight variable directly, there is no need to use the get method, besides a get method returns a value, which will cause an error.
    }



Get methods, return private variable values, and set methods change those private variable contents. There are very few circumstances where a get or set method should call the other.

Also you have not created a getWeight method, so it would be impossible for the code to know what you are trying to do when calling it.

This post has been edited by William_Wilson: 26 September 2008 - 12:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Methods with parameters + set and get methods.

Posted 26 September 2008 - 01:40 PM

View Postnrp88, on 26 Sep, 2008 - 11:32 AM, said:

And here is my attempt:

:angry: WHAT AM I DOING WRONG! :/ Am I close?
Please explain to me what I need to fix/add. When I compile it (in JCreator LE) , the errors messages I am receiving are:
line 28 : "(" expected
line 28 : invalid method declaration; return type required.


Thanks in advance!


Your 2 methods should almost be clone but one difference:
- HairColor is a String
- Weight is a int

class MainApplication {

	public static void main(String args[]) {

		Mammal dog = new Mammal();
		dog.setHairColor("Blue");
		dog.setWeight(50);
	}
}

// Class Mammal
public class Mammal {

	private String hairColor;
	private int Weight;	
			
		public void setHairColor(String color){
	 	   hairColor = color;
	}
		public String getHairColor(){
	 	   return color;
	}
	
	public void setWeight(int w){
		   Weight = w;
	}
	public int getWeight(){
		   return Weight;
	}



}

Was This Post Helpful? 0
  • +
  • -

#4 nrp88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 19-September 08

Re: Methods with parameters + set and get methods.

Posted 26 September 2008 - 02:57 PM

View PostWilliam_Wilson, on 26 Sep, 2008 - 12:22 PM, said:

Follow the example given to you, it is much easier than you are making it.
private String Weight;
    
    public void dog.setWeight(int weight){
           dog.getWeight = weight;
    }


should be:
//variables should start with lowercase letters, unless a static variables, in which case all letters would be uppercase
private int weight; //you had type string, but the method accepts an int
    
    public void dog.setWeight(int w){ //try not to use the same variable name in the function and the class
           weight = w; //inside the class you have access to the weight variable directly, there is no need to use the get method, besides a get method returns a value, which will cause an error.
    }



Get methods, return private variable values, and set methods change those private variable contents. There are very few circumstances where a get or set method should call the other.

Also you have not created a getWeight method, so it would be impossible for the code to know what you are trying to do when calling it.


Alright, I have re-done the code to do what you said by changing the string to int, but it still will not compile correctly.

here is my new code; what is wrong with it that it will not compile? I'm still getting the same error messages.

class MainApplication {

	public static void main(String args[]) {

		Mammal dog = new Mammal();
		dog.setHairColor("Blue");

		dog.setWeight(50);
	}
}

// Class Mammal
public class Mammal {

	private String hairColor;
	
	private int weight;
	
			
   	public void growHair(){
	 	   System.out.println("Hair Growing");
	}

   	public void setHairColor(int color){
	 	   hairColor = color;
	}
	
	public void dog.setWeight(int w){
		   weight = w;
	}

}

Was This Post Helpful? 0
  • +
  • -

#5 nrp88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 19-September 08

Re: Methods with parameters + set and get methods.

Posted 27 September 2008 - 08:06 AM

View Postnrp88, on 26 Sep, 2008 - 02:57 PM, said:

View PostWilliam_Wilson, on 26 Sep, 2008 - 12:22 PM, said:

Follow the example given to you, it is much easier than you are making it.
private String Weight;
    
    public void dog.setWeight(int weight){
           dog.getWeight = weight;
    }


should be:
//variables should start with lowercase letters, unless a static variables, in which case all letters would be uppercase
private int weight; //you had type string, but the method accepts an int
    
    public void dog.setWeight(int w){ //try not to use the same variable name in the function and the class
           weight = w; //inside the class you have access to the weight variable directly, there is no need to use the get method, besides a get method returns a value, which will cause an error.
    }



Get methods, return private variable values, and set methods change those private variable contents. There are very few circumstances where a get or set method should call the other.

Also you have not created a getWeight method, so it would be impossible for the code to know what you are trying to do when calling it.


Alright, I have re-done the code to do what you said by changing the string to int, but it still will not compile correctly.

here is my new code; what is wrong with it that it will not compile? I'm still getting the same error messages.

class MainApplication {

	public static void main(String args[]) {

		Mammal dog = new Mammal();
		dog.setHairColor("Blue");

		dog.setWeight(50);
	}
}

// Class Mammal
public class Mammal {

	private String hairColor;
	
	private int weight;
	
			
   	public void growHair(){
	 	   System.out.println("Hair Growing");
	}

   	public void setHairColor(int color){
	 	   hairColor = color;
	}
	
	public void dog.setWeight(int w){
		   weight = w;
	}

}



Please help?! :(
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Methods with parameters + set and get methods.

Posted 27 September 2008 - 09:18 AM

The problem appears to be with this code

    
    public void dog.setWeight(int w){
           weight = w;
    }

}



Get rid of dog.setWeight and replace it with setWeight. In your Mammal class it doesnt know what dog is because it's just the Mammal class. You inherit from that in your main class to create a dog class. Try this

class MainApplication {

    public static void main(String args[]) {

        Mammal dog = new Mammal();
        dog.setHairColor("Blue");

        dog.setWeight(50);
    }
}

// Class Mammal
public class Mammal {

    private String hairColor;
    
    private int weight;
    
            
       public void growHair(){
            System.out.println("Hair Growing");
    }

       public void setHairColor(int color){
            hairColor = color;
    }
    
    public void setWeight(int w){
           weight = w;
    }

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1