0

Im writing a program that will instatiate an object from another object but I have no idea how to get it to work.
Here is my main code

public class testPurse {
	public static void main(String[] args) 
		{
		Purse p1 = new Purse();
		System.out.println(p1);
		p1.insert(3,0,2,1);
		System.out.println(p1);
		p1.insert(3,1,1,3);
		System.out.println(p1);
		p1.remove(3,1,0,2);
		System.out.println(p1);
		p1.remove(0,0,0,4);
		System.out.println(p1);
		Purse p2 = new Purse(4,3,4,4);
		System.out.println(p2);
		Purse p3 = new Purse(p1); // This is the problem I dont know how to make the new object with the data from the old object
		System.out.println(p3); 
		}
}

and here is the other class

import java.text.DecimalFormat; 
public class Purse
{
	private double penny;
	private double nickle;
	private double dime;
	private double quarter;

	public Purse(){
		penny = 0;
		nickle = 0;
		dime = 0;
		quarter = 0;
	}

	public Purse(double pennyAmount, double nickleAmount, double dimeAmount, double quarterAmount){
		penny = pennyAmount;
		nickle = nickleAmount;
		dime = dimeAmount;
		quarter = quarterAmount;
	}


	public void insert(double pennyAmount, double nickleAmount, double dimeAmount, double quarterAmount){
	penny = penny + pennyAmount;
	nickle = nickle + nickleAmount;
	dime = dime + dimeAmount;
	quarter = quarter + quarterAmount;
	}

	public void remove(double pennyAmount, double nickleAmount, double dimeAmount, double quarterAmount){
	penny = penny - pennyAmount;
	nickle = nickle - nickleAmount;
	dime = dime - dimeAmount;
	quarter = quarter - quarterAmount;
	}

	public String toString() {
	DecimalFormat twoDigits = new DecimalFormat("0.00");
	DecimalFormat oneDigit = new DecimalFormat("0"); 
    return (oneDigit.format(quarter) + " Quarters + " + oneDigit.format(dime) + " dimes + " + oneDigit.format(nickle) + " nickles " + oneDigit.format(penny)
		+ " pennies = $" + (twoDigits.format((quarter * .25) + (dime * .1) + (nickle * .05) + (penny * .01))));
	}
}

any help would be appreciated

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by masijade
0

By creating an appropriate constructor in the "Purse" class.

I.E

public Bogus(Bogus bogus) {
    someValue = bogus.someValue;
    someOtherValue = bogus.someOtherValue;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.