Instantiating an Object of a Class Confusion

Reply

Join Date: Oct 2007
Posts: 7
Reputation: mariejt is an unknown quantity at this point 
Solved Threads: 0
mariejt mariejt is offline Offline
Newbie Poster

Instantiating an Object of a Class Confusion

 
0
  #1
Oct 13th, 2007
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

  1. Inventory_Program6a:
  2. import java.awt.*;
  3. import java.io.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.awt.BorderLayout;
  7. import java.awt.FlowLayout;
  8. import java.awt.GridLayout;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11.  
  12. import javax.swing.*;
  13. import javax.swing.JPanel;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.JTextArea;
  16. import javax.swing.JButton;
  17. import javax.swing.JFrame;
  18. import javax.swing.JTextField;
  19. import javax.swing.Icon;
  20. import javax.swing.ImageIcon;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.JLabel;
  23.  
  24. public class Inventory_Program6a extends JFrame
  25. {
  26. // instance variables
  27. private int displayBooks = 0; // variable for actionEvents
  28. private int arrayCount;
  29.  
  30. private JButton firstButton;
  31. private JButton previousButton;
  32. private JButton nextButton;
  33. private JButton lastButton;
  34. private JButton addButton;
  35. private JButton deleteButton;
  36. private JButton modifyButton;
  37. private JButton saveButton;
  38. private JButton searchButton;
  39.  
  40. private JTextField bookTitleField;
  41. private JTextField itemNumberField;
  42. private JTextField unitsInStockField;
  43. private JTextField bookPriceField;
  44. private JTextField inventoryValueField;
  45. private JTextField bookAuthorField;
  46. private JTextField restockingFeeField;
  47.  
  48. private JLabel bookTitleLabel;
  49. private JLabel itemNumberLabel;
  50. private JLabel unitsInStockLabel;
  51. private JLabel bookPriceLabel;
  52. private JLabel inventoryValueLabel;
  53. private JLabel bookAuthorLabel;
  54. private JLabel restockingFeeLabel;
  55.  
  56. private JPanel display;
  57. private JPanel content;
  58. private JTextArea textArea;
  59. private JLabel label;
  60.  
  61. // Declare an array of classes
  62.  
  63. private Book2 myBook[];
  64.  
  65. // method to sort inventory by book title
  66. public Book2[] sortBookInventory()
  67. {
  68. for(int i = 0; i < myBook.length; i++)
  69. {
  70. String bookTitle1 = myBook[i].getBookTitle();
  71. int min = i;
  72. String bookTitle = bookTitle1;
  73. for(int j = i+1; j < myBook.length; j++)
  74. {
  75. String bookTitle2 = myBook[j].getBookTitle();
  76. if(bookTitle2.compareTo(bookTitle) < 0)
  77. {
  78. min = j;
  79. bookTitle = bookTitle2;
  80. }
  81. }
  82. if(!bookTitle.equals(bookTitle1))
  83. {
  84. Book2 temp = myBook[i];
  85. myBook[i] = myBook[min];
  86. myBook[min] = temp;
  87. }
  88. }
  89. return myBook;
  90. } // end method sortBookInventory
  91.  
  92. // constructor
  93. public Inventory_Program6a()
  94. {
  95. double totalInventoryValue = 0.0;
  96.  
  97. // Specify how many items are in the array
  98. // instantiate Book2 object
  99. myBook = new Book2[5];
  100. myBook[0] = new Book2("The Key Triology", 1, 25, 7.99, "Nora Roberts");
  101. myBook[1] = new Book2("The Shinning", 2, 15, 5.99, "Stephen King");
  102. myBook[2] = new Book2("Wild Acre", 3, 7, 4.99, "Phillipa Gregory");
  103. myBook[3] = new Book2("Dark Paradise", 4, 2, 12.99, "Tami Hoag");
  104. myBook[4] = new Book2("Dollhouse Murders", 5, 18, 2.95, "Betty Ren Wright");
  105.  
  106. // call method sort book inventory for display
  107. sortBookInventory();
  108.  
  109. for (int i = 0; i<5; i++)
  110. {
  111.  
  112. // display the book title
  113. System.out.println("The book title is: " + myBook[i].getBookTitle());
  114.  
  115. // display the item number
  116. System.out.println("The item number is: " + myBook[i].getItemNumber());
  117.  
  118. // display the number of units in stock
  119. System.out.println("The number of units in stock is: " + myBook[i].getBookUnits());
  120.  
  121. // display the price per book
  122. System.out.printf("The price of the book is: $%.2f\n", myBook[i].getBookPrice());
  123.  
  124. // display the value of the inventory
  125. System.out.printf("The value of the inventory is: $%.2f\n", myBook[i].inventoryValue());
  126.  
  127. // display the book author
  128. System.out.println("The book author is: " + myBook[i].getBookAuthor());
  129.  
  130. // display the restocking fee
  131. System.out.println("Restock Fee: " + myBook[i].inventoryValue() * 0.05);
  132.  
  133. // calculate total inventory value with restocking fee added
  134. totalInventoryValue += (myBook[i].inventoryValue() * 1.05);
  135.  
  136. // insert blank line
  137. System.out.println();
  138.  
  139. } // end for
  140.  
  141. // display the total value of the inventory with the restocking fee
  142. System.out.printf("The total value of the book inventory including the restocking fee is: $%5.2f.\n", totalInventoryValue);
  143.  
  144. // display the total value of the inventory excluding the restocking fee
  145. System.out.println("The total value of the book inventory is: " + totalInventoryValue());
  146.  
  147. //JLabel constructor for logo
  148. Icon logo = new ImageIcon("C:/book.jpg");
  149. label = new JLabel(logo);
  150. label.setToolTipText("Joyce's Logo");
  151.  
  152. // create textArea
  153. textArea = new JTextArea(myBook[3]+"\n");
  154.  
  155. // Create content panel, set layout
  156. content = new JPanel();
  157. content.setLayout(new FlowLayout());
  158. content.setLayout(new GridLayout(3,4));
  159.  
  160. // create JPanel for array items
  161. display = new JPanel();
  162. display.setLayout(new FlowLayout());
  163. display.setLayout(new GridLayout(7,1));
  164.  
  165. arrayCount = displayBooks;
  166.  
  167. // set window (JFrame) characteristics
  168. setLayout(new BorderLayout());
  169. getContentPane().add(new JScrollPane(display), BorderLayout.CENTER);
  170. getContentPane().add(content, BorderLayout.SOUTH);
  171. getContentPane().add(label, BorderLayout.NORTH);
  172. pack();
  173. setTitle("Book Inventory Program");
  174. setSize(400, 400); // set frame size
  175. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  176. setLocationRelativeTo(null);
  177.  
  178. // initialize components
  179.  
  180. bookTitleField = new JTextField();
  181. bookTitleField.setEditable(false);
  182.  
  183. itemNumberField = new JTextField();
  184. itemNumberField.setEditable(false);
  185.  
  186. unitsInStockField = new JTextField();
  187. unitsInStockField.setEditable(false);
  188.  
  189. bookPriceField = new JTextField();
  190. bookPriceField.setEditable(false);
  191.  
  192. inventoryValueField = new JTextField();
  193. inventoryValueField.setEditable(false);
  194.  
  195. bookAuthorField = new JTextField();
  196. bookAuthorField.setEditable(false);
  197.  
  198. restockingFeeField = new JTextField();
  199. restockingFeeField.setEditable(false);
  200.  
  201. // initialize components
  202. firstButton = new JButton("First");
  203. content.add(firstButton);
  204.  
  205. previousButton = new JButton("Previous");
  206. content.add(previousButton);
  207.  
  208. nextButton = new JButton("Next");
  209. content.add(nextButton);
  210.  
  211. lastButton = new JButton("Last");
  212. content.add(lastButton);
  213.  
  214. addButton = new JButton("Add");
  215. content.add(addButton);
  216.  
  217. deleteButton = new JButton("Delete");
  218. content.add(deleteButton);
  219.  
  220. modifyButton = new JButton("Modify");
  221. content.add(modifyButton);
  222.  
  223. saveButton = new JButton("Save");
  224. content.add(saveButton);
  225.  
  226. searchButton = new JButton("Search");
  227. content.add(searchButton);
  228.  
  229. // display JLabels and JTextFields on display panel
  230. display.add(new JLabel("Book Title: "));
  231. display.add(bookTitleField);
  232.  
  233. display.add(new JLabel("Item Number: "));
  234. display.add(itemNumberField);
  235.  
  236. display.add(new JLabel("Units in Stock: "));
  237. display.add(unitsInStockField);
  238.  
  239. display.add(new JLabel("Book Price: "));
  240. display.add(bookPriceField);
  241.  
  242. display.add(new JLabel("Inventory Value: "));
  243. display.add(inventoryValueField);
  244.  
  245. display.add(new JLabel("Book Author: "));
  246. display.add(bookAuthorField);
  247.  
  248. display.add(new JLabel("Restocking Fee: "));
  249. display.add(restockingFeeField);
  250.  
  251. // assign actionListener and actionEvent to firstButton
  252. firstButton.addActionListener(new ActionListener()
  253. {
  254. public void actionPerformed(ActionEvent event)
  255. {
  256. if (event.getActionCommand()== "First")
  257. {
  258. displayBooks = 0;
  259. }
  260. setTextFields();
  261. } // end firstButton actionEvent
  262. }); // end lastButton actionListener
  263.  
  264. // assign actionListener and actionEvent to previousButton
  265. previousButton.addActionListener(new ActionListener()
  266. {
  267. public void actionPerformed(ActionEvent event)
  268. {
  269. if (event.getActionCommand()== "Previous")
  270. displayBooks--;
  271. if (displayBooks < 0)
  272. {
  273. displayBooks = 0;
  274. if (displayBooks == 0)
  275. {
  276. displayBooks = displayBooks = myBook.length-1;
  277. }
  278. }
  279. setTextFields();
  280. } // end previousButton actionEvent
  281. }); // end previousBUtton actionListener
  282.  
  283. // assign actionListener and actionEvent to nextButton
  284. nextButton.addActionListener(new ActionListener()
  285. {
  286. public void actionPerformed(ActionEvent event)
  287. {
  288. if (event.getActionCommand()== "Next")
  289. displayBooks++;
  290. if (displayBooks >= myBook.length)
  291. {
  292. displayBooks = myBook.length-1;
  293. if (displayBooks == myBook.length-1)
  294. {
  295. displayBooks = 0;
  296. }
  297. }
  298. setTextFields();
  299. } // end nextButton actionEvent
  300. }); // end nextButton actionListener
  301.  
  302. // assign actionListener and actionEvent to lastButton
  303. lastButton.addActionListener(new ActionListener()
  304. {
  305. public void actionPerformed(ActionEvent event)
  306. {
  307. if (event.getActionCommand()== "Last")
  308. {
  309. displayBooks = myBook.length-1;
  310. }
  311. setTextFields();
  312. } // end lastButton actionEvent
  313. }); // end lastButton actionListener
  314.  
  315.  
  316. // assign actionListener and actionEvent to lastButton
  317. addButton.addActionListener(new ActionListener()
  318. {
  319. public void actionPerformed(ActionEvent event)
  320. {
  321.  
  322. Book2 newBookTitle = new Book2();
  323. newBookTitle.setBookTitle(bookTitleField.getText());
  324. } // end addButton actionEvent
  325. }); // end addButton actionListener
  326.  
  327.  
  328.  
  329.  
  330. // assign actionListener and actionEvent to saveButton
  331. saveButton.addActionListener(new ActionListener()
  332. {
  333. public void actionPerformed(ActionEvent event)
  334. {
  335. if (event.getActionCommand()== "Save")
  336. {
  337. FileOutputStream out;
  338. PrintStream p;
  339. try
  340. {
  341. String strDirectory = "c:/data";
  342. new File(strDirectory).mkdir();
  343. out = new FileOutputStream ("C:/data/inventory.dat");
  344. p = new PrintStream(out);
  345. for (int i = 0; i<= 5 - 1; i++)
  346. {
  347. p.println("Book Title: " + myBook[i].getBookTitle()+"\n");
  348. p.println("Item Number: " + myBook[i].getItemNumber()+"\n");
  349. p.println("Book Units: " + myBook[i].getBookUnits()+"\n");
  350. p.println("Book Price: " + myBook[i].getBookPrice()+"\n");
  351. p.println("Inventory Value: " + myBook[i].inventoryValue()+"\n");
  352. p.println("Book Restocking Fee: " + myBook[i].bookRestockingFee()+"\n");
  353. p.println("");
  354. }
  355. p.close();
  356. }
  357. catch (Exception e)
  358. {
  359. System.out.println("error");
  360. }
  361. }
  362. setTextFields();
  363.  
  364. } // end lastButton actionEvent
  365. }); // end lastButton actionListener
  366.  
  367. } // end constructor
  368.  
  369. // method to calculate the value of the entire inventory
  370. public double totalInventoryValue()
  371. {
  372. double totalInventoryValue = 0.00;
  373.  
  374. for ( int counter = 0; counter < myBook.length; counter++ )
  375.  
  376. totalInventoryValue += myBook[counter].inventoryValue();
  377.  
  378. return totalInventoryValue;
  379.  
  380. } // end method totalBookValue
  381.  
  382. // method to setTextFields
  383. private void setTextFields()
  384. {
  385. if (displayBooks == arrayCount)
  386. {
  387. displayBooks = 0;
  388. }
  389. if (displayBooks < 0)
  390. {
  391. displayBooks = arrayCount -1;
  392. }
  393. bookTitleField.setText(myBook[displayBooks].getBookTitle()+"\n");
  394. itemNumberField.setText(myBook[displayBooks].getItemNumber()+"\n");
  395. unitsInStockField.setText(myBook[displayBooks].getBookUnits()+"\n");
  396. bookPriceField.setText(myBook[displayBooks].getBookPrice()+"\n");
  397. inventoryValueField.setText(myBook[displayBooks].inventoryValue()+"\n");
  398. bookAuthorField.setText(myBook[displayBooks].getBookAuthor()+"\n");
  399. restockingFeeField.setText(myBook[displayBooks].bookRestockingFee()+"\n");
  400. } // end method setTextFields
  401.  
  402. // main method begins execution of Java application
  403. public static void main( String args [])
  404. {
  405. new Inventory_Program6a();
  406. //create JFrame
  407. Inventory_Program6a inventory = new Inventory_Program6a();
  408. inventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  409. inventory.setVisible(true);
  410.  
  411. } // end method main
  412.  
  413. } // end class Inventory_Program6a

Book2:
  1. public class Book2 extends Book
  2. {
  3.  
  4. // Book2 variable
  5. private String bookAuthor; // Author of the book
  6.  
  7.  
  8. // Book2 constructor
  9. public Book2(String bookTitle, int itemNumber, int bookUnits, double bookPrice, String bookAuthor)
  10. {
  11.  
  12. // call super class constructor
  13. super(bookTitle, itemNumber, bookUnits, bookPrice);
  14.  
  15. this.bookAuthor = bookAuthor; // initializes bookTitle for Book2
  16. } // end constructor
  17.  
  18. // method to set the book author
  19. public void setBookAuthor (String bookAuthor)
  20. {
  21. this.bookAuthor = bookAuthor;
  22. } // end method setBookAuthor
  23.  
  24. // method to retrieve the book author
  25. public String getBookAuthor()
  26. {
  27. return bookAuthor;
  28. } // end method getBookAuthor
  29.  
  30. // Calculate the stock value
  31. // Returns the total value of books in stock, adding a 5% restocking fee
  32. public double inventoryValue()
  33. {
  34. return (super.inventoryValue() * 1.05);
  35. }
  36.  
  37. // method for returning the book restocking fee
  38. public double bookRestockingFee()
  39. {
  40. return(super.inventoryValue() * 0.05);
  41. }
  42.  
  43. public String toString()
  44. {
  45. 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());
  46. }
  47.  
  48. } // end class Book2
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 101
Reputation: cms271828 is an unknown quantity at this point 
Solved Threads: 4
cms271828 cms271828 is offline Offline
Junior Poster

Re: Instantiating an Object of a Class Confusion

 
0
  #2
Oct 13th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 7
Reputation: mariejt is an unknown quantity at this point 
Solved Threads: 0
mariejt mariejt is offline Offline
Newbie Poster

Re: Instantiating an Object of a Class Confusion

 
0
  #3
Oct 13th, 2007
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......

Thanks!

I've attached my Book code:

  1. public class Book // Book has four attributes
  2. {
  3.  
  4. private String bookTitle = "";
  5. private int itemNumber = 0;
  6. private int bookUnits = 0;
  7. private double bookPrice = 0.00;
  8.  
  9. // Book constructor
  10. public Book (String bookTitle, int itemNumber, int bookUnits, double bookPrice)
  11. {
  12. this.bookTitle = bookTitle; // initializes bookTitle for Book
  13. this.itemNumber = itemNumber; // initializes itemNumber for Book
  14. this.bookUnits = bookUnits; // initializes bookUnits for Book
  15. this.bookPrice = bookPrice; // initializes bookPrice for Book
  16. } // end constructor
  17.  
  18. // method to set the book title
  19. public void setBookTitle (String bookTitle)
  20. {
  21. this.bookTitle = bookTitle;
  22. } // end method setBookTitle
  23.  
  24. // method to retrieve the book title
  25. public String getBookTitle()
  26. {
  27. return bookTitle;
  28. } // end method getBookTitle
  29.  
  30. // method to set the item number
  31. public void setItemNumber (int itemNumber)
  32. {
  33. this.itemNumber = itemNumber;
  34. } // end method setItemNumber
  35.  
  36. // method to retrieve the item number
  37. public int getItemNumber()
  38. {
  39. return itemNumber;
  40. } // end method getItemNumber
  41.  
  42. // method to set the book units
  43. public void setBookUnits (int bookUnits)
  44. {
  45. this.bookUnits = bookUnits;
  46. } // end method setBookUnits
  47.  
  48. // method to retrieve the book units
  49. public int getBookUnits()
  50. {
  51. return bookUnits;
  52. } // end method getBookUnits
  53.  
  54. // method to set the book price
  55. public void setBookPrice (double bookPrice)
  56. {
  57. this.bookPrice = bookPrice;
  58. } // end method setBookPrice
  59.  
  60. // method to retrieve the book price
  61. public double getBookPrice()
  62. {
  63. return bookPrice;
  64. } // end method getBookPrice
  65.  
  66. // method to calculate the inventory value
  67. public double inventoryValue()
  68. {
  69. return bookPrice * bookUnits;
  70. } // end method inventoryValue
  71.  
  72. } // end class Book
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 101
Reputation: cms271828 is an unknown quantity at this point 
Solved Threads: 4
cms271828 cms271828 is offline Offline
Junior Poster

Re: Instantiating an Object of a Class Confusion

 
0
  #4
Oct 13th, 2007
Ok, this is easy...

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

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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 7
Reputation: mariejt is an unknown quantity at this point 
Solved Threads: 0
mariejt mariejt is offline Offline
Newbie Poster

Re: Instantiating an Object of a Class Confusion

 
0
  #5
Oct 13th, 2007
Awesome - Thank you so much!!!!!!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: Instantiating an Object of a Class Confusion

 
0
  #6
Oct 13th, 2007
Hi mariejt,

Originally Posted by 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.

greetings
Parthiban
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 101
Reputation: cms271828 is an unknown quantity at this point 
Solved Threads: 4
cms271828 cms271828 is offline Offline
Junior Poster

Re: Instantiating an Object of a Class Confusion

 
0
  #7
Oct 13th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC