my code displaying some minor wrong o/p ....plz suggest me an answer !
its a simple program thats return the weight of the dog bt whenever any one entered a negative weigh it ll also display that as a positive weight
for eg if enter -50 it ll show you +50 or 50 ....but it is just repeating the 1st entered weight i.e 45
so if u entered 45 once and -80 on second time the o/p should be 45 & 80 not 45 and 45
plz tell me whats wrong in the code so that i can learn
thnx in advance
Code :
public class Dog {
private int weight;
public int getweight() {
return weight;
}
public void setweight(int newweight){
if (newweight > 0){
weight = newweight;
}
}
}
Code :
public class TestDog {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Dog d = new Dog();
//System.out.print("the weight of the dog is " + d.getWeight());
d.setweight(45);
System.out.print(" the weight of the dog is " + d.getweight());
String newLine = System.getProperty("line.separator");
System.out.println("" + newLine + "");
d.setweight(-25);
System.out.print("the weight of the dog is " + d.getweight());
}
}
Re: my code displaying some minor wrong o/p ....plz suggest me an answer !
Please see the link in my signature on asking smart questions, especially the part on not using abbreviations and SMS speak. I can barely understand you, and I'm a native English speaker.
Have you run through your code with a debugger to figure out what's going on? Boil your problem down to an example that's a few lines long (an SSCCE), and we'll go from there.
Re: my code displaying some minor wrong o/p ....plz suggest me an answer !
Code :
public void setweight(int newweight){
if (newweight > 0){
weight = newweight;
}
}
If newweight is greater than zero, you update the weight of the dog. Cool!
However, what if the weight is equal to or less than zero? You haven't told the method what to do with those values.
Re: my code displaying some minor wrong o/p ....plz suggest me an answer !
ok ! now i understand this ..thnx