Coin Class question

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 2965 Views - Last Post: 08 March 2013 - 09:48 PM Rate Topic: -----

#1 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Coin Class question

Posted 06 March 2013 - 09:32 AM

Howdy Folks;
I could use some help with making sure that I am on the right path of the assignment's directions(assignment is below). I have also began a rough outline of what I interpreted the instructions (see code below) as. I would deeply appreciate if you could answer this question:
Am I on the right path of and am I interpreting the instructions correctly or no?
Also if I am, is there any coding suggestions that you could make or
If I am wrong, could you please point me in the right direction?

ASSIGNMENT

Quote

/**
* Implement a class Coin that represents any coin.
* The class should store the name and the value of the coin
* provide appropriate methods to retrieve these two attributes
* Write a toString method that returns a string representation of the current value and name of the object.
* In addition, you should be able to clone objects of this class
* also to compare any two Coin objects.
* provide an implementation of the equals method that is compatible with the method equals of the class Object.
*
* You must also create a test program that test your class as $1.00, $0.50, $0.25, $0.10, $0.05, and $0.01 coins.
* Please make sure you are using GIT and you submit your compressed folder.
* Make sure you do a final git add and git submit when you are completely done with your program.
*/

MY ROUGH CODE
/**
 * Implement a class Coin that represents any coin.
 * The class should store the name and the value of the coin
 * provide appropriate methods to retrieve these two attributes 
 * Write a toString method that returns a string representation of the current value and name of the object. 
 * In addition, you should be able to clone objects of this class
 * also to compare any two Coin objects.
 * provide an implementation of the equals method that is compatible with the method equals of the class Object.
 * 
 * You must also create a test program that test your class as $1.00, $0.50, $0.25, $0.10, $0.05, and $0.01 coins.
 * Please make sure you are using GIT and you submit your compressed folder.
 * Make sure you do a final git add and git submit when you are completely done with your program.
 */
package programmingassignmentunit5;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @date 3/6/2013
 * @author k3y
 */
class ProgrammingAssignmentUnit5 {
    private static Scanner console = new Scanner(System.in);
    private static int userChoice;
    private static String nameCoin;
    private static boolean acceptableInput = false;
    private static String menu = 
            "$0.01 - Penny (1)\n"
            + "$0.05 - Nickel (2)\n"
            + "$0.10 - Dime (3)\n"
            + "$0.25 - Quarter (4)\n"
            + "$0.50 - Half Dollar (5)\n"
            + "$1.00 - Gold Dollar (6)\n"
            + "Please enter the (value) that specifies your coin";
    /**
     * This method will:
     * 1. print menu of coin name
     * 2. return the correct name for the selected option
     * @return name of coin
     */
    private static String getCoinName() {
        nameCoin = null;
        System.out.println(menu);
        do{
            try{
                userChoice = console.nextInt();
            }
            catch(InputMismatchException | NumberFormatException e){
                System.err.println(e.toString());
            }
            catch(Exception e){
                System.err.println(e.toString());
            }
            finally{
                switch(userChoice){
                    case 1:
                        nameCoin = "Penny";
                        break;
                    case 2:
                        nameCoin = "Nickel";
                        break;
                    case 3:
                        nameCoin = "Dime";
                        break;
                    case 4:
                        nameCoin = "Quarter";
                        break;
                    case 5:
                        nameCoin = "Half Dollar";
                        break;
                    case 6:
                        nameCoin = "Gold Dollar";
                        break;
                    default:
                        System.err.println("Invalid Option");
                        break;
                }
            }
        }while(!(acceptableInput));
        return nameCoin;
    }
    private static double getCoinValue() {
        /* */
    }
    static class Coin{
        public String nameOfCoin;
        public double valueOfCoin;

        private Coin(String nameOfCoin, double valueOfCoin) {
            String coinName = nameOfCoin;
            double coinValue = valueOfCoin;
        }
        
    }
    public static void main(String[] args) {
        String nameOfCoin = getCoinName();
        double valueOfCoin = getCoinValue();
        Coin coinObject = new Coin(nameOfCoin, valueOfCoin);
    }
}



Thank You

This post has been edited by k3y: 06 March 2013 - 09:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Coin Class question

#2 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 862
  • View blog
  • Posts: 2,651
  • Joined: 29-July 11

Re: Coin Class question

Posted 06 March 2013 - 10:17 AM

I didn't critique your code for logic errors, but you have something to get ad set the name as well as the value, so it looks as if you're halfway done. You need to override clone() to provide what I assume your teacher wants is a shallow copy of the coin. Overriding equals is no brainer too.

protected Object clone() {
return (Coin)super.clone(); //you need to do try catch.
}



I'll let you work out equals because it is easy.
Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Coin Class question

Posted 06 March 2013 - 10:29 AM

Why the hell is everything static?

I see you actually do have a coin class, but it's got a major bug in it. You're not actually setting those instance variables. I'd avoid double and go with int, using number of pennies.

I don't see any real instances of that Coin class. If you had a list of valid coin instances, that would be useful.
Was This Post Helpful? 2
  • +
  • -

#4 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 862
  • View blog
  • Posts: 2,651
  • Joined: 29-July 11

Re: Coin Class question

Posted 06 March 2013 - 10:51 AM

I should start reading people's code. :) Nah.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Coin Class question

Posted 06 March 2013 - 01:42 PM

Beside main() nothing should be static and as I had never seen a nickel with a value 0f 5.0001 or a dime with a value of 9.999999
don't see why your coins are double
Was This Post Helpful? 2
  • +
  • -

#6 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Coin Class question

Posted 07 March 2013 - 07:29 PM

After some cleaning up and such I have this so far
Main Class
package programmingunit5;

/**
 *
 * @author k3y
 */
class ProgrammingUnit5 {
    public static void main(String[] args){
        Coin userCoinP = new Coin("penny", 0.01);
        Coin userCoinN = new Coin("nickel", 0.05);
        Coin userCoinD = new Coin("dime", 0.10);
        Coin userCoinQ = new Coin("quarter", 0.25);
        Coin userCoinH = new Coin("half-dollar", 0.50);
        Coin userCoinG = new Coin("gold-dollar", 1.00);
        System.out.println(userCoinP.toString());
        System.out.println(userCoinN.toString());
        System.out.println(userCoinD.toString());
        System.out.println(userCoinQ.toString());
        System.out.println(userCoinH.toString());
        System.out.println(userCoinG.toString());
        
        try{
        Coin testCoin1 = (Coin) userCoinP.clone();
        }catch(CloneNotSupportedException e){
            System.err.println(e);
        }
    }
}


Coin Class
package programmingunit5;

/**
 *
 * @author k3y
 */
class Coin implements Cloneable{
    private String coinN;
    private double coinV;
    
    Coin(String coinName, double coinValue) {
        coinN = coinName;
        coinV = coinValue;
    }
    public String getCoinName(){
        return (coinN);
    }
    public double getCoinValue(){
        return (coinV);
    }
    @Override
    public String toString(){
        return (coinN + " " + "$" + coinV);
    }
    @Override
    public Object clone() throws CloneNotSupportedException{
        Coin clonedCoin = (Coin)super.clone();
        return clonedCoin;
    }
    public Coin(Coin other){
        this.coinN = other.coinN;
        this.coinV = other.coinV;
    }
}


I am pretty sure that I have everything (mostly), except the comparing. I would deeply appreciate any tips on how to go about testing my cloned object with one of the original objects. Would I override equals?

This post has been edited by k3y: 07 March 2013 - 07:30 PM

Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Coin Class question

Posted 08 March 2013 - 03:23 AM

Use an array.

You don't have the equals... This would be the comparing part. The trick is to take the object passed and figure out if it is an instance of your class; go from there.

Your clone looks dubious. What does super know about your class? Your class knows how to make a duplicate instance. Do that.
Was This Post Helpful? 1
  • +
  • -

#8 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Coin Class question

Posted 08 March 2013 - 05:51 PM

Okay, I have run into a stump. For some reason my coin name values change (work properly) however my coin values appear to be replacing the previous one with the next one in the coin values list. How would I go about correcting this
package programmingunit5;

import java.util.ArrayList;

/**
 *
 * @author k3y
 */
class ProgrammingUnit5 {
    ////////////////////VARIABLE DECLARATIONS////////////////////
    
    /**
     * @variable COIN_NAMES, these are the predefined (constant) coin names
     */
    private final static String[] COIN_NAMES = 
    {
        "Penny", "Nickel", 
        "Dime", "Quarter", 
        "Half-Dollar", "Gold-Dollar"
    };
    /**
     * @variable COIN_VALUES, these are the predefined (constant) coin values
     */
    private final static double[] COIN_VALUES = 
    {
        0.01, 0.05, 
        0.10, 0.25, 
        0.50, 1.00, 
    };
    /**
     * @variable generatedCoins, these are where the generated coins are stored
     */
    private static ArrayList<Coin> generatedCoins = new ArrayList<>();
    
    ////////////////////////////////////////////////////////////
    public static void main(String[] args){
        /**
         * @for loop 
         * for each index in COIN_NAMES
         *      add a new Coin object (COIN_NAMES[index], COIN_VALUES[x]) to generatedCoins arrayList
         *      print out each value stored in generatedCoins [index]
         * end for loop
         */
        for (int x = 0; x < COIN_NAMES.length; x++){
            generatedCoins.add(new Coin(COIN_NAMES[x], COIN_VALUES[x]));
            System.out.println(generatedCoins.toString()); 
        }
        try{
            Coin userCoin = (Coin)generatedCoins.get(1).clone();
            System.out.println(userCoin);
            if(userCoin.equals(generatedCoins.get(1))){
                System.out.println("match");
            }
            else{
                System.out.println("nope");
            }
        }
        catch(CloneNotSupportedException e){
            System.err.println(e);
        }
    }
}


This post has been edited by k3y: 08 March 2013 - 05:54 PM

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,259
  • Joined: 27-December 08

Re: Coin Class question

Posted 08 March 2013 - 07:08 PM

Avoid Cloneable and use a copy constructor instead. Cloneable is a marker interface, and doesn't guarantee that clone() will be overridden. The clone() method may also not guarantee a deep copy where the two objects are equal(), while the copy constructor guarantees this. Also, copy constructors don't require Exception handling. They're overall a better design choice.
Was This Post Helpful? 1
  • +
  • -

#10 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Coin Class question

Posted 08 March 2013 - 07:17 PM

View Postmacosxnerd101, on 08 March 2013 - 09:08 PM, said:

Avoid Cloneable and use a copy constructor instead. Cloneable is a marker interface, and doesn't guarantee that clone() will be overridden. The clone() method may also not guarantee a deep copy where the two objects are equal(), while the copy constructor guarantees this. Also, copy constructors don't require Exception handling. They're overall a better design choice.

  public static Coin newInstance(Coin aCopy) {
    return new Coin(aCopy.getCoinName(), aCopy.getCoinValue());
  }


Something like this? also, I would really like to know if there is a way to fix that array problem that I previously listed... any help is appreciated.

This post has been edited by k3y: 08 March 2013 - 07:18 PM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Coin Class question

Posted 08 March 2013 - 07:24 PM

View Postk3y, on 08 March 2013 - 09:17 PM, said:

  public static Coin newInstance(Coin aCopy) {
    return new Coin(aCopy.getCoinName(), aCopy.getCoinValue());
  }


Something like this?


Why making a static method ? why not simply make a constructor ?

  public Coin (Coin aCopy) {
    name = aCopy.getCoinName();
    value = aCopy.getValue();
  }


Was This Post Helpful? 2
  • +
  • -

#12 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Coin Class question

Posted 08 March 2013 - 07:29 PM

That looks much nicer. I now have a nice copy class, however; I'm still stuck with my array problem, it seems to be replacing the previous value. Would a two dimensional array work? 1 dimension for the name and 1 dimension for the value and make the array of Object type? however; what if I want to add to it later, is there a way to do it with an ArrayList?
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,259
  • Joined: 27-December 08

Re: Coin Class question

Posted 08 March 2013 - 07:34 PM

I ran your code and didn't encounter this problem. My output:

Quote

[Penny $0.01]
[Penny $0.01, Nickel $0.05]
[Penny $0.01, Nickel $0.05, Dime $0.1]
[Penny $0.01, Nickel $0.05, Dime $0.1, Quarter $0.25]
[Penny $0.01, Nickel $0.05, Dime $0.1, Quarter $0.25, Half-Dollar $0.5]
[Penny $0.01, Nickel $0.05, Dime $0.1, Quarter $0.25, Half-Dollar $0.5, Gold-Dollar $1.0]
Nickel $0.05
nope

Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Coin Class question

Posted 08 March 2013 - 07:35 PM

Make a single array what is your problem ? There are always people who like to complicate their life
Good programmers are the lazy ones, who avoid complicated stuff :)


private static final Coin[] myArray = {new Coin("Nickel", 5), new Coin("Dime", 10)};


Was This Post Helpful? 1
  • +
  • -

#15 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Coin Class question

Posted 08 March 2013 - 07:37 PM

View Postmacosxnerd101, on 08 March 2013 - 09:34 PM, said:

I ran your code and didn't encounter this problem. My output:

Quote

[Penny $0.01]
[Penny $0.01, Nickel $0.05]
[Penny $0.01, Nickel $0.05, Dime $0.1]
[Penny $0.01, Nickel $0.05, Dime $0.1, Quarter $0.25]
[Penny $0.01, Nickel $0.05, Dime $0.1, Quarter $0.25, Half-Dollar $0.5]
[Penny $0.01, Nickel $0.05, Dime $0.1, Quarter $0.25, Half-Dollar $0.5, Gold-Dollar $1.0]
Nickel $0.05
nope


here is my output

Quote

[Penny $0.01]
[Penny $0.05, Nickel $0.05]
[Penny $0.1, Nickel $0.1, Dime $0.1]
[Penny $0.25, Nickel $0.25, Dime $0.25, Quarter $0.25]
[Penny $0.5, Nickel $0.5, Dime $0.5, Quarter $0.5, Half-Dollar $0.5]
[Penny $1.0, Nickel $1.0, Dime $1.0, Quarter $1.0, Half-Dollar $1.0, Gold-Dollar $1.0]
Nickel $1.0
match


why it no like me?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3