Results 1 to 10 of 10
- 07-25-2010, 02:14 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 12
- Rep Power
- 0
Solved Exception in thread "main" java.lang.ClassCastException:
I am trying to create a program for Class. I am stuck with this exception that is getting thrown. Anyone help me please. There are 5 .java files. This is due tomorrow night and I am using Netbeans to create. Thanks
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package product; /** * * @author sjjardi */ public class Product { /** * The number of the product. */ private int itemNumber; /** * The name of the product. */ private String name; /** * The stock quantity of the product. */ private int unitsInStock; /** * The unit price of the product. */ private double unitPrice; /** * A useful constructor which initializes the attributes. */ public Product(int itemNumber, String name, int unitsInStock, double unitPrice) { this.itemNumber = itemNumber; this.name = name; this.unitsInStock = unitsInStock; this.unitPrice = unitPrice; } /* * Accessor method (we need this because the related attribute is private). */ public int getItemNumber() { return itemNumber; } /* * Accessor method (we need this because the related attribute is private). */ public String getName() { return name; } /* * Accessor method (we need this because the related attribute is private). */ public double getUnitPrice() { return unitPrice; } /* * Accessor method (we need this because the related attribute is private). */ public int getUnitsInStock() { return unitsInStock; } /* * Accessor method (we need this because the related attribute is private). */ public double getValueOfProduct() { return unitsInStock * unitPrice; } /* * Mutator method (we need this because the related attribute is private). */ public void setItemNumber(int itemNumber) { this.itemNumber = itemNumber; } /* * Mutator method (we need this because the related attribute is private). */ public void setName(String name) { this.name = name; } /* * Mutator method (we need this because the related attribute is private). */ public void setUnitPrice(double unitPrice) { this.unitPrice = unitPrice; } /* * Mutator method (we need this because the related attribute is private). */ public void setUnitsInStock(int unitsInStock) { this.unitsInStock = unitsInStock; } /** * Returns a formatted representation of the product. * * Useful for print() methods. */ public String toString() { String itemStr = String.format("%-2s | ", itemNumber); String titleStr = String.format("%-20s | ", name); String unitsStr = String.format("%4d | ", unitsInStock); String costStr = String.format("$ %6.2f | ", unitPrice); String valueStr = String.format("$ %8.2f", getValueOfProduct()); return itemStr + titleStr + unitsStr + costStr + valueStr; } }Java Code:package inventoryprogrampart5; import inventory.Inventory; import inventorygui.InventoryGUI; import product.Product; public class InventoryProgramPart5 { public static void main(String args[]) { /* * Create some cell phones. */ CellPhone one = new CellPhone(1, "KG-800 Chocolate", 10, 250.00, "LG"); CellPhone two = new CellPhone(2, "Kickflip", 5, 200.00, "Helio"); CellPhone three = new CellPhone(3, "SGH-E870", 10, 185.00, "Samsung"); CellPhone four = new CellPhone(4, "W42H", 20, 124.90, "Hitachi"); /* * Add the cell phones to the inventory */ Inventory inventory = new Inventory(); inventory.add(one); inventory.add(two); inventory.add(three); inventory.add(four); /* * Sort the cell phones */ inventory.bubbleSort(); /* * Modify the Inventory Program to use a GUI */ InventoryGUI gui = new InventoryGUI(inventory); gui.setVisible(true); //gui.setVisible(true); } } /** * Represents a Cell Phone. * * A subclass of the product class that uses one additional unique feature. */ class CellPhone extends Product { /** * The brand of the cell phone */ private String brand; /** * A useful constructor which initializes the attributes. */ public CellPhone(int itemNumber, String name, int unitsInStock, double unitPrice, String brand) { super(itemNumber, name, unitsInStock, unitPrice); setBrand(brand); } /** * Returns the value of the restocking fee. */ public double getRestockingFee() { return getUnitsInStock() * getUnitPrice() * 0.05; } /** * Overrides the method getValueOfProduct() in order to add a 5% restocking * fee to the value of the inventory of that product. */ @Override public double getValueOfProduct() { return getUnitsInStock() * getUnitPrice() + getRestockingFee(); } /* * Mutator method (we need this because the related attribute is private). */ public void setBrand(String brand) { this.brand = brand; } /* * Accessor method (we need this because the related attribute is private). */ public String getBrand() { return brand; } /** * Returns a formatted representation of the product. * * Useful for print() methods. */ @Override public String toString() { String feeStr = String.format("$ %12.2f | ", getRestockingFee()); String brandStr = String.format("%-10s", brand); return super.toString() + " | " + brandStr + " | " + feeStr; } } /** * Manipulates an array of products. */ /** * The GUI should display the information one product at a time, including the * item number, the name of the product, the number of units in stock, the price * of each unit, and the value of the inventory of that product. In addition, * the GUI should display the value of the entire inventory, the additional * attribute, and the restocking fee. */Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package inventorygui; import cellphone.CellPhone; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import inventory.Inventory; /** * * @author sjjardi */ @SuppressWarnings("serial") public class InventoryGUI extends JFrame implements ActionListener { // declare the field components of the window private JTextField itemNumberTextField; private JTextField nameTextField; private JTextField unitsInStockTextField; private JTextField unitPriceTextField; private JTextField valueOfProductTextField; private JTextField restockingFeeTextField; private JTextField valueOfInventoryTextField; // declare some buttons private JButton previousButton; private JButton nextButton; private JButton firstButton; private JButton lastButtion; // declare the inventory object which will be manipulated by the GUI private Inventory inventory; // this stores the index of the current product (the product which is // displayed on the window) private int itemIndex; /** * Default constructor. Initializes the window and its components. */ private void displayProduct() { if (itemIndex == -1) { // no product itemNumberTextField.setText(null); nameTextField.setText(null); unitsInStockTextField.setText(null); unitPriceTextField.setText(null); valueOfProductTextField.setText(null); restockingFeeTextField.setText(null); valueOfInventoryTextField.setText(null); } else { // get the product by its index CellPhone p = (CellPhone) inventory.get(itemIndex); //inventory.get(itemIndex); // update the fields itemNumberTextField.setText(String.valueOf(p.getItemNumber())); nameTextField.setText(p.getName()); unitsInStockTextField.setText(String.valueOf(p.getUnitsInStock())); unitPriceTextField.setText(String.valueOf(p.getUnitPrice())); valueOfProductTextField.setText(String.valueOf(p.getValueOfProduct())); restockingFeeTextField.setText(String.valueOf(p.getRestockingFee())); valueOfInventoryTextField.setText(String.valueOf(inventory.getValue())); } } public InventoryGUI(Inventory inventory) { // set the title of the window super("Inventory Program - Part 5"); // store the inventory object this.inventory = inventory; // set the size of the window setSize(new Dimension(360, 320)); // make the application terminate when the window is closed setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // center the window on the screen setLocationRelativeTo(null); // set the layout of the window (this manages the size and // the position of the components). JPanel detailpane = new JPanel(); detailpane.setLayout(new GridLayout(7, 2)); /* * Create and set the components to display the item number */ itemNumberTextField = new JTextField(); detailpane.add(new JLabel("Item Number:")); detailpane.add(itemNumberTextField); /* * Create and set the components to display the name */ nameTextField = new JTextField(); detailpane.add(new JLabel("Name:")); detailpane.add(nameTextField); /* * Create and set the components to display the units in stock */ unitsInStockTextField = new JTextField(); detailpane.add(new JLabel("Units In Stock:")); detailpane.add(unitsInStockTextField); /* * Create and set the components to display the unit price */ unitPriceTextField = new JTextField(); detailpane.add(new JLabel("Unit Price:")); detailpane.add(unitPriceTextField); /* * Create and set the components to display the value of product */ valueOfProductTextField = new JTextField(); detailpane.add(new JLabel("Value of Product:")); detailpane.add(valueOfProductTextField); /* * Create and set the components to display the restocking fee */ restockingFeeTextField = new JTextField(); detailpane.add(new JLabel("Restocking Fee:")); detailpane.add(restockingFeeTextField); /* * Create and set the components to display the value of inventory */ valueOfInventoryTextField = new JTextField(); detailpane.add(new JLabel("Value of the entire Inventory:")); detailpane.add(valueOfInventoryTextField); // create a panel for the buttons JPanel buttons = new JPanel(new GridLayout(1, 3)); firstButton = new JButton("First"); firstButton.addActionListener(this); buttons.add(firstButton); /* * Create and set the button Previous */ previousButton = new JButton("Previous"); previousButton.addActionListener(this); buttons.add(previousButton); /* * Create and set the button Next */ nextButton = new JButton("Next"); nextButton.addActionListener(this); buttons.add(nextButton); lastButtion = new JButton("Last"); lastButtion.addActionListener(this); buttons.add(lastButtion); // add the buttons panel in the main panel // displays the company logo //set up a panel to hold the logo JPanel logoPanel = new JPanel(); JLabel logoLabel = new JLabel(new ImageIcon("CompanyLogo.jpg")); logoPanel.add(logoLabel); getContentPane().add(logoPanel, BorderLayout.NORTH); getContentPane().add(detailpane, BorderLayout.CENTER); getContentPane().add(buttons, BorderLayout.SOUTH); // display the first last item itemIndex = inventory.getCount() - 1; displayProduct(); } /** * Updates the fields to reflect the value of the attributes of the current * product. */ /** * Handle the actions to be performed by the application. This actions are * triggered by the buttons. */ @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == previousButton) { // update the item index and display the previous product (if any) if (itemIndex >= 0) { if (itemIndex == 0) { itemIndex = inventory.getCount(); } itemIndex--; displayProduct(); } } else if (e.getSource() == nextButton) { // update the item index and display the next product (if any) if (itemIndex == inventory.getCount() - 1) { itemIndex = -1; } itemIndex++; displayProduct(); } else if (e.getSource() == firstButton) { itemIndex = 0; displayProduct(); } else if (e.getSource() == lastButtion) { itemIndex = inventory.getCount() - 1; displayProduct(); } } }Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package inventory; import product.Product; /** * * @author sjjardi */ public class Inventory { /** * Use an array to store the items. */ private Product products[]; private int count; /** * Default constructor - initializes the products array */ public Inventory() { products = new Product[100]; count = 0; } /** * Add a product to the inventory */ public void add(Product p) { products[count++] = p; } /** * Return a product added to the inventory by its index. */ public Product get(int index) { return products[index]; } /** * Sort the array items by the name of the product. */ public void bubbleSort() { int n = getCount(); for (int search = 1; search < n; search++) { for (int i = 0; i < n - search; i++) { String nameA = products[i].getName(); String nameB = products[i + 1].getName(); if (nameA.compareToIgnoreCase(nameB) > 0) { Product temp = products[i]; products[i] = products[i + 1]; products[i + 1] = temp; } } } } /** * Calculate the value of the entire inventory. */ public double getValue() { double value = 0.00; for (int i = 0; i < count; i++) { value += products[i].getValueOfProduct(); } return value; } /** * Returns the number of products in the inventory. */ public int getCount() { return count; } }Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package cellphone; import product.Product; /** * * @author sjjardi */ public class CellPhone extends Product { /** * The brand of the cell phone */ private String brand; /** * A useful constructor which initializes the attributes. */ public CellPhone(int itemNumber, String name, int unitsInStock, double unitPrice, String brand) { super(itemNumber, name, unitsInStock, unitPrice); setBrand(brand); } /** * Returns the value of the restocking fee. */ public double getRestockingFee() { return getUnitsInStock() * getUnitPrice() * 0.05; } /** * Overrides the method getValueOfProduct() in order to add a 5% restocking * fee to the value of the inventory of that product. */ @Override public double getValueOfProduct() { return getUnitsInStock() * getUnitPrice() + getRestockingFee(); } /* * Mutator method (we need this because the related attribute is private). */ public void setBrand(String brand) { this.brand = brand; } /* * Accessor method (we need this because the related attribute is private). */ public String getBrand() { return brand; } /** * Returns a formatted representation of the product. * * Useful for print() methods. */ public String toString() { String feeStr = String.format("$ %12.2f | ", getRestockingFee()); String brandStr = String.format("%-10s", brand); return super.toString() + " | " + brandStr + " | " + feeStr; } }Last edited by extremeshannon; 07-27-2010 at 06:01 PM. Reason: Solved
- 07-25-2010, 02:42 AM #2
Post the full stack of the errormessage.
This will tell you which line the error is occuring.
Could you also paste the line which the error occurs.
- 07-25-2010, 03:04 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 12
- Rep Power
- 0
Thanks for the fast reply I posted them in the order they came in and the first 2 are InventoryGUI class and the third is InventoryProgramPart5 class
run:
Exception in thread "main" java.lang.ClassCastException: inventoryprogrampart5.CellPhone cannot be cast to cellphone.CellPhone
at inventorygui.InventoryGUI.displayProduct(Inventory GUI.java:66)
at inventorygui.InventoryGUI.<init>(InventoryGUI.java :194)
at inventoryprogrampart5.InventoryProgramPart5.main(I nventoryProgramPart5.java:47)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)Java Code:CellPhone p = (CellPhone) inventory.get(itemIndex);
Java Code:displayProduct();
Java Code:InventoryGUI gui = new InventoryGUI(inventory);
-
The error is telling you what's wrong: you have two CellPhone classes when you should have only one.
- 07-25-2010, 03:26 AM #5
In the Inventory class, you have: import product.Product;
So the class Product is in the package: product for this class.
The contents of the inventory object is from the package: inventoryprogrampart5
The Inventory class is in the package: inventoryprogrampart5 In the same source file there is a class definition for Product.
There are two definitions for the class CellPhone.
You need to
organize what package each class should be in,
Or change the import statement
or change the cast.
- 07-25-2010, 03:38 AM #6
Member
- Join Date
- Jul 2010
- Posts
- 12
- Rep Power
- 0
Awesome everyone thanks got it to work. Did not see I had the Cellphone class twice :) happy days
- 07-25-2010, 07:00 AM #7
Member
- Join Date
- Jul 2010
- Posts
- 12
- Rep Power
- 0
I have another question about the program. How do I get the value out put to be currency. I thought this would do it.
Java Code:public String toString() { String feeStr = String.format("$ %12.2f | ", getRestockingFee()); String brandStr = String.format("%-10s", brand); return super.toString() + " | " + brandStr + " | " + feeStr; }
- 07-25-2010, 12:45 PM #8
What is "currency"?How do I get the value out put to be currency.
What does the shown code output? Can you show that and explain why it is not what you want?
What is the super to this class? What does it contribute to the String being returned.
-
Have a look at NumberFormat.getCurrencyInstance() to get an object that can format numbers into currency Strings. Also, if you pass a locale to the getCurrencyInstance method, you can get an object that is locale-specific.
- 07-27-2010, 06:00 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 PM -
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/star/lang/XEventLi
By baktha.thalapathy in forum New To JavaReplies: 5Last Post: 06-02-2010, 01:05 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks