I have a problem for my return in the output

Discussion in 'Java' started by dbstyless@hotmail.com, Apr 30, 2008.

  1. Guest

    My program doesnt give me the output i expect it to give back. for the
    total value of the coins it doesnt give me the total.
    it just give a Zero as total value.
    i would like you to help me solve it . Thanks in advance.


    public class Coins {


    private static final int NICKEL = 5;
    private static final int DIME = 10;
    private static final int QUARTER = 25;
    private static final int PENNY = 1;
    private int Quart;
    private int Nick;
    private int Dim;
    private int Pen;
    private int Total;
    private int NoP;
    private int NoN;
    private int NoD;
    private int NoQ;


    public Coins(int P, int N, int D, int Q)
    {
    NoP = P ;
    NoN = N ;
    NoD = D ;
    NoQ = Q ;
    }

    public void addCoins (String Co, int C)
    {if (Co.equalsIgnoreCase ("PENNIES"))
    {

    NoP = NoP + C;

    }
    else if (Co.equalsIgnoreCase ("QUARTERS"))
    {
    Co = "Quarter";

    NoQ = NoQ +C;

    }
    else if (Co.equalsIgnoreCase ("NICKELS"))
    {
    Co = "Nickel";

    NoN =NoN +C;

    }
    else if (Co.equalsIgnoreCase ("DIMES"))
    {
    Co= "Dime";

    NoD = NoD +C;

    }

    }


    public void getPennyValue()
    {
    Pen = (NoP*PENNY);

    }
    public void getNickelValue()
    {
    Nick= (NICKEL*NoN);

    }
    public void getQuarterValue()
    {
    Quart = (QUARTER *NoQ);

    }
    public void getDimeValue()
    {
    Dim = (DIME*NoD);

    }
    public int getTotalValue()
    {
    Total = Dim + Nick + Pen + Quart;
    return Total ;
    }
    public String toString() {
    String str = "[";
    str = str + "Number of penny = " + NoP + ", Number of Nickel = " +
    NoN + ", Number of Dime = " + NoD +", Number of Quarter = " + NoQ + ",
    Total Value = " + Total +"]";
    return str;
    }
    }
    , Apr 30, 2008
    #1
    1. Advertising

  2. make sure you are not multiplying by zero
    Chase Preuninger, Apr 30, 2008
    #2
    1. Advertising

  3. Nigel Wade Guest

    wrote:

    > My program doesnt give me the output i expect it to give back. for the
    > total value of the coins it doesnt give me the total.
    > it just give a Zero as total value.
    > i would like you to help me solve it . Thanks in advance.
    >
    >
    > public class Coins {
    >
    >
    > private static final int NICKEL = 5;
    > private static final int DIME = 10;
    > private static final int QUARTER = 25;
    > private static final int PENNY = 1;
    > private int Quart;
    > private int Nick;
    > private int Dim;
    > private int Pen;
    > private int Total;
    > private int NoP;
    > private int NoN;
    > private int NoD;
    > private int NoQ;
    >
    >
    > public Coins(int P, int N, int D, int Q)
    > {
    > NoP = P ;
    > NoN = N ;
    > NoD = D ;
    > NoQ = Q ;
    > }
    >
    > public void addCoins (String Co, int C)
    > {if (Co.equalsIgnoreCase ("PENNIES"))
    > {
    >
    > NoP = NoP + C;
    >
    > }
    > else if (Co.equalsIgnoreCase ("QUARTERS"))
    > {
    > Co = "Quarter";
    >
    > NoQ = NoQ +C;
    >
    > }
    > else if (Co.equalsIgnoreCase ("NICKELS"))
    > {
    > Co = "Nickel";
    >
    > NoN =NoN +C;
    >
    > }
    > else if (Co.equalsIgnoreCase ("DIMES"))
    > {
    > Co= "Dime";
    >
    > NoD = NoD +C;
    >
    > }
    >
    > }
    >
    >
    > public void getPennyValue()
    > {
    > Pen = (NoP*PENNY);
    >
    > }
    > public void getNickelValue()
    > {
    > Nick= (NICKEL*NoN);
    >
    > }
    > public void getQuarterValue()
    > {
    > Quart = (QUARTER *NoQ);
    >
    > }
    > public void getDimeValue()
    > {
    > Dim = (DIME*NoD);
    >
    > }
    > public int getTotalValue()
    > {
    > Total = Dim + Nick + Pen + Quart;
    > return Total ;
    > }
    > public String toString() {
    > String str = "[";
    > str = str + "Number of penny = " + NoP + ", Number of Nickel = " +
    > NoN + ", Number of Dime = " + NoD +", Number of Quarter = " + NoQ + ",
    > Total Value = " + Total +"]";
    > return str;
    > }
    > }


    Your class relies on too much internal state.

    You have state for NoP, NoN etc., the number of each individual type of coin.
    You also have different state for Pen, Nick etc. the total value for each type
    of coin.

    These two types of state are not interlinked. When you add coins you only update
    the number of coins, you don't update the state for the total value of each
    coin. This is (rather peculiarly for internal state) only set by an external
    interface call, getPennyValue() etc. Either drop the duality of state and
    calculate the total from the coins, or update both state values when you add
    coins.

    As it stands your getTotalValue() method returns the value of the state
    variables for total coin value, which are only set if you previously call the
    methods getPennyValue() etc. first to set that state.

    --
    Nigel Wade
    Nigel Wade, Apr 30, 2008
    #3
  4. Mark Space Guest

    wrote:

    > public int getTotalValue()
    > {
    > Total = Dim + Nick + Pen + Quart;
    > return Total ;
    > }


    Maybe you should call getDimeValue() and friends etc. here before you
    total these up....
    Mark Space, Apr 30, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cmdyYW5kaWRpZXI=?=

    Can I have base class derived from System.Web.UI.Page have design

    =?Utf-8?B?cmdyYW5kaWRpZXI=?=, Jul 22, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    479
    Patrice
    Jul 22, 2005
  2. okaminer
    Replies:
    3
    Views:
    304
    okaminer
    Aug 1, 2005
  3. =?Utf-8?B?ZGF2aWQ=?=
    Replies:
    6
    Views:
    5,493
    Kevin Spencer
    Aug 18, 2005
  4. Greenhorn
    Replies:
    15
    Views:
    812
    Keith Thompson
    Mar 6, 2005
  5. Paul
    Replies:
    1
    Views:
    430
Loading...

Share This Page