Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help, please.

  1. #1
    Junior Member Dragonkndr712's Avatar
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help, please.

    I am having problem with class extending a class. I have the classes needed wrote correctly but when writingthe test case there is an error message.

    import javax.swing.JOptionPane;
     
     
     
    public class ComputerTest
    {
    	public void main(String args[])
    	{
    		String message = "";
    		Computer computerArray[] = new Computer[5];
     
    		String dataArray[][] = {{"Laptop", "Sony", "Detroit", "599.99", "3", "15"},
    					{"Desktop", "Dell", "Houston", "1150.00", "5", "Inspiron"},
    					{"Desktop", "HP", "Los Angeles", "925.00", "2", "Pavilion"},
    					{"Laptop", "HP", "Los Angeles", "699.99", "12", "17"}, 
    					{"Laptop", "Apple", "Cupertino", "1399.00", "7", "13"}};
     
    		for(int i = 0; i < computerArray.length; i++)
    		{
    			if(dataArray[i][0].equals("Laptop"))
    				computerArray[i] = new Laptop(new Brand(dataArray[i][1], dataArray[i][2]), 		
     
    			Double.parseDouble(dataArray[i]									
     
    			[3]),Integer.parseInt(dataArray[i][4]), Integer.parseInt(dataArray[i]				
     
    			[5]));
     
    			else if(dataArray[i][0].equals("Desktop"))
    				computerArray[i] = new Desktop(new Brand(dataArray[i][1], dataArray[i][2]), 		
     
    			Double.parseDouble(dataArray[i]									
     
    			[3]),Integer.parseInt(dataArray[i][4]), dataArray[i][5]);
    		}
     
    		for(int i = 0; i < computerArray.length; i++)
    		{
     
    			if (computerArray != null)
    				computerArray[i].calculateCharge(computerArray[i].getQuantity());
     
     
     
    			if (computerArray[i].getClass().getName().equals("Laptop"))
    				message += String.format("Laptop %s %.2f %s inches \n",
    					computerArray[i].getBrand(), computerArray[i].getTotalCharge(), 		
     
    					computerArray[i].Laptop.getSize());
    			else if (computerArray[i].getClass().getName().equals("Desktop"))
    				message += String.format("Desktop %s %.2f %s \n",
    					computerArray[i].getBrand(), computerArray[i].getTotalCharge(), 		
     
    					computerArray[i].getCategory());
     
     
    		}
    		JOptionPane.showMessageDialog(null, message);
    	}
    }

    The error code looks like:

    CommandPromptComputerTest.jpg

    If more info is needed please let me know.

  2. #2
    Junior Member Dragonkndr712's Avatar
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help, please.

    I decided to put up my classes in case there is something I am missing, although they compile fine.
    The 2 errors in the code, one is supposed to come from Laptop and on from Desktop. I do not understand why though.
    This one is Brand:
    public class Brand
    {
    	private String name;
    	private String city;
     
    	public Brand()
    	{
    		setName("");
    		setCity("");
    	}
     
    	public Brand(String name, String city)
    	{
    		setName(name);
    		setCity(city);
    	}
     
    	public void setName(String name)
    	{
    		this.name = name;
    	}
     
    	public void setCity(String city)
    	{
    		this.city = city;
    	}
     
    	public String getName()
    	{
    		return name;
    	}
     
    	public String getCity()
    	{
    		return city;
    	}
     
    	public String toString()
    	{
    		return("Name" + name + "City" + city);
    	}
    }

    This one is Computer:
    public class Computer
    {
    	private Brand brand;
    	private double price;
    	private int quantity;
    	private double totalCharge;
     
    	public Computer()
    	{
    		setBrand(new Brand());
    		setPrice(0.0);
    		setQuantity(0);
     
    	}
     
    	public Computer(Brand brand, double price, int quantity)
    	{
    		setBrand(brand);
    		setPrice(price);
    		setQuantity(quantity);
     
    	}
     
    	public void setBrand(Brand brand)
    	{
    		this.brand = brand;	
    	}
     
    	public Brand getBrand()
    	{
    		return brand;
    	}
     
    	public void setPrice(double price)
    	{
    		this.price = price;
    	}
     
    	public void setQuantity(int quantity)
    	{
    		this.quantity = quantity;
    	}
     
    	public void setTotalCharge(double totalCharge)
    	{
    		this.totalCharge = totalCharge;
    	}
     
     
     
    	public double getPrice()
    	{
    		return price;
    	}
     
    	public int getQuantity()
    	{
    		return quantity;
    	}
     
     
     
    	public double getTotalCharge()
    	{
    		return totalCharge;
    	}
     
    	public String toString()
    	{		
    		return(brand.toString() + "Price" + price + "Quantity" + quantity);
    	}
     
    	public void calculateCharge(double quantitySold)
    	{
    		setTotalCharge(price * quantitySold);
     
    	}
     
     
    }

    This one is Laptop extends Computer:
    public class Laptop extends Computer
    {
    	private int size;
     
    	public Laptop()
    	{
    		super();
    		setSize(0);
    	}
     
    	public Laptop(Brand brand, double price, int quantity, int size)
    	{	
    		super(brand, price, quantity);
    		setSize(size);
    	}
     
    	public void setSize(int size)
    	{
    		this.size = size;
    	}
     
    	public int getSize()
    	{
    		return size;
    	}
     
    	public String toString()
    	{
    		return(super.toString() + "Size" + size);
    	}
    }

    This last one is Desktop extends Computer:

    public class Desktop extends Computer
    {
    	private String category;
     
    	public Desktop()
    	{
    		super();
    		setCategory("");
    	}
     
    	public Desktop(Brand brand, double price, int quantity, String category)
    	{
    		super(brand, price, quantity);
    		setCategory(category);
    	}
     
    	public void setCategory(String category)
    	{
    		this.category = category;
    	}
     
    	public String getCategory()
    	{
    		return category;
    	}
     
    	public String toString()
    	{
    		return(super.toString() + "Category" + category);
    	}
    }

  3. #3
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Need help, please.

    You need to type cast computerArray[i] to specific class
    like : Laptopn lap = (Laptop) computerArray[i];
    and then use
    lap.getSize();

    Similarly you can type cast for Desktop
    Thanks and Regards
    Dan Brown

    Common Java Mistakes