# Thread: problem in my code

1. Member Join Date
Mar 2010
Posts
2
Rep Power
0

## problem in my code

I wrote the following code to operate on lengths. However, it seems the value of Lt1 changes after I call the add method . I'm new to Java ; can't figure out the problem. :confused:

public class mcmLength {

int m,cm,mm,temp,temp1,temp2;
mcmLength(int met,int cms,int mils){
m=met;
cm=cms;
mm=mils;
}

mcmLength(int mils){
m=mils/1000;
cm=(mils - m*1000)/10;
mm=mils - (m*1000) - (cm*10);
}

mcmLength(double cms){
temp=(int)cms*10;
m=temp/1000;
cm=(temp - m*1000)/10;
mm=temp - (m*1000) - (cm*10);
}

mcmLength(){
m=cm=mm=0;
}

double Area(mcmLength L2){
return (this.m*1000 + this.cm*10 + this.mm)*(L2.m*1000 + L2.cm*10 + L2.mm)/1000000;

}
temp=(this.m+L2.m)*1000 + (this.cm+L2.cm)*10 + (this.mm +L2.mm);
m=temp/1000;
cm=(temp - m*1000)/10;
mm=temp - m*1000 - cm*10;
return Integer.toString(m) + " m " + cm + " cm " + mm + " mm" ;
}
String sub(mcmLength L2){
temp1= this.m*1000 + this.cm*10 + this.mm ;
temp2= L2.m*1000 + L2.cm*10 + L2.mm;
temp=(Math.max(temp1,temp2)- Math.min(temp1,temp2));
m=temp/1000;
cm=(temp - m*1000)/10;
mm=temp - m*1000 - cm*10;
return Integer.toString(m) + " m " + cm + " cm " + mm + " mm " ;
}
public String toString(){
return Integer.toString(m) + " m " + cm + " cm " + mm + " mm ";
}
}

-------------------

public class Test {
public static void main(String[] args){
int i = (int)(Math.random()*10000);
double d = Math.random()*1000;
final mcmLength Lt1 = new mcmLength(i);
final mcmLength Lt2 = new mcmLength(d);
System.out.println("Line 1 : " + Lt1);
System.out.println("Line 2 : " + Lt2);
System.out.println("\nLine 1 : " + Lt1);
System.out.println("\nDiff : " + Lt1.sub(Lt2));
System.out.println("Area = : " + Lt1.Area(Lt2) + " sq.m.");
}

}  Reply With Quote

2. Senior Member Join Date
Aug 2009
Posts
2,388
Rep Power
14

## 1.) Please use code tags when posting code.
2.) Use meaningful variable names
3.) The add method changes the values of m,cm,and mm of Lt1. So you the change you see should be expected really.  Reply With Quote

3. Member Join Date
Feb 2010
Posts
15
Rep Power
0

## Bad coding style.

First of all try to code properly.  Reply With Quote

4. ##  Originally Posted by r035198x 1.) 3.) The add method changes the values of m,cm,and mm of Lt1. So you the change you see should be expected really.

that's the reason why Lt1 is changing. in the add method you change instance variables you use to calculate Lt1. so it's ok if the value of Lt1 change. or do you mean the input is 12.55 and the output is 1.255?
Last edited by j2me64; 04-08-2010 at 09:12 AM.  Reply With Quote

5. Member Join Date
Mar 2010
Posts
2
Rep Power
0

##  Originally Posted by Chetans First of all try to code properly.
I am a beginner and learning Java on my own. So I would really appreciate if I am helped with the concepts first , rather than being told to code properly. That will improve automatically .  Reply With Quote

6. ##  Originally Posted by wannabe I am a beginner and learning Java on my own. So I would really appreciate if I am helped with the concepts first , rather than being told to code properly. That will improve automatically .
1) Folks are telling your concepts here. If you don't understand what's been stated, just ask.

2) Meaningful variable names and code tags will not only help you, it will help us better understand your code and better be able to help you. Since advice here is free, it is in your best interest to do this -- to make it as easy as possible for others to help you. That way you're likely to get more help.

3) You might want to get a thicker skin. No one's out to insult you, and all the advice given so far has been in your best interest.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•