Coin Class question

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

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

#16 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Coin Class question

Posted 08 March 2013 - 07:41 PM

I still think, and persist to think, that having coin as double is an herisi

Some times I really miss the Spanish Inquisition
Was This Post Helpful? 1
  • +
  • -

#17 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: Coin Class question

Posted 08 March 2013 - 07:41 PM

A copied these two classes into a .java file to test them and ran it. For the sake of completeness, below is exactly what I ran. Note that I don't advocate when working on a project placing two separate classes in the same file.
Spoiler

Was This Post Helpful? 1
  • +
  • -

#18 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:45 PM

That is strange, it is now printing the proper way... This is weird.. I appreciate all of the help I have received. I will continue tweaking this and making it simpler.
Was This Post Helpful? 0
  • +
  • -

#19 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: Coin Class question

Posted 08 March 2013 - 07:46 PM

Glad to have helped!
Was This Post Helpful? 1
  • +
  • -

#20 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:49 PM

@pbl Idk, our professor wanted us to make it print out in the format coinName followed by value ($0.00) so I could try your idea and use a decimalFormat and see if I can get it to work.
Was This Post Helpful? 0
  • +
  • -

#21 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: Coin Class question

Posted 08 March 2013 - 07:51 PM

NumberFormat getCurrencyInstance() is the appropriate tool to use. Oracle suggests using BigDecimal when dealing with money due to the imprecisions of double. Really, using an int and dealing with pennies is sufficient.
Was This Post Helpful? 2
  • +
  • -

#22 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 - 08:00 PM

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

NumberFormat getCurrencyInstance() is the appropriate tool to use. Oracle suggests using BigDecimal when dealing with money due to the imprecisions of double. Really, using an int and dealing with pennies is sufficient.


Should I change my toString to:
return (coinN + " " + "$" + NumberFormat.getInstance().format(coinV));


and for the int. what do you mean+?
Was This Post Helpful? 0
  • +
  • -

#23 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: Coin Class question

Posted 08 March 2013 - 08:08 PM

You could really just use the Currency instance of the NumberFormat class.

As for the int- have the field in the Coin class model the value in pennies. So a nickel's value would be 5, as it is worth 5 pennies.
Was This Post Helpful? 1
  • +
  • -

#24 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Coin Class question

Posted 08 March 2013 - 08:13 PM

Show that your teacher

public class Coin {

	public static void main(String[] args) {
		double dNickel = 0.05;
		int iNickel = 5;
		
		double dTotal = 0.0;
		long iTotal = 0;
		
		for(long i = 0; i < 0xFFFFFFFL; ++i) {
			dTotal += dNickel;
			iTotal += iNickel;
		}
		System.out.println(dTotal + " " + iTotal);
	}
}


yields:
1.3421772802941175E7 1342177275

I didn't check it by hand but I'll bet you whatever you want that
1342177275 is right and 1.3421772802941175E7 is wrong :)
Was This Post Helpful? 1
  • +
  • -

#25 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 - 08:19 PM

Okay here is what I have (still alittle dirty)
MAIN
package programmingunit5;

import java.util.ArrayList;

/**
 *
 * @author Patrick
 */
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("***confirm ArrayList***");
        /**
         * @print this is to check/make sure that generatedCoins have valid values
         */
         System.out.println(generatedCoins.toString());
        System.out.println("***********************"); 
        
  
        try{
            Coin userCoin = (Coin)generatedCoins.get(1).clone();
            System.out.println(generatedCoins.get(1).toString());
            System.out.println(userCoin.toString());
            if(userCoin.equals(generatedCoins.get(1))){
                System.out.println("match");
            }
            else{
                System.out.println("nope");
            }
        }
        catch(CloneNotSupportedException e){
            System.err.println(e);
        }
    }
}



COIN
package programmingunit5;

import java.text.NumberFormat;
import java.util.Currency;

/**
 *
 * @author Patrick
 */
class Coin implements Cloneable{
    private String coinN;
    private double coinV;
    
    public 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 + " " + NumberFormat.getCurrencyInstance().format(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;
    }

    public void copy(Coin aCopy) {
        coinN = aCopy.getCoinName();
        coinV = aCopy.getCoinValue();
    }
}


My clone appears to be working, however; my equals() is now off. Is my way not good? or what do I have to do in order make it actually print the right result? Should I use comparable?

output

Quote

run:
***confirm ArrayList***
[Penny $0.01, Nickel $0.05, Dime $0.10, Quarter $0.25, Half-Dollar $0.50, Gold-Dollar $1.00]
***********************
Nickel $0.05
Nickel $0.05
nope

That should say "match"... why this no like?

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

Was This Post Helpful? 0
  • +
  • -

#26 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Coin Class question

Posted 08 March 2013 - 08:22 PM

Show that your teacher
public class Coin {

	public static void main(String[] args) {
		double dNickel = 0.05;
		int iNickel = 5;
		
		double dTotal = 0.0;
		long iTotal = 0;
		
		for(long i = 0; i < 0xFFFFFFFL; ++i) {
			dTotal += dNickel;
			iTotal += iNickel;
		}
		System.out.println(dTotal + " " + iTotal);
	}
}



yields:
1.3421772802941175E7 1342177275

I didn't check it by hand but I'll bet you whatever you want that
1342177275 is right and 1.3421772[b]802941175[/b[E7 is wrong
Was This Post Helpful? 1
  • +
  • -

#27 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Coin Class question

Posted 08 March 2013 - 08:33 PM

Still don't see why you still have these 2 arrays ?!?!?!?!!?
    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, 
    };



A single array should do as I have showed to you
Was This Post Helpful? 1
  • +
  • -

#28 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 - 08:43 PM

okay I did it the Canadian way (pbl's way :bigsmile:/>) and it seems to be sort of working however:

OUTPUT

Quote

***confirm ArrayList***
Penny $1.00
Nickel $5.00
Dime $10.00
Quarter $25.00
Half-Dollar $50.00
Gold-Dollar $100.00
***********************


CODE
package programmingunit5;

import java.util.ArrayList;

/**
 *
 * @author Patrick
 */
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<>();
    private static final Coin[] coinArray = 
    {
        new Coin("Penny", 1),
        new Coin("Nickel", 5),
        new Coin("Dime", 10),
        new Coin("Quarter", 25),
        new Coin("Half-Dollar", 50),
        new Coin("Gold-Dollar", 100)
    };
    ////////////////////////////////////////////////////////////
    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("***confirm ArrayList***");
        /**
         * @print this is to check/make sure that generatedCoins have valid values
         */
        for(int x = 0; x <coinArray.length;x++){
         System.out.println(coinArray[x].toString());}
        System.out.println("***********************"); 
        
  
        //try{
            /*Coin userCoin = (Coin)generatedCoins.get(1).clone();
            System.out.println(generatedCoins.get(1).toString());
            System.out.println(userCoin.toString());
            if(userCoin.equals(generatedCoins.get(1))){
                System.out.println("match");
            }
            else{
                System.out.println("nope");
            }*/
        //}
        //catch(CloneNotSupportedException e){
          //  System.err.println(e);
        }
    
}


COIN
package programmingunit5;

import java.text.NumberFormat;
import java.util.Currency;

/**
 *
 * @author Patrick
 */
class Coin implements Cloneable{
    private String coinN;
    private double coinV;
    
    public 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 + " " + NumberFormat.getCurrencyInstance().format(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;
    }

    public void copy(Coin aCopy) {
        coinN = aCopy.getCoinName();
        coinV = aCopy.getCoinValue();
    }
}




Do I have to type cast somewhere?

okay I divided it by 100 when I formatted it and it looks beautiful.

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

Was This Post Helpful? 0
  • +
  • -

#29 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Coin Class question

Posted 08 March 2013 - 08:50 PM

View Postk3y, on 08 March 2013 - 10:43 PM, said:

okay I did it the Canadian way (pbl's way :bigsmile:/>) and it seems to be sort of working however:

My way would be with int not double
surely not display with .00
Was This Post Helpful? 1
  • +
  • -

#30 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 - 09:12 PM

Okay the last piece to the davinci code here:

Quote

run:
***********************
Penny $0.01
Nickel $0.05
Dime $0.10
Quarter $0.25
Half-Dollar $0.50
Gold-Dollar $1.00
***********************
Penny $0.01
Penny $0.01
nope
BUILD SUCCESSFUL (total time: 0 seconds)


As you can see the last two entries are both pennies (one is the orig) and one is the copy. So my copy method must be working, however my equals is not. How can I improve my equals method?
Was This Post Helpful? 0
  • +
  • -

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