1.11M Members

Java codes which i have no idea what's happening

 
0
 

i was wondering if someone can help me with this Java codes?

/**
 * This is a class of Money for us to be able to input an amount and do addition or substraction
 * to it and display the balance..
 * 
 * @author HUBERT JIANG 
 * @version 0.2
 */
public class Money
{
    // Create a instance variable for the Money Class
    private int _totalPence;
    // Create a instance variable for the currency
    private String _currency;

    /**
     * Create an amount of money in round pounds.
     * The constructor will use the amount of pounds and multiply it by 100 
     * to achieve the total number of pence.
     */
    public Money(int pounds)
    {
        // To get the total pence from the input amount
        _totalPence= pounds;
        _currency = "£";
    }

    /**
     * Create an amount of money in pounds and pence.
     */
    public Money(int pounds, int pence)
    {
        //To get the pounds fields and pence fields together
        _totalPence = pounds *100 +pence;
        _currency = "£";
    }

    /**
     *Create an amount in units, hundredths and a currency 
     */
    public Money(int units, int hundredths, String currency)
    {
        _totalPence= hundredths *100 + units;
        _currency=currency;
    }

    /**
     * Methods
     * Get the number of pounds, rounded down, eg"£42.99" returns 42
     */

    public int getPounds()
    {
        return _totalPence / 100;
    }

    /**
     * Get the number of pence left over by getPounds() eg "£42.99" returns 99.
     */
    public int getPence()
    {
        return _totalPence % 100;
    }

    /**
     * Get the total number ofpence, eg"£42.99" returns 4299
     */
    public int getTotalPence()
    {
        return _totalPence;
    }

    /**
     * Get the currency
     */
    public String getCurrency()
    {
        return _currency;
    }

    /**
     * Retrun the amount of money as a String, eg "£24.99"
     */
    public String toString()
    {
        // int pounds = (_totalPence /100);
        // int pence = (_totalPence %100);
        if (this.getPounds()>0 || this.getPence()>0)
        {
            return _currency + this.getPounds() + "." + this.getPence();
        }
        else
        {
            return "-"+ _currency + Math.abs(this.getPounds()) + "." + Math.abs(this.getPence());
        }
    }

    /**
     * This is to pull out the common code into a private helper method.
     */
    private Money makeMoney(int total){
        //convert total to pounds and pence,
        // and resturn a new Money object with those parameters.
        int pounds = (_totalPence/100);
        int pence = (_totalPence % 100);
        return new Money (pounds,pence);
    }

    /**
     * Add another amount of Money to this one, 
     * returning a new Money object and leaving both this and other unchanged.
     */
    public Money add(Money other)
    {
        _totalPence= _totalPence + other.getTotalPence();
        return this.makeMoney(_totalPence);
    }

    /**
     * Subtract another amount of Money to this one, 
     * returning a new Money object and leaving both this and other unchanged.
     */

    public Money subtract(Money other)
    {
        _totalPence= _totalPence - other.getTotalPence();
        return this.makeMoney(_totalPence);
    } 
}

I get 'ERROR: Cannot Find Symbol-Variable (anything I type here)' when I input a value using the constructor with currency. I am using BlueJ...not sure if the code i posted is correct but its the only codes i have and i am not very sure where is the error.

Thanks for help. Thanks Thanks

 
1
 

show your main method (or where you call/use your class).
also, I think you're getting a few logical errors there:

public Money(int pounds)
    {
        // To get the total pence from the input amount
        _totalPence= pounds;
        _currency = "£";
    }
 
    /**
     * Create an amount of money in pounds and pence.
     */
    public Money(int pounds, int pence)
    {
        //To get the pounds fields and pence fields together
        _totalPence = pounds *100 +pence;
        _currency = "£";
    }

according to your naming, and your comments in your class, the following lines should produce '5 == 5' as output. try it out:

Money mon = new Money(5);
Money aMon = new Money(5, 0);
System.out.println(mon.getPounds() + " == " + aMon.getPounds());
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: