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

Recommended Answers

All 6 Replies

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."

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

commented: Loved the sarcasm :D +13
commented: bahahahaha +7
/*
 * 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();
  }  
  }
}

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.

Should wallet extend coin?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.