We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,731 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Coin Problem java

Implement in Java a class Coin that describes a coin with a name (e.g. dime) and a value (e.g. 10 cents).

Also implement a class Wallet that describes a collection of coins. Supply a method add() to add a coin to a purse, and a method printContent() that prints out to standard output the contents of a purse in the following format:
coinName (coinValue): numberOfCoins

Example

penny (1 cent): 11
dime (10 cents): 3
nickel (5 cents): 2
euro (100 eurocents): 5
In order to test your Wallet class you'll also implement a class WalletTest whose main() method does the following:

Constructs an empty wallet
Prompts the user to enter the name of a coin (e.g. dime), or an empty string to quit
Prompts the user to enter the value of a coin (e.g. 10 cents)
Adds the coin to the wallet
Prints the contents of the wallet
Goes to the 2nd bullet point above

5
Contributors
6
Replies
1 Week
Discussion Span
3 Months Ago
Last Updated
14
Views
mehmud
Newbie Poster
3 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

And? Where is your code so far? Have you tried to do something?
Our members and rules topic says this:
Do provide evidence of having done some work yourself if posting questions from school or work assignments.

We won't do your homework for you, we'll help you on a specific point from your homework, and which gives you trouble.

Come back when you have something specific to ask, rather than "heres the problem... I'm waiting for the full solution."

Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 11

your first java-homework ... I'm soooooo proud!!
they do grow up so fast, don't they?

stultuske
Industrious Poster
4,370 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 23
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serviceClass;

/**
 *
 * 
 */
public class Coin {
    String name="";
    String value="";
    public Coin(){
        name="";
        value="";
    }
    public Coin(String name1,String value1){
        name=name1;
        value=value1;
    }
    public void setName(String name1){
       name=name1;
        }
     public void setValue(String value1){
       value=value1;
        }
     public String getName(){
         return name;
     }
     public String getValue(){
         return value;
     }
     public String toString(){
         return "The coin name is "+name+" and the value is "+value;
     }

}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serviceClass;

/**
 *
 * 
 */
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
public class Wallet {
     int coinNumber=0;
   Coin c=new Coin();
   String a="penny";
   String b="dime";
   String d="quarters";
   String name="";
    String value="";

   public Wallet(){
       name="";
        value="";
   }
   public Wallet(String name1,String value1){
        name=name1;
        value=value1;
    }


    public void setName(String name1){
       name=name1;
        }
     public void setValue(String value1){
       value=value1;
        }
     public String getName(){
         return name;
     }
     public String getValue(){
         return value;
     }
    public void add(){
      coinNumber++;  
    }
    String[] purse=new String[coinNumber];
    public void printContent(){
        System.out.println( name +"("+value+")"+":" +coinNumber);
    }
    public boolean equals(Coin c){
        if(this.getName().equals(c.getName()) && this.getValue().equals(c.getValue())){
            return true;
        }
        return false;
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serviceClass;

import java.util.Scanner;

/**
 *
 * 
 */
public class WalletTest {

  public static void main(String[] args){
      Wallet w=new Wallet();
      boolean b=true;
      Scanner scan=new Scanner(System.in);
       Scanner scan1=new Scanner(System.in);
      System.out.println(" Enter the name of the coin or an empty string to quit>");
      String name=scan.next();
      while(!name.equals("")){
      System.out.println(" Enter the value of the coin> ");
      String value=scan1.next();

      w=new Wallet(name,value);
      w.add();
      w.printContent();
      System.out.println(" Enter the name of the coin or an empty string to quit>");
      name=scan.next();
  }  
  }
}
mehmud
Newbie Poster
3 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

It's strange, but from the requirements of WalletTest it seems that you will need to keep track of possible 2-cent pennies and 7-cent dimes. But at least there's no reason to change the name or value of a coin, so you shouldn't have those setters in Coin. Instead you should add an Coin.equals and a Coin.hashCode so that you can use your coins as keys in a java.util.HashMap<Coin,Integer>.

You are creating a new Wallet every time through the loop but your instructions say to only do that once at the beginning. Your Wallet.add method should take a coin as an argument, otherwise it won't know what to add to the wallet.

bguild
Posting Whiz
330 posts since Oct 2012
Reputation Points: 166
Solved Threads: 73
Skill Endorsements: 7

Should wallet extend coin?

mehmud
Newbie Poster
3 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Should wallet extend coin?

Whenever in doubt apply the IS A-test: Wallet IS A Coin? (or rephrased: IS Wallet A Coin?) If the answer to that question is no, then you shouldn't.

mvmalderen
Posting Maven
2,612 posts since Feb 2009
Reputation Points: 2,221
Solved Threads: 280
Skill Endorsements: 36

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0758 seconds using 2.75MB