Results 1 to 4 of 4
  1. #1
    storm88 is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Class object mistake

    public class Mone {

    static int dollars;
    static int cents;
    static int sum ;
    static int sum1;
    static int total;

    public Mone(int theGreen, int qcee)
    {
    dollars = theGreen;
    cents = qcee;
    }
    public Mone()
    {
    dollars = 0;
    cents = 0;
    }
    public Mone(int dollarqq)
    {
    dollars = dollarqq;

    }
    public static Mone add(Mone m1, Mone m2)
    {
    int d1 = m1.getDollars();
    int d2 = m2.getDollars();
    int c1 = m1.getCents();
    int c2 = m2.getCents();
    int sumDollars = dollars1 + dollars2;
    int totalCentss = c1 + c2;
    Mone output = new Mone(sumDollars, sumCents);
    return output;
    }


    public int getDollars()
    {
    return dollars;
    }
    public int getCents()
    {
    return cents;
    }
    public void setDollars(int theDollars)
    {
    dollars = theDollars;
    }
    public void setCents(int thecents)
    {
    cents = thecents;
    }


    public int add(Mone x)
    {
    int d1 = this.getDollars();
    int d2 = x.getDollars();
    int totalDollars = d1 + d2;
    int c1 = this.getCents();
    int c2 = x.getCents();
    int totalDollars = d1 + d2;
    int totalCents = c1 + c2;
    double totalC = totalCents / 100;

    return (double)totalDollars + totalC;
    }
    public String toString()
    {
    return ("$" + getDollars() + "." + getCents());
    }

    }

    public class Gw{

    public static void main(String[] args)
    {
    System.out.println("The calculation start");
    Mone my = new Mone(5);
    Mone other = new Mone(8);

    my.add(other);

    System.out.println("The total is");
    System.out.println(m3);
    }
    }

    What was the wrong there ? The code calculate with add(non static version) method , only see m2 object and calculate other(object name) object dollars + other(object name) object dollars and they do not know my object so result is 16 .It is a wrong result how can I make correct?The result should be 13
    Last edited by storm88; 10-07-2011 at 07:07 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Class object mistake

    a) Please use the code tags. b) please post an SSCCE demonstrating the problem (this code is not compilable).

  3. #3
    storm88 is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: Class object mistake

    what is SSCCE?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  2. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  3. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  4. Replies: 3
    Last Post: 03-15-2010, 08:44 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

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