Results 1 to 6 of 6
  1. #1
    wannabe is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Question 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

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  3. #3
    Chetans is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Thumbs down Bad coding style.

    First of all try to code properly.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by r035198x View Post
    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 10:12 AM.

  5. #5
    wannabe is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Chetans View Post
    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 .

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by wannabe View Post
    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.

Similar Threads

  1. Help me!!!!!!!! Problem with Code!!
    By Miyaki in forum New To Java
    Replies: 0
    Last Post: 03-08-2009, 01:26 AM
  2. wat is the problem in the code ?
    By Shyam Singh in forum New To Java
    Replies: 5
    Last Post: 08-14-2008, 11:34 PM
  3. Problem with code
    By jvasilj1 in forum New To Java
    Replies: 5
    Last Post: 02-02-2008, 09:34 AM
  4. Problem with code
    By oregon in forum New To Java
    Replies: 3
    Last Post: 08-05-2007, 06:57 PM
  5. Problem with zero in my code
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 07:39 AM

Posting Permissions

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