Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

I have a problem for my return in the output

Thread view: 
dbstyless@hotmail.com - 30 Apr 2008 01:43 GMT
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;
    }
}
Chase Preuninger - 30 Apr 2008 02:12 GMT
make sure you are not multiplying by zero
Nigel Wade - 30 Apr 2008 09:28 GMT
> 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.
[quoted text clipped - 89 lines]
>        }
> }

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.

Signature

Nigel Wade

Mark Space - 30 Apr 2008 09:49 GMT
> public int getTotalValue()
>     {
>         Total = Dim + Nick + Pen + Quart;
>         return Total ;
>     }

Maybe you should call getDimeValue() and friends etc. here before you
total these up....
Sabine Dinis Blochberger - 30 Apr 2008 10:18 GMT
> 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 {

I think you need a class Coin not Coins. The Purse class is who keeps
track of its contents, IMO.

Signature

Sabine Dinis Blochberger

Op3racional
www.op3racional.eu



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.