Results 1 to 6 of 6
Thread: problem in my code
 03232010, 03:45 AM #1Member
 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;
}
String add(mcmLength L2){
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("\nSum : " + Lt1.add(Lt2));
System.out.println("\nLine 1 : " + Lt1);
System.out.println("\nDiff : " + Lt1.sub(Lt2));
System.out.println("Area = : " + Lt1.Area(Lt2) + " sq.m.");
}
}
 please help :o
 03232010, 07:32 AM #2Senior 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.
 04082010, 08:22 AM #3Member
 Join Date
 Feb 2010
 Posts
 15
 Rep Power
 0
Bad coding style.
First of all try to code properly.
 04082010, 08:58 AM #4
 04122010, 01:43 AM #5Member
 Join Date
 Mar 2010
 Posts
 2
 Rep Power
 0

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.
Similar Threads

Help me!!!!!!!! Problem with Code!!
By Miyaki in forum New To JavaReplies: 0Last Post: 03082009, 12:26 AM 
wat is the problem in the code ?
By Shyam Singh in forum New To JavaReplies: 5Last Post: 08142008, 10:34 PM 
Problem with code
By jvasilj1 in forum New To JavaReplies: 5Last Post: 02022008, 08:34 AM 
Problem with code
By oregon in forum New To JavaReplies: 3Last Post: 08052007, 05:57 PM 
Problem with zero in my code
By fernando in forum New To JavaReplies: 1Last Post: 08052007, 06:39 AM
Bookmarks