• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • 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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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