Good Morning!

I am trying to create a Book2 object and assign to Inventory_Program6a. When I try the following: Book2 newBookTitle = Book2();

I get the error message:
C:\java>javac Inventory_Program6a.java
Inventory_Program6a.java:325: cannot find symbol
symbol : constructor Book2()
location: class Book2
Book2 newBookTitle = new Book2();
1 error

I can't for the life of me figure out why I can't create a Book2 object.

Any assistance is greatly appreciated!

I've attached my code for both Inventory_Program6a and Book2

import java.awt.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JLabel;

public class Inventory_Program6a extends JFrame
	// instance variables
	private int displayBooks = 0; // variable for actionEvents
	private int arrayCount;

	private JButton firstButton;
	private JButton previousButton;
	private JButton nextButton;
	private JButton lastButton;
	private JButton addButton;
	private JButton deleteButton;
	private JButton modifyButton;
	private JButton saveButton;
	private JButton searchButton;

	private JTextField bookTitleField;
	private JTextField itemNumberField;
	private JTextField unitsInStockField;
	private JTextField bookPriceField;
	private JTextField inventoryValueField;
	private JTextField bookAuthorField;
	private JTextField restockingFeeField;

	private JLabel bookTitleLabel;
	private JLabel itemNumberLabel;
	private JLabel unitsInStockLabel;
	private JLabel bookPriceLabel;
	private JLabel inventoryValueLabel;
	private JLabel bookAuthorLabel;
	private JLabel restockingFeeLabel;

	private JPanel display;
	private JPanel content;
	private JTextArea textArea;
	private JLabel label;

	// Declare an array of classes
	private Book2 myBook[];

	// method to sort inventory by book title
	public Book2[] sortBookInventory()
        	for(int i = 0; i < myBook.length; i++) 
            	String bookTitle1 = myBook[i].getBookTitle();
            	int min = i;
            	String bookTitle = bookTitle1;
            		for(int j = i+1; j < myBook.length; j++) 
                	String bookTitle2 = myBook[j].getBookTitle();
                		if(bookTitle2.compareTo(bookTitle) < 0) 
                    		min = j;
                    		bookTitle = bookTitle2;
                	Book2 temp = myBook[i];
                	myBook[i] = myBook[min];
                	myBook[min] = temp;
        		return myBook;
	} // end method sortBookInventory

	// constructor
	public Inventory_Program6a()
		double totalInventoryValue = 0.0;

		// Specify how many items are in the array
		// instantiate Book2 object
		myBook = new Book2[5];
		myBook[0] = new Book2("The Key Triology", 1, 25, 7.99, "Nora Roberts");
		myBook[1] = new Book2("The Shinning", 2, 15, 5.99, "Stephen King");
		myBook[2] = new Book2("Wild Acre", 3, 7, 4.99, "Phillipa Gregory");
		myBook[3] = new Book2("Dark Paradise", 4, 2, 12.99, "Tami Hoag");
		myBook[4] = new Book2("Dollhouse Murders", 5, 18, 2.95, "Betty Ren Wright");
		// call method sort book inventory for display

		for (int i = 0; i<5; i++)

			// display the book title
			System.out.println("The book title is: " + myBook[i].getBookTitle());

			// display the item number
			System.out.println("The item number is: " + myBook[i].getItemNumber());

			// display the number of units in stock
			System.out.println("The number of units in stock is: " + myBook[i].getBookUnits());

			// display the price per book
			System.out.printf("The price of the book is: $%.2f\n", myBook[i].getBookPrice());

			// display the value of the inventory
			System.out.printf("The value of the inventory is: $%.2f\n", myBook[i].inventoryValue());

			// display the book author
			System.out.println("The book author is: " + myBook[i].getBookAuthor());

			// display the restocking fee
			System.out.println("Restock Fee: " + myBook[i].inventoryValue() * 0.05);

			// calculate total inventory value with restocking fee added
			totalInventoryValue += (myBook[i].inventoryValue() * 1.05);

			// insert blank line

		} // end for

		// display the total value of the inventory with the restocking fee
		System.out.printf("The total value of the book inventory including the restocking fee is: $%5.2f.\n", totalInventoryValue);

		// display the total value of the inventory excluding the restocking fee
		System.out.println("The total value of the book inventory is: " + totalInventoryValue());

		//JLabel constructor for logo
		Icon logo = new ImageIcon("C:/book.jpg");
		label = new JLabel(logo);
		label.setToolTipText("Joyce's Logo");

		// create textArea
		textArea = new JTextArea(myBook[3]+"\n");
		// Create content panel, set layout
		content = new JPanel();
		content.setLayout(new FlowLayout());
		content.setLayout(new GridLayout(3,4));

		// create JPanel for array items
		display = new JPanel();
		display.setLayout(new FlowLayout());
		display.setLayout(new GridLayout(7,1));

		arrayCount = displayBooks;

		// set window (JFrame) characteristics
		setLayout(new BorderLayout());
		getContentPane().add(new JScrollPane(display), BorderLayout.CENTER);
		getContentPane().add(content, BorderLayout.SOUTH);
		getContentPane().add(label, BorderLayout.NORTH);
		setTitle("Book Inventory Program");
		setSize(400, 400); // set frame size

		// initialize components
		bookTitleField = new JTextField();

		itemNumberField = new JTextField();
		unitsInStockField = new JTextField();

		bookPriceField = new JTextField();

		inventoryValueField = new JTextField();

		bookAuthorField = new JTextField();

		restockingFeeField = new JTextField();
		// initialize components
		firstButton = new JButton("First");

		previousButton = new JButton("Previous");

		nextButton = new JButton("Next");

		lastButton = new JButton("Last");

		addButton = new JButton("Add");

		deleteButton = new JButton("Delete");

		modifyButton = new JButton("Modify");

		saveButton = new JButton("Save");

		searchButton = new JButton("Search");

		// display JLabels and JTextFields on display panel
		display.add(new JLabel("Book Title: "));

		display.add(new JLabel("Item Number: "));

		display.add(new JLabel("Units in Stock: "));

		display.add(new JLabel("Book Price: "));

		display.add(new JLabel("Inventory Value: "));
		display.add(new JLabel("Book Author: "));
		display.add(new JLabel("Restocking Fee: "));

		// assign actionListener and actionEvent to firstButton
		firstButton.addActionListener(new ActionListener()
		public void actionPerformed(ActionEvent event)
			if (event.getActionCommand()== "First")
			displayBooks = 0;
		} // end firstButton actionEvent
		}); // end lastButton actionListener

		// assign actionListener and actionEvent to previousButton
		previousButton.addActionListener(new ActionListener()
		public void actionPerformed(ActionEvent event)
		if (event.getActionCommand()== "Previous")
		if (displayBooks < 0)
		displayBooks = 0;
			if (displayBooks == 0)
			displayBooks = displayBooks = myBook.length-1;
		} // end previousButton actionEvent
		}); // end previousBUtton actionListener

		// assign actionListener and actionEvent to nextButton
		nextButton.addActionListener(new ActionListener()
		public void actionPerformed(ActionEvent event)
		if (event.getActionCommand()== "Next")
		if (displayBooks >= myBook.length)
		displayBooks = myBook.length-1;
			if (displayBooks == myBook.length-1)
			displayBooks = 0;
		} // end nextButton actionEvent
		}); // end nextButton actionListener

		// assign actionListener and actionEvent to lastButton
		lastButton.addActionListener(new ActionListener()
		public void actionPerformed(ActionEvent event)
		if (event.getActionCommand()== "Last")
		displayBooks = myBook.length-1;
		} // end lastButton actionEvent
		}); // end lastButton actionListener

		// assign actionListener and actionEvent to lastButton
		addButton.addActionListener(new ActionListener()
		public void actionPerformed(ActionEvent event)
		Book2 newBookTitle = new Book2();
		} // end addButton actionEvent
		}); // end addButton actionListener

		// assign actionListener and actionEvent to saveButton
		saveButton.addActionListener(new ActionListener()
		public void actionPerformed(ActionEvent event)
		if (event.getActionCommand()== "Save")
			FileOutputStream out;
			PrintStream p;
				String strDirectory = "c:/data";
				new File(strDirectory).mkdir();
				out = new FileOutputStream ("C:/data/inventory.dat");
				p = new PrintStream(out);
					for (int i = 0; i<= 5 - 1; i++)
						p.println("Book Title: " + myBook[i].getBookTitle()+"\n");
						p.println("Item Number: " + myBook[i].getItemNumber()+"\n");
						p.println("Book Units: " + myBook[i].getBookUnits()+"\n");
						p.println("Book Price: " + myBook[i].getBookPrice()+"\n");
						p.println("Inventory Value: " + myBook[i].inventoryValue()+"\n");
						p.println("Book Restocking Fee: " + myBook[i].bookRestockingFee()+"\n");
			catch (Exception e)

		} // end lastButton actionEvent
		}); // end lastButton actionListener

	} // end constructor

	// method to calculate the value of the entire inventory
	public double totalInventoryValue()
		double totalInventoryValue = 0.00;

		for ( int counter = 0; counter < myBook.length; counter++ )

			totalInventoryValue += myBook[counter].inventoryValue();

		return totalInventoryValue;

	} // end method totalBookValue

	// method to setTextFields
	private void setTextFields()
	if (displayBooks == arrayCount)
			displayBooks = 0;
		if (displayBooks < 0)
			displayBooks = arrayCount -1;
	} // end method setTextFields

	// main method begins execution of Java application
	public static void main( String args [])
		new Inventory_Program6a();
		//create JFrame
		Inventory_Program6a inventory = new Inventory_Program6a();
	} // end method main

} // end class Inventory_Program6a


public class Book2 extends Book

	// Book2 variable
	private String bookAuthor; // Author of the book

	// Book2 constructor
	public Book2(String bookTitle, int itemNumber, int bookUnits, double bookPrice, String bookAuthor)

		// call super class constructor
		super(bookTitle, itemNumber, bookUnits, bookPrice);
		this.bookAuthor = bookAuthor; // initializes bookTitle for Book2
	} // end constructor

	// method to set the book author
	public void setBookAuthor (String bookAuthor)
		this.bookAuthor = bookAuthor;
	} // end method setBookAuthor

	// method to retrieve the book author
	public String getBookAuthor()
		return bookAuthor;
	} // end method getBookAuthor

	// Calculate the stock value
	// Returns the total value of books in stock, adding a 5% restocking fee
	public double inventoryValue()
		return (super.inventoryValue() * 1.05);

	// method for returning the book restocking fee
	public double bookRestockingFee()
		return(super.inventoryValue() * 0.05);

	public String toString()
	return String.format("Book Title: %s\nItem Number: %d\nUnits in Stock: %d\nBook Price: $%.2f\nInventory Value: $%.2f\nBook Author: %s\nRestocking Fee: $%.2f\n", getBookTitle(), getItemNumber(), getBookUnits(), getBookPrice(), inventoryValue(), getBookAuthor(), bookRestockingFee());

} // end class Book2
10 Years
Discussion Span
Last Post by cms271828

Hmmm, I don't get it

In your Book2 class, you've got:

super(bookTitle, itemNumber, bookUnits, bookPrice);

but when I look at java.awt.print.Book,
the only constructor I see is Book().

I've never even seen this class before, but still, if the constructor you are invoking doesn't exist, thats a problem.

I checked J2SE 1.4 and 1.5, and in both, Book() is only constructor.

Please let me know whats going on, maybe I can help.


I also have a Book class. Book 2 is a subclass of Book. I'm still baffled as to why I can't creat an object of Book or Book2 as they both exisit......


I've attached my Book code:

public class Book // Book has four attributes

	private String bookTitle = "";	
	private int itemNumber = 0;
	private int bookUnits = 0;
	private double bookPrice = 0.00;

	// Book constructor
	public Book (String bookTitle, int itemNumber, int bookUnits, double bookPrice)
		this.bookTitle = bookTitle; // initializes bookTitle for Book		
		this.itemNumber = itemNumber; // initializes itemNumber for Book
		this.bookUnits = bookUnits; // initializes bookUnits for Book
		this.bookPrice = bookPrice; // initializes bookPrice for Book
	} // end constructor
	// method to set the book title
	public void setBookTitle (String bookTitle)
		this.bookTitle = bookTitle;
	} // end method setBookTitle

	// method to retrieve the book title
	public String getBookTitle()
		return bookTitle;
	} // end method getBookTitle

	// method to set the item number
	public void setItemNumber (int itemNumber)
		this.itemNumber = itemNumber;
	} // end method setItemNumber

	// method to retrieve the item number
	public int getItemNumber()
		return itemNumber;
	} // end method getItemNumber

	// method to set the book units
	public void setBookUnits (int bookUnits)
		this.bookUnits = bookUnits;
	} // end method setBookUnits

	// method to retrieve the book units
	public int getBookUnits()
		return bookUnits;
	} // end method getBookUnits

	// method to set the book price
	public void setBookPrice (double bookPrice)
		this.bookPrice = bookPrice;
	} // end method setBookPrice

	// method to retrieve the book price
	public double getBookPrice()
		return bookPrice;
	} // end method getBookPrice

	// method to calculate the inventory value
	public double inventoryValue()
		return bookPrice * bookUnits;
	} // end method inventoryValue

} // end class Book

Ok, this is easy...

If you dont have any constructors in your class, then you automatically get the default one for free...

You have a constructor..Book(a,b,c)
So therefore, you do not get the default no-args constructor, if you want it, you must put it in explicity as well.

Book2() { }

Book2(a,b,c) { // some code }

Hope that helps


Hi mariejt,

I am trying to create a Book2 object and assign to Inventory_Program6a. When I try the following: Book2 newBookTitle = Book2();

Book2 newBookTitle = new Book2(); The problem is you didn't declare default constructor in both Book and Book2
.So Simply add Book() and Book2() constructors in their respective classes .

One more thing use "code=java" while posting the code so that it will provide syntax highlighting which makes easy to read it.



Another point to note...
If you do not call super(...) in your constructor, then it is called automatically for you on the default no-args constructor of the super class.

So supposing in super class you only had...
Book(int a,int b,int c){ }

Then if you DONT call super in book2's constructors, it will try to invoke Book(), and it will not compile.

This is a common question that comes up in java exams, and worth knowing about.

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.