Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

recursive constructor invocation ?

843789Aug 19 2009 — edited Aug 19 2009
heya all

i can seam to grasp how to correct this error anyone help ?
public class Shop
{
    public static void main(String args[])
    {
        Book book1, book2, book3;
        Pen pen1, pen2;

        book1 = new Book("Pigs may fly", 100, 120);
        book2 = new Book("Pigs can't fly", 75, 100);
        book3 = new Book ("Pigs must not fly", 10, 75, 100);

        pen1 = new Pen("Balck", 10, 10, 15);
        pen2 = new Pen("Blue", 10, 15);

        book1.purchaseCopies(10);
        book2.purchaseCopies(10);
        pen2.purchasePens(10);

        book1.sellCopies(5);
        book2.sellCopies(2);
        book3.sellCopies(1);

        pen1.sellPens(10);
        pen2.sellPens(3);

        System.out.println("\nBooks");
        book1.printDetails();
        book2.printDetails();
        book3.printDetails();

        System.out.println("\nPens");
        pen1.printDetails();
        pen2.printDetails();
        System.out.print("\nTotal profit");
        int sum = book1.calculateProfit();
        sum += book2.calculateProfit();
        sum += book3.calculateProfit();
        sum += pen1.calculateProfit();
        sum += pen2.calculateProfit();
        System.out.println(sum + "p");
    }
}
public class Pen
{
    private String inkColour;
    private int numPensPurchased;
    private int purchasePricePerPen;
    private int salePricePerPen;
    private int numPensSold;

    public Pen(String ink, int purchasePrice, int salePrice)
    {
    this (ink, 0, purchasePrice, salePrice);
    }
    public Pen(String ink, int numPens, int purchasePrice, int salePrice)
    {
        inkColour = ink;
        numPensPurchased = numPens;
        purchasePricePerPen = purchasePrice;
        salePricePerPen = salePrice;
        numPensSold = 0;
    }
    public void purchasePens(int numPens)
    {
        numPensPurchased += numPens;
    }
    public void sellPens(int numPens)
    {
        numPensSold += numPens;
    }
    public int calculateProfit()
    {
        return numPensSold* (salePricePerPen - purchasePricePerPen);
    }
    public void printDetails()
    {
    System.out.printf("%8s; %3d bought @ %3dp; %3d Sold @ %3dp; profit is %5dp",
            inkColour,
            numPensPurchased, purchasePricePerPen,
            numPensSold, salePricePerPen, calculateProfit());
    }
}
public class Book
{
    private String title;
    private int numCopiesPurchased;
    private int purchasePricePerCopy;
    private int salePricePerCopy;
    private int numCopiesSold;


    public Book(String theTitle, int purchasePrice, int salePrice)
    {
    this(theTitle, purchasePrice, salePrice);
    }
    public Book(String theTitle, int numCopies, int purchasePrice, int salePrice)
    {
    title = theTitle;
    numCopiesPurchased = numCopies;
    purchasePricePerCopy = purchasePrice;
    salePricePerCopy = salePrice;
    numCopiesSold = 0;

    }
    public void purchaseCopies(int numCopies)
    {
    numCopiesPurchased += numCopies;
    }
    public void sellCopies(int numCopies)
    {
    salePricePerCopy += numCopies;
    }
    public int calculateProfit()
    {
    return numCopiesSold*(salePricePerCopy - purchasePricePerCopy);
    }
    public void printDetails()
    {
    System.out.printf("%8s; %3d bought @ %3dp; %3d Sold @ %3dp; profit is %5dp",
            title,
            numCopiesPurchased, purchasePricePerCopy,
            numCopiesSold, salePricePerCopy, calculateProfit());
    }
}
I found this example of how to solve it , but i cant seam to adapt if for this ?
public Rectangle( float L, float W ) {
this( L, W );
}
should be changed to..
public Rectangle( float L, float W ) {
setLength(L);
setWidth(W);
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 16 2009
Added on Aug 19 2009
10 comments
1,478 views