hey again i hate to be a bother but i still can't get this to work
my driver program(the second code) won't call the add method or the minus method to subtract input
i tried to do this in my code below and have failed
if you have any suggestions please i really need to make this work thank you for any help the class(first code) and the driver program are below

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
	{
		int dollars1 = m3.getDollars();
		int dollars2 = m4.getDollars();
		int cents1 = m3.getCents();
		int cents2 = m4.getCents();
		int sumDollars = dollars1 + dollars2;
		int sumCents = cents1 + cents2;
		
		if(sumCents >= 100)
		{
			sumDollars = sumDollars + 1;
			sumCents = sumCents - 100;
		}
		
		Money output = new Money(sumDollars, sumCents);
		return output;
	}
	
	public static Money minus(Money m1, Money m2)//static method for subtraction
	{
		int dollars1 = m1.getDollars();
		int dollars2 = m2.getDollars();
		int cents1 = m1.getCents();
		int cents2 = m2.getCents();
		int minusDollars = dollars1 - dollars2;
		int minusCents = cents1 - cents2;
		
		Money output1 = new Money(minusDollars, minusCents);
		return output1;
	}
	
	public void add2(Money a)//method using calling object for addition
	{
		int dollars1 = this.getDollars();
		int dollars2 = a.getDollars();
		int cents1 = this.getCents();
		int cents2 = a.getCents();
		int sumDollars = dollars1 + dollars2;
		int sumCents = cents1 + cents2;
		
		if(sumCents >= 100)
		{
			sumDollars = sumDollars + 1;
			sumCents = sumCents - 100;
		}
		
		this.setDollars(sumDollars);
		this.setCents(sumCents);
	}
	
	public void minus2(Money c)//method using calling object for subtraction
	{
		int dollars1 = this.getDollars();
		int dollars2 = c.getDollars();
		int cents1 = this.getCents();
		int cents2 = c.getCents();
		int minusDollars = dollars1 - dollars2;
		int minusCents = cents1 - cents2;
		
		this.setDollars(minusDollars);
		this.setCents(minusCents);
	}
	
	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));
	}
}
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.");
		good.setDollars(keyboard.nextInt());
		System.out.println("Enter a cents amount.");
		good.setCents(keyboard.nextInt());
		
		Money bad = new Money();
		System.out.println("Enter a dollar amount.");
		bad.setDollars(keyboard.nextInt());
		System.out.println("Enter a cents amount.");
		bad.setCents(keyboard.nextInt());
		
		Money ok = new Money();
		ok.add(good, bad);
		System.out.println(ok);
		ok.minus(good, bad);
		System.out.println(ok);
	}
}

Recommended Answers

All 9 Replies

Didn't we explain to you in the last thread how to use code tags? Under 'Go Advanced' there is a button that says code tags. Use it. Also add [CODE=Java] to the first part.

i did what you said but it still doesn't work
could you please just help me with my code now then ill fix my code tag skills later
i've tried so hard to make this code work and i still can't fix my driver program
thank you

First, you should access the static methods (add, minus) in a static way, like Money.add( ... ) and Money.minus ( ... ).

Accessing a static method on an object (ok) is not supposed to change the object, because it is a static method, you don't have to instantiate a class to call it.

What you return is not taken by ok to create an object. You should do:

ok = Money.add(good, bad);

And it should work.

thanks very much but can you give me a suggestion for the regular add and minus methods i tried to do

this.add2(bad)

but the program didnt compile
thank you so much for your help

The code tags are just the following:

for the first one and the last one is exactly what gets pasted in there when you click the button. So all you're adding is the =Java.

And you didn't set your previous thread to solved. While I don't mind helping you, these are both things that you should be doing out of courtesy.

On to the programming, I can come back and give you some more help tomorrow. For now, you might want to consider that "cents" can be much greater than 200. It looks like your code in the add method will not work if cents is > 200. (Lets say you had 0 dollars, 200 cents. Your code would convert that to 1 dollar, 100 cents - but it should convert it to 2 dollars.)[CODE=Java] for the first one and the last one is exactly what gets pasted in there when you click the button. So all you're adding is the =Java.

And you didn't set your previous thread to solved. While I don't mind helping you, these are both things that you should be doing out of courtesy.

On to the programming, I can come back and give you some more help tomorrow. For now, you might want to consider that "cents" can be much greater than 200. It looks like your code in the add method will not work if cents is > 200. (Lets say you had 0 dollars, 200 cents. Your code would convert that to 1 dollar, 100 cents - but it should convert it to 2 dollars.)

My interwebs freaked out. What do I do? (Triple post, sorry)

My internets freaked out. Triple post - sorry guys.

thanks very much but can you give me a suggestion for the regular add and minus methods i tried to do

this.add2(bad)

but the program didnt compile
thank you so much for your help

I looked at your add2 method and your add method. They both look like they will compile. What error are you getting? Post the entire error & your code again in code tags

On to the programming, I can come back and give you some more help tomorrow. For now, you might want to consider that "cents" can be much greater than 200. It looks like your code in the add method will not work if cents is > 200. (Lets say you had 0 dollars, 200 cents. Your code would convert that to 1 dollar, 100 cents - but it should convert it to 2 dollars.)

Actually, because javaman is only adding two Money objects, it is impossible to get to 200 cents (assuming that each Money object has a maximum of 99 cents).

However, the minus methods are still wrong. As I said in the last thread, what happens if you have $5.20 minus $1.90?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.