• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Cannot figure out what I am doing wrong

This should be fairly easy for someone who is not exhausted as I am.
Here is my code:

public class Hw2Pr3 {
      public static void main(String[] args) {
            Dog dog1 = new Dog();
            dog1.setWeight("20 LB");
            System.out.println("dog1's weight is " + dog1.getWeight());
            
            Dog dog2 = new Dog();
            dog2.setWeight("30 LB");
            System.out.println("dog2's weight is " + dog2.getWeight());
            
      
      }
}


class Dog {
      private String weight;
      
            public void setWeight(theWeight) {
                  weight = new String(theWeight);
            }
            
            public String getWeight(){
                  return weight;
            
      }      
}

It's giving me 2 errors:
--------------------Configuration: Hw2Pr2 - JDK version 1.5.0 <Default> - <Default>--------------------
C:\Program Files\Xinox Software\JCreatorV3 LE\MyProjects\Hw1Pr2\Hw2Pr2\Hw2Pr3.java:20: <identifier> expected
                public void setWeight(theWeight) {
                                               ^
C:\Program Files\Xinox Software\JCreatorV3 LE\MyProjects\Hw1Pr2\Hw2Pr2\Hw2Pr3.java:27: ')' expected
        }      
               ^
2 errors

Process completed.
0
biloonline
Asked:
biloonline
  • 5
  • 4
2 Solutions
 
zzynxSoftware engineerCommented:
public void setWeight(String theWeight) {
               weight = theWeight;
}
0
 
CEHJCommented:
>>public void setWeight(theWeight) {

should be

public void setWeight(int theWeight) {

or maybe

public void setWeight(double theWeight) {



0
 
zzynxSoftware engineerCommented:
You forgot the type of the parameter
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
zzynxSoftware engineerCommented:
I think String since it is called like

dog1.setWeight("20 LB");
0
 
CEHJCommented:
>>
public String getWeight(){
              return weight;
>>

should be

public String getWeight(){
              return weight;
}
0
 
CEHJCommented:
>>I think String

OK - but the key point is - you need a parameter *type*
0
 
zzynxSoftware engineerCommented:
>> OK - but the key point is - you need a parameter *type*
That's for sure ;)
0
 
biloonlineAuthor Commented:

>>
public String getWeight(){
              return weight;
>>

should be

public String getWeight(){
              return weight;
}

<--- That's exactly what I have.

Problem was I was missing the 'String' in public void setWeight(theWeight) {

Thanks guys again, you are a big help.
0
 
CEHJCommented:
8-)
0
 
zzynxSoftware engineerCommented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now