Solved

Cannot figure out what I am doing wrong

Posted on 2004-09-21
10
192 Views
Last Modified: 2010-03-31
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
Comment
Question by:biloonline
  • 5
  • 4
10 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 25 total points
ID: 12118101
public void setWeight(String theWeight) {
               weight = theWeight;
}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 25 total points
ID: 12118102
>>public void setWeight(theWeight) {

should be

public void setWeight(int theWeight) {

or maybe

public void setWeight(double theWeight) {



0
 
LVL 37

Expert Comment

by:zzynx
ID: 12118103
You forgot the type of the parameter
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 37

Expert Comment

by:zzynx
ID: 12118109
I think String since it is called like

dog1.setWeight("20 LB");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12118111
>>
public String getWeight(){
              return weight;
>>

should be

public String getWeight(){
              return weight;
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12118116
>>I think String

OK - but the key point is - you need a parameter *type*
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12118128
>> OK - but the key point is - you need a parameter *type*
That's for sure ;)
0
 

Author Comment

by:biloonline
ID: 12118159

>>
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12118194
8-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12118414
Thanks
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
what is a "java.lang.System Property"   ? 20 67
Way to decrease size of apk file 9 70
arguments to jar 5 26
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question