Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: problem in my code

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question problem in my code

    I wrote this code to operate with lengths. However , the value of Lt1 changes when I call the add method. I am new to JAVA ; can't understand what's gone wrong.
    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
    Last edited by helloworld922; March 23rd, 2010 at 12:55 AM.


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Location
    Home-Ujjain /Job-Mumbai
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem in my code

    Hay look at this
    String add(mcmLength L2)
    	{
    		temp=(this.m+L2.m)*1000 + (this.cm+L2.cm)*10 + (this.mm +L2.mm);
    		[B][COLOR="Red"]m=temp/1000;                [COLOR="DarkSlateBlue"] //   The Problem is here, m is the variable of Lt1 and you are modifying it [/COLOR]
    		cm=(temp - m*1000)/10;                               [COLOR="RoyalBlue"]//   This is same as accessing this.cm [/COLOR]
    		mm=temp - m*1000 - cm*10;[/COLOR][/B]  
    		return Integer.toString(m) + " m " + cm + " cm " + mm + " mm" ;
    	}
    [COLOR="SeaGreen"][B]
    [/B][/COLOR]

    In this method m and this.m both are similar by mistake you are modifying the value of Lt1
    If you dont want to change the value of Lt1 then you should use some other local variables


    Good Luck
    Last edited by helloworld922; March 23rd, 2010 at 12:55 AM.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem in my code

    Thank you very much , Sunil !
    Now , it works. I had missed this point .

Similar Threads

  1. Problem with Recursive code
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 22nd, 2010, 09:36 PM
  2. Problem to organize my code in classes
    By lumpy in forum Java Theory & Questions
    Replies: 2
    Last Post: February 21st, 2010, 12:13 PM
  3. Re: Java Newbie Code Problem
    By erinbasim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:05 AM
  4. [SOLVED] Java Newbie Code Problem
    By lee in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2010, 03:05 PM
  5. code needed for the following problem
    By romilc in forum Java Theory & Questions
    Replies: 1
    Last Post: October 11th, 2009, 10:05 AM