Results 1 to 4 of 4
Thread: Class object mistake
- 10-07-2011, 06:26 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
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 13Last edited by storm88; 10-07-2011 at 07:07 PM.
- 10-07-2011, 08:10 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Re: Class object mistake
a) Please use the code tags. b) please post an SSCCE demonstrating the problem (this code is not compilable).
- 10-09-2011, 01:56 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
Re: Class object mistake
what is SSCCE?
- 10-10-2011, 12:41 AM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Similar Threads
-
Drawing an object in my canvas class, the object is created in a separate class
By Hornfreak in forum AWT / SwingReplies: 3Last Post: 05-02-2011, 04:37 AM -
Insert class file as object in a table & read the object from the blob.
By facemeguru in forum New To JavaReplies: 1Last Post: 02-02-2011, 06:11 PM -
Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 04:29 AM -
add object to ArrayList (object is from extends other class)
By mBull in forum Java AppletsReplies: 3Last Post: 03-15-2010, 08:44 PM -
Creating object of Type Object class
By venkatv in forum New To JavaReplies: 3Last Post: 07-17-2007, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks