Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > General Coding
You last visited: Today at 09:04

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



HELP WITH JAVA

Discussion on HELP WITH JAVA within the General Coding forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Oct 2006
Posts: 45
Received Thanks: 14
HELP WITH JAVA

Directions:

Create your own PiggyBank class. This class will be used to create PiggyBank objects; therefore, everyone can have their own piggy bank! When your personal bank is created (instantiated), it may be stuffed with a certain number of pennies, nickels, dimes, and quarters. You will be able to access the number of each type of coin deposited into the bank as well as the bank’s total dollar value. You will also need to create methods that will allow the piggy bank to receive coins and update the appropriate values.
Here are some thoughts about what your class needs:

An instance variable for each of the following coins: pennies, nickels, dimes and quarters.

Constructors. There should be a default constructor that can create a brand new piggy bank without anything in it as well as a constructor that can accept initial values of each coin denomination. Would there be any other constructors that could come in handy?

Getter methods for returning the number of each type of coin currently in the bank.

A method or methods that will receive more coins and add them to the total. Think about the easiest way for your client to input the values. Would it be easiest to have one method that inputs all the values at once? What if the client only has a bunch of nickels? Or nickels and quarters? Keep these thoughts in mind when designing your methods.

A method to calculate the total value of the coins currently in the bank. Should this method print the value directly, or should it simply return the value and let the client deal with the data?

What I have
Code:
public class PiggyBank
{
    // instance variables - replace the example below with your own
    private double pennies;
    private double nickels;
    private double dimes;
    private double quarters;
    private double myBalance;
    private int c;
    /**
     * Constructor for objects of class PiggyBank
     */
    public PiggyBank(int coin)
    {
        c = coin;

    }
    
    public PiggyBank(double p, double n, double d, double q)
    {
        p = pennies;
        n = nickels;
        d = dimes;
        q = quarters;
    }

    public void CoinReturn()
    {
        quarters = c / 25;
        System.out.println("Quarter(s) " + quarters);
        c -= quarters * 25;
        dimes = c / 10;
        System.out.println("Dime(s) " + dimes);
        c -= dimes * 10;
        nickels = c / 5;
        System.out.println("Nickel(s) " + nickels);
        c -= nickels * 5;
        pennies = c / 1;
        System.out.println("Penny(s) " + pennies);   
    }
    
    public void deposit(double amount)
    {
        myBalance += amount;
    }
    
    public void withdraw(double amount)
    {
        myBalance -= amount;
    }
    
    public double getBalance()
    {
        return myBalance;
    }
}
EatMyChidori is offline  
Old 10/02/2012, 14:49   #2
 
elite*gold: 0
Join Date: Jul 2010
Posts: 388
Received Thanks: 196
Your design is utterly flawed.

1. You use c to calculate output, but c is set optionally. If PiggyBank(int coint) is never called c is zero.

2. You chose double for pennies, nickles, dimes and quarters. Why? They represent the number of coins, you can't have 3.7 quarters in your piggybank. Make them an integer.

3. Your deposit method only allows a double for whole dollar value, and your myBalance member doesn't take into account the pennys, nickles, dimes and quarters that might be added with the constructor.

4.

Code:
public PiggyBank(double p, double n, double d, double q)
    {
        pennies = p;
        nickels = n;
        dimes = d;
        quarters = q;
    }
SmackJew is offline  
Reply


Similar Threads Similar Threads
[JAVA Error] Could not create the java virtual machine
07/21/2013 - Technical Support - 10 Replies
Schönen Abend! Leider hat es sich aus einem unerfindlichen Grund ergeben, dass sobald ich die Minecraft.exe starten will die Errormeldung kommt. Die Tips auf Minecraft.net habe ich schon ohne Erfolg befolgt. Hoffe ihr könnt mir weiterhelfen... Mein PC:
[Java] Could not create the Java virtual machine
06/22/2011 - Minecraft - 1 Replies
hallo ihr minecraftler ^^ habe seit heute das problem das wenn ich minecraft starte original als auch cracked das diese fehlermeldung kommt: Java virtual machine Launcher Could not create the Java virtual machine



All times are GMT +2. The time now is 09:04.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.