hi everyone, i need help with an assignment that i'm having trouble with.

I need to create a class called Money whose objects represent amounts of money, with two instance variables of type int for the dollars and cents in the amount of money

i got the constructors down but am having trouble with the static methods.

i need to make two static methods, one that adds two objects of type Money and the other that subtracts two objects of type Money.

I also need to add a second version of the methods for addition and subtraction which should have the same names as the static version but should use a calling object and a single argument(i am still thinking about this part but any suggestions would be very helpful)
I'll put my code below and thanks for any help

public class Money
{
    private int dollars;
    private int cents;

    public Money(int d, int c)
    {
        dollars = d;
        cents = c;
    }

    public Money(int da)
    {
        dollars = da;
        cents = 0;
    }

    public Money()
    {
        dollars = 0;
        cents = 0;
    }

    public static int add(int m3, int m4)
    {
        return (m3+m4);
    }

    public static int minus(int m1, int m2)
    {
        return (m1-m2);
    }

    public int getDollars()
    {
        return dollars;
    }

    public int getCents()
    {
        return cents;
    }

    public void setDollars(int dol)
    {
        dollars = dol;
    }

    public void setCents(int cen)
    {
        cents = cen;
    }

    public String toString()
    {
        return ("Dollar amount: " + dollars + "Cents amount: " + cents + dollars + "." + cents);
    }

    public boolean equals(Money yourMoney)
    {
        return ((this.dollars==yourMoney.dollars)&&(this.cents==yourMoney.cents));
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hint: Your method signature for the add method should look like this:

public static Money add(Money m3, Money m4)

The method should add the dollars and cents of each Money object and return another Money object. You will need to take into account what happens when you add 90 cents to 50 cents to the dollars and cents of the returned object.

A similar approach is required for your static minus method.

EDIT:
For the non-static methods, the following method signature is what is required:

public void add(Money m)

This method should add the dollars and cents values of m to the caller's dollars and cents. Similar for minus.

Have a go, repost if you need further help :)

thank you very much for your help
i understand what im doing so far but i need a little help with my driver program
How can i test these methods in my driver program
My code for the driver program is listed below and thank you for your help:

import java.util.Scanner;

public class driver1
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        Money good = new Money();
        System.out.println("Enter a dollar amount followed by a cents amount.");//i am confused after this line
        good.setDollars(keyboard.nextInt());//how can i get this to be a dollar amount to add
        good.setCents(keyboard.nextInt());// and this to be a cents amount

        System.out.println("The two objects added together equal: " + Money.add(dollars, cents));//using static method 
        System.out.println("The two objects subtracted equal: " + Money.minus(dollars, cents));

        System.out.println("Input two more money values.");
        int m6 = keyboard.nextInt();
        int m7 = keyboard.nextInt();
        System.out.println("The two objects added again equal: " + m6.add(m7));//using method with calling object but i think i should use this with static method
        System.out.println("The two objects subtracted equal: " + m6.minus(m7));

        System.out.println(good);

    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

im sorry to be a bother but i still dont understand how to work the add and minus method as a static or regular method
i modified my code below if you could help me see why i dont understand:

public class Money
{
    private int dollars;//instance variables
    private int cents;

    public Money(int d, int c)//constructor with two parameters for dollars and cents
    {
        dollars = d;
        cents = c;
    }

    public Money(int da)//constructor with one parameter for dollars
    {
        dollars = da;
        cents = 0;
    }

    public Money()//no-argument constructor
    {
        dollars = 0;
        cents = 0;
    }

    public static int add(int m3, int m4)//static method for addition
    {
        return (m3+m4);
    }

    public static int minus(int m1, int m2)//static method for subtraction
    {
        return (m1-m2);
    }

    public int add2(int a)//method using calling object for addition
    {
        return (dollars + a);
    }

    public int minus2(int c)//method using calling object for subtraction
    {
        return (dollars - c);
    }

    public int getDollars()//accessor method to get dollars
    {
        return dollars;
    }

    public int getCents()//accessor method to get cents
    {
        return cents;
    }

    public void setDollars(int dol)//mutator method to set amount of dollars
    {
        dollars = dol;
    }

    public void setCents(int cen)//mutator method to set amount of cents
    {
        cents = cen;
    }

    public String toString()//toString method
    {
        return ("Dollar amount: " + dollars + " Cents amount: " + cents);

    }

    public boolean equals(Money yourMoney)//equals method
    {
        return ((this.dollars==yourMoney.dollars)&&(this.cents==yourMoney.cents));
    }
}

driver program:
import java.util.Scanner;

public class driver1
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        Money good = new Money();
        System.out.println("Enter a dollar amount as a cents amount.");
        good.setDollars(keyboard.nextInt());
        System.out.println("Enter a cents amount.");
        good.setCents(keyboard.nextInt());

        System.out.println("Enter a number to add or subtract from the dollars object.");
        int p = keyboard.nextInt();
        System.out.println("These numbers added equal: " + good.add2(p));
        System.out.println("These numbers subtracted equal: " + good.minus2(p));

        System.out.println("Input two more money values.");
        int m6 = keyboard.nextInt();
        int m7 = keyboard.nextInt();
        System.out.println("The two objects added again equal: " + Money.add(m6, m7));//using static method to add two values
        System.out.println("The two objects subtracted equal: " + Money.minus(m6, m7));

        System.out.println(good);

    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Please use code tags when posting your code.

You have missed the point of my last post. You don't pass in two ints to your static add method, you pass in two objects of type Money, each of whom contain two int fields that need to be added together and returned as a Money object. The test for this would be like so:

Money m1 = new Money(5,30); // this one = $5.30
Money m2 = new Money(6,90); // this one = $6.90
Money m3 = Money.add(m1,m2); // m3 should = $12.20

For the non-static method, you need to create two Money objects and call the add method to add one to the other. The test would look like this:

Money m4 = new Money(5,30); // m4 = $5.30
Money m5 = new Money(6,90); // m5 = $6.90
m4.add(m5); // now m4 should = $12.20

so what would the method be like to make something like Money(5,30) be 5.30

would it be a string method where you would return the dollar amount + "." + cents?

No, it is your constructor with two parameters. You call it by saying new.

EDIT: Your constructor methods look ok, by the way. You just need to change your add and minus methods.

i am so sorry for acting stupid but what do you mean by calling it using new
i knew you had to use the first constructor but i dont know how to make it so that i could put the money in the form dollars.cents
and is this code ok now

public class Money
{
	private int dollars;//instance variables
	private int cents;
	
	public Money(int d, int c)//constructor with two parameters for dollars and cents
	{
		dollars = d;
		cents = c;
	}
	
	public Money(int da)//constructor with one parameter for dollars
	{
		dollars = da;
		cents = 0;
	}
	
	public Money()//no-argument constructor
	{
		dollars = 0;
		cents = 0;
	}
	
	public static Money add(Money m3, Money m4)//static method for addition
	{
	
	}
	
	public static Money minus(Money m1, Money m2)//static method for subtraction
	{
		
	}
	
	public int add2(Money a)//method using calling object for addition
	{
		
	}
	
	public int minus2(Money c)//method using calling object for subtraction
	{
		
	}
	
	public int getDollars()//accessor method to get dollars
	{
		return dollars;
	}
	
	public int getCents()//accessor method to get cents
	{
		return cents;
	}
	
	public void setDollars(int dol)//mutator method to set amount of dollars
	{
		dollars = dol;
	}
	
	public void setCents(int cen)//mutator method to set amount of cents
	{
		cents = cen;
	}
	
	public String toString()//toString method
	{
		return ("Dollar amount: " + dollars + " Cents amount: " + cents);
	
	}
	
	public boolean equals(Money yourMoney)//equals method
	{
		return ((this.dollars==yourMoney.dollars)&&(this.cents==yourMoney.cents));
	}
}

do i have to put anything in the add or minus methods?
thanks so much for your help

That looks ok, except that the add2 and minus2 methods should not return anything (ie void instead of int). Yes, you need to put something in your add and minus methods, but you need to figure out what. I wanted to give you a hint, and let you actually write the method yourself. I would change your toString method too:

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

Now when you print your Money object it will be in the form of $5.30 for example. You may need to change it for situations where cents < 10 so that it has a leading 0.

is this better now:

public class Money
{
	private int dollars;//instance variables
	private int cents;
	
	public Money(int d, int c)//constructor with two parameters for dollars and cents
	{
		dollars = d;
		cents = c;
	}
	
	public Money(int da)//constructor with one parameter for dollars
	{
		dollars = da;
		cents = 0;
	}
	
	public Money()//no-argument constructor
	{
		dollars = 0;
		cents = 0;
	}
	
	public static Money add(Money m3, Money m4)//static method for addition
	{
		return (Money m3 + Money m4);
	}
	
	public static Money minus(Money m1, Money m2)//static method for subtraction
	{
		return (Money m1 + Money m2);
	}
	
	public void add2(Money a)//method using calling object for addition
	{
		this.add(a);
	}
	
	public void minus2(Money c)//method using calling object for subtraction
	{
		this.minus(c);
	}
	
	public int getDollars()//accessor method to get dollars
	{
		return dollars;
	}
	
	public int getCents()//accessor method to get cents
	{
		return cents;
	}
	
	public void setDollars(int dol)//mutator method to set amount of dollars
	{
		dollars = dol;
	}
	
	public void setCents(int cen)//mutator method to set amount of cents
	{
		cents = cen;
	}
	
	public String toString()//toString method
	{
		return ("$" + getDollars() + "." + getCents());
	
	}
	
	public boolean equals(Money yourMoney)//equals method
	{
		return ((this.dollars==yourMoney.dollars)&&(this.cents==yourMoney.cents));
	}
}

im sorry again but i cant figure this stuff out
i wanted to use DecimalFormat for cents being less than 10 but i cant
thanks again for your help

When you use the code tags in the Java forum, where it says

, put [CODE=java] and it will give it Java highlighting and it will number the lines. This makes it easier for everyone to find and discuss things.

Also, I looked through your code, and I noticed one mistake (so far):

[CODE=Java]public static Money add(Money m3, Money m4)
	{
		return (Money m3 + Money m4);
	}

Money is your class name. The method header (the top line where it says public static Money...) needs to say 'Money' because it tells the program what type of Object to expect as a return type, and what type of Objects are going to be passed in to your method. But in the return statement, this is not allowed. But you have another error. . you cannot just add objects. m3 and m4 are 'Money' Objects. m3 has 'dollars' and 'cents' and so does m4. So add up those dollars and cents, then create a new Money Object using your constructor, then return that Money Object. If you do what I just suggested, and called the Money object "addedMoney", your code would look like this:

public static Money add(Money m3, Money m4)
	{

		//You do the adding of the dollars and cents here
//You create a new Money Object, addedMoney, here, using your constructor and the dollars and cents you just added.

return addedMoney;
	}

Your minus method has the same problem that I just described above. And your add2 method will not work either:

public void add2(Money a)//method using calling object for addition
	{
		this.add(a);
	}

Your add2 method calls your add method. Your add method requires 2 parameters (m3 and m4) as you can see from your method header. However, you attempted to call your add method, supplying only one parameter - "a". You have to call it using two.

Ok, I can see you are trying, I will show you what I would do for your add methods, then you can write the minus methods.

public static Money add(Money m1, Money m2)
{
  int dollars1 = m1.getDollars();
  int dollars2 = m2.getDollars();
  int cents1 = m1.getCents();
  int cents2 = m2.getCents();
  // now we have all of the info we need to add the two together
  int sumDollars = dollars1 + dollars2;
  int sumCents = cents1 + cents2;
  // now we need to check if we have too many cents
  if( sumCents > 100 )
  {
    sumDollars = sumDollars +1;
    sumCents = sumCents - 100;
  }

  Money output = new Money( sumDollars, sumCents );
  return output;
}

public void add(Money m)
{
  int dollars1 = this.getDollars();
  int dollars2 = m.getDollars();
  int cents1 = this.getCents();
  int cents2 = m.getCents();
  // now we have all our info
  int sumDollars = dollars1 + dollars2;
  int sumCents = cents1 + cents2;
  // again, check to see if we have too many cents
  if( sumCents > 100 )
  {
    sumDollars = sumDollars + 1;
    sumCents = sumCents - 100;
  }
  // now the purpose of this method is to update the caller's values of dollars and cents with new values
  this.setDollars( sumDollars );
  this.setCents( sumCents );
}

Have a go at the minus methods and let me know if you don't understand what I have done.

No problem. Set the thread to 'solved' if you don't have any more questions. And Darkagn, you should have let him work through it by himself.

No problem. Set the thread to 'solved' if you don't have any more questions. And Darkagn, you should have let him work through it by himself.

Well I thought I was doing that, eventually after (s)he couldn't get what I was trying to say I decided to do half of it so that they could do the rest, hopefully understanding it in the process.

This question has already been answered. Start a new discussion instead.