Results 1 to 3 of 3
  1. #1
    Nixx04 is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Can someone tell me what I am doing wrong here?

    I have a piece of code I am working on for an assignment in class. I have no compiler errors but when I run the program, I get the following error:

    Exception in thread "main" java.lang.ClassCastExeption: MobilePhone cannot be cast to MPhone at InventoryWk7.main(InventoryWk7.java:226)

    Here is my code so far if someone would be kind enough to take a look:



    Java Code:
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.NumberFormat; // Program uses Number Formatting
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.util.Arrays;
    import java.util.Comparator;
    
    class MobilePhone implements Comparable<MobilePhone>
    {
    	private int itemNumber;
    	private String name;
    	private String department;
    	private int units;
    	private double price;
    	MPhone mphone;
    
    	public MobilePhone(MPhone mphone)
    	{
    		this.mphone = mphone;
    	}
    
    	public MobilePhone()
    	{
    		itemNumber = 0;
    		name = "";
    		department = "";
    		units = 0;
    		price = 0;
    	}
    
    	public MobilePhone(int itemNumber, String name, String department, int units, double price, String moblieOS)
    	{
    		this.itemNumber = itemNumber;
    		this.name = name;
    		this.department = department;
    		this.units = units;
    		this.price = price;
    	}
    
    	public void setItemNumber(int itemNumber)
    	{
    		this.itemNumber = itemNumber;
    	}
    
    	public int getItemNumber()
    	{
    		return itemNumber;
    	}
    
    	public void setName(String name)
    	{
    		this.name = name;
    	}
    
    	public String getName()
    	{
    		return name;
    	}
    
    	public void setDepartment(String department)
    	{
    		this.department = department;
    	}
    
    	public String getDepartment()
    	{
    		return department;
    	}
    
    	public void setUnits(int units)
    	{
    		this.units = units;
    	}
    
    	public int getUnits()
    	{
    		return units;
    	}
    
    	public void setPrice(double price)
    	{
    		this.price = price;
    	}
    
    	public double getPrice()
    	{
    		return price;
    	}
    
    	public double inventoryValue()
    	{
    		return units * price;
    	}
    
    	public int compareTo(MobilePhone compareMobilePhone)
    	{
    		int compareUnits = ((MobilePhone) compareMobilePhone).getUnits();
    
    		return this.units - compareUnits;
    	}
    
    	public static Comparator<MobilePhone> MobilePhoneNameComparator = new Comparator<MobilePhone>()	
    	{
    		
    		public int compare (MobilePhone MobilePhone1, MobilePhone MobilePhone2) 
    		{
    
    			String MobilePhoneName1 = MobilePhone1.getName().toUpperCase();
    			String MobilePhoneName2 = MobilePhone2.getName().toUpperCase();
    
    			return MobilePhoneName1.compareTo(MobilePhoneName2);
    
    			// Decending order = return MobilePhoneName2.compareTo(MobilePhoneName1);
    		}
    	};
    
    	class MPhone extends MobilePhone
    	{
    		private String mobileOS;
    
    		public MPhone()
    		{
    			super();
    			mobileOS = "";
    		}
    
    		public MPhone(int itemNumber, String name, String department, int units, double price, String mobileOS)
    		{
    			super(itemNumber, name, department, units, price, mobileOS);
    			this.mobileOS = mobileOS;
    		}
    
    		public double getRestockingFee()
    		{
    			return getPrice() * getUnits() * 0.05;
    		}
    
    		public double inventoryValue()
    		{
    			return getPrice() * getUnits();
    		}
    
    		public void setMobileOS(String mobileOS)
    		{
    			this.mobileOS = mobileOS;
    		}
    
    		public String getMobileOS()
    		{
    			return mobileOS;
    		}
    	}
    }
    
    
    
    class Inventory 
    {
    	private MobilePhone[] phone;
    
    	public Inventory(MobilePhone[] phone)
    	{
    		this.phone = phone;
    	}
    
    	public double totalValue()
    	{
    		double total = 0;
    		for (int i = 0; i < phone.length; i++)
    		{
    			total += phone[i].inventoryValue();
    		}
    		
    		return total;
    	}
    
    	public int getSize()
    	{
    		return phone.length;
    	}
    
    	public MobilePhone get(int idx)
    	{
    		return phone[idx];
    	}
    }
    
    public class InventoryWk7
    {
    	public static void main(String[] args)
    	{
    		// Declare variable departmentName
            String departmentName;
            departmentName = "Electronics";
    
            // Creation of MobilePhone Array
            MobilePhone[] phone = new MobilePhone[7];
    
            phone[0] = new MobilePhone(438551, "Motorolla Droid", departmentName, 28, 280.00, "Android OS");
            phone[1] = new MobilePhone(438552, "LG G2", departmentName, 15, 225.00, "Android OS");
            phone[2] = new MobilePhone(438553, "HTC One Mini", departmentName, 11, 175.00, "Android OS");
            phone[3] = new MobilePhone(438554, "Apple Iphone 5S", departmentName, 35, 330.00, "Apple iOS");
            phone[4] = new MobilePhone(438555, "Samsung Galaxy Mega 6.3", departmentName, 25, 210.00, "Android OS");
            phone[5] = new MobilePhone(438556, "Nokia Lumia 625", departmentName, 22, 154.00, "Windows Mobile");
            phone[6] = new MobilePhone(438557, "Samsung Galaxy Note 3", departmentName, 17, 296.00, "Android OS");
    
            Inventory invent = new Inventory(phone);
    
                    
            Arrays.sort(phone);
            for (int i =0; i < phone.length; i++)
            {
            	MPhone prod = (MPhone)invent.get(i);
            	NumberFormat nf = NumberFormat.getCurrencyInstance();
            	System.out.println("Department: " + phone[i].getDepartment());
            	System.out.println("Asset Number: " + phone[i].getItemNumber());
            	System.out.println("Asset Name: " + phone[i].getName());
            	System.out.println("Operating System: " + prod.getMobileOS());
            	System.out.println("Units on hand: " + phone[i].getUnits());
            	System.out.println("Price per unit: " + nf.format(phone[i].getPrice()));
            	System.out.println("Total Inventory value of units: " + nf.format(phone[i].inventoryValue()));
            	System.out.println("Restocking Fee: " + nf.format(prod.getRestockingFee()));
            }
    
            NumberFormat nf = NumberFormat.getCurrencyInstance();
            System.out.println("\n\nTotal Value of all units in Warehouse: " + nf.format(invent.totalValue()));
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can someone tell me what I am doing wrong here?

    A super class object B isn't necessarily a derived class object D (where D extends B); this is exactly what you're trying to do: you create a MobilPhone object (the super class object) and you're trying to cast it to a derived class object MPhone ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Nixx04 is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Re: Can someone tell me what I am doing wrong here?

    Quote Originally Posted by JosAH View Post
    A super class object B isn't necessarily a derived class object D (where D extends B); this is exactly what you're trying to do: you create a MobilPhone object (the super class object) and you're trying to cast it to a derived class object MPhone ...

    kind regards,

    Jos
    Thanks Jos, I think I understand. I am clearly missing something. I will do some research on this to get a better understanding of it. I really appreciate the feedback and quick response.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. I can't find anything wrong with this but somehow it's wrong.
    By Biscuit Tickler in forum New To Java
    Replies: 2
    Last Post: 09-12-2012, 09:28 PM
  3. What did I do wrong?
    By Astralogic in forum New To Java
    Replies: 6
    Last Post: 04-05-2012, 12:16 PM
  4. Replies: 10
    Last Post: 07-16-2010, 12:34 AM
  5. what am i doing wrong here??
    By tornbacchus in forum New To Java
    Replies: 19
    Last Post: 04-16-2009, 03:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •