Results 1 to 9 of 9
  1. #1
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default polymorphic arrays

    Java Code:
    public abstract class Asset 
    {
     	private int serialNum;
     	private double value;
    	private String descrip;
    	
    	public Asset(int serialNum, double value, String descrip)
    	{
    		this.serialNum = serialNum; this.value = value; this.descrip = descrip;
    	}
    	
    	public void setSerialNum(int serialNum)
    	{
    		this.serialNum = serialNum;
    	}
    	
    	public int getSerialNum()
    	{
    		return serialNum;
    	}
    	
    	public void setValue(double value)
    	{
    		this.value = value;
    	}
    	
    	public double getValue()
    	{
    		return value;
    	}
    	public void setDescrip(String descrip)
    	{
    		this.descrip = descrip;
    	}
    	
    	public String getDescrip()
    	{
    		return descrip;
    	}
    	
    	public void printDetails()
    	{
    		System.out.println("Serial num: " + getSerialNum());
    		System.out.println("Value : " + getValue());
    		System.out.println("Description: " + getDescrip());
    	}
    
    }
    
    
    public class Electronics extends Asset 
    {
    	private String rating;
    
    	public Electronics(int serialNum, double value, String descrip,String rating) 
    	{
    		super(serialNum, value, descrip);
    		this.rating = rating;
    		
    	}
    	
    	public void setRating(String rating)
    	{
    		this.rating = rating;
    	}
    	public String getRating()
    	{
    		return rating;	
    	}
    	
    	public void printDetails()
    	{
    		super.printDetails();
    		System.out.println("Rating: " + getRating() + "\n");
    	}
    	
    	public void depreciate()
    	{
    		setValue(getValue() * 80);
    	}
    
    }
    
    
    
    public class Vehicles extends Asset
    {
    	private String regNum;
    	
    	public Vehicles(int serialNum, int value, String descrip, String regNum)
    	{
    		super(serialNum, value, descrip);
    		this.regNum = regNum;
    		
    	}
    	
    	public void setRegNum(String regNum)
    	{
    		this.regNum = regNum;
    	}
    	public String getRegNum()
    	{
    		return regNum;	
    	}
    	
    	public void printDetails()
    	{
    		super.printDetails();
    		System.out.println("Reg number: " + getRegNum()+ "\n");
    	}
    	
    	public void depreciate()
    	{
    		setValue(getValue() * 85);
    	}
    
    
    }
    
    
    
    
    public class Furniture extends Asset
    
    {
    	private String type; //bedroom, kitchen, etc...
    	
    	public Furniture(int serialNum, int value, String descrip, String type) 
    	{
    		super(serialNum, value, descrip);
    		this.type = type;
    	}
    	
    	public void setType(String Type)
    	{
    		this.type = type;
    	}
    	public String getType()
    	{
    		return type;	
    	}
    	
    	public void printDetails()
    	{
    		super.printDetails();
    		System.out.println("Type : " + getType()+ "\n");
    	}
    	
    	public void depreciate()
    	{
    		setValue(getValue() * 90);
    	}
    
    
    }
    
    
    public class TestAsset
    {
    	public static void main(String[] args) 
    	{
    		Asset assetCollection[] = {new Electronics(111, 155, "TV", "A"),new Electronics(222, 300, "Dishwasher", "A+"),
    								   new Vehicles(333, 20000, "Ford Focus", "08 D 75845"), new Vehicles(444, 80000, "Audi A8", "09 D 45"),
    								   new Furniture(555, 200, "Locker", "Bedroom"), new Furniture(666, 600 , "Bed", "Bedroom")};
    		
    		System.out.println("Total number assets: " + assetCollection.length);
    		
    		for(int i = 0; i< assetCollection.length; i++)
    		{
    			assetCollection[i].printDetails();
    		}
    		
    		/*for(int i = 0; i< assetCollection.length; i++)
    			{
    				int totalValue =
    			}
    		
    		for(int i = 0; i< assetCollection.length; i++)
    		{
    			assetCollection[i].depreciate();
    			assetCollection[i].printDetails();
    		}*/
    	}
    
    }

    Tasks:
    Create a polymorphic array of two of each item. DONE

    Call the display details method for each item in the array. DONE

    Calculate and display the total value of all the items in the array. PROBLEM
    - I tried this using a for loop but i'm stuck.... do i create an int for totalValue in the loop as above? i want to get the total of all the items in the array, across 6 objects of 3 types, how do i access a specific variable in that way and get the total??

    Call the depreciate method for each item in the array followed by the display details method and note the change in values.
    -havn't got this far yet but i assume i'll have a problem. i could just create 3 differant methods in my parent class, one for sub class, eg vehicleDepr() etc, and then call them in the test class, but surely there's a more efficient way to this?

    Display the total value of all items again. - havnt got here yet

    all help, hints and suggestions appreciated.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by smallmos1 View Post
    Calculate and display the total value of all the items in the array. PROBLEM
    - I tried this using a for loop but i'm stuck.... do i create an int for totalValue in the loop as above? i want to get the total of all the items in the array, across 6 objects of 3 types, how do i access a specific variable in that way and get the total??
    You want to sum up in the loop. Anything declared in the loop will be redeclared each time round, so declaring the totalValue in the loop won't work. You need to declare it before going into the loop.

    As for getting the value of an Asset, is there a method you could use? Once you have that, then you can just add it to totalValue.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by smallmos1 View Post
    Calculate and display the total value of all the items in the array. PROBLEM

    you declare your variable inside the loop for each iteration the variable is overwritten. use the following code as example

    Java Code:
            int totalValue = 0;
    
            for (int i = 0; i < assetCollection.length; i++) {
                totalValue += assetCollection[i].getValue();
            }

    and the output will be

    Total value: 101255.0
    Last edited by j2me64; 10-13-2010 at 09:00 PM.

  4. #4
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    thanks for the replies.

    i was thinking something like that but as Tolls said i have to delcare the varible outside the loop.

    so basically you are saying that totalValue is initially 0. then each time the loop iterates the value of assetCollection[0] is added, then assetCollection[1] is added on etc etc.

    does the += operator literally add the next value in the array onto the previous?

    could i not say: totalValue = totalValue + assetCollection[i].getValue();
    or is that the same as '+='?

    sorry for all the q's...just wanna make sure i understand what's happening rather than copy and paste the snippet in.

    thanks again.

  5. #5
    d4v1dw1r7h is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default

    Quote Originally Posted by smallmos1 View Post
    thanks for the replies.

    could i not say: totalValue = totalValue + assetCollection[i].getValue();
    or is that the same as '+='?
    yes, '+=' is the same as totalValue= totalValue + assetCollection[i].getValue();

  6. #6
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    I've now been asked to:

    Call the depreciate method for each item in the array followed by the display details method and note the change in values.

    Depreciate the value of each item of each subclass by;

    Electronic Appliances 20%
    Vehicles 15%
    Furniture - 10%


    I'm having trouble doing this. My array is polymorphic, 6 objects of 3 differant types. I've tries a few differant ways:

    I had a depreciate method in each subclass which depreciated the value accordingly.

    But when I tried this code in my test class:

    Java Code:
    for(int i = 0; i< assetCollection.length; i++)
    		{
                         assetCollection[i].depreciate();
    		}
    i was prompted by eclipse to change the code to this:

    Java Code:
    for(int i = 0; i< assetCollection.length; i++)
    		{
    			((Electronics)assetCollection[i]).depreciate();
    			((Vehicles) assetCollection[i]).depreciate();
    			((Furniture) assetCollection[i]).depreciate();
    		}
    which ran but gave me errors.

    I then tried to add this code to my parent class:

    Java Code:
    public void depreciate(Asset a)
    	{
    		if(a instanceof Electronics)
    		{
    			Electronics e = (Electronics)a;
    			e.depreciate();
    		}
    		else if(a instanceof Vehicles)
    		{
    			Vehicles v = (Vehicles)a;
    			v.depreciate();
    		}
    		else if(a instanceof Furniture)
    		{
    			Furniture f = (Furniture)a;
    			f.depreciate();
    		}
    still no luck. can anyone help me? what's the best way to call 3 differant methods that change 3 variables (value) that are of three differant types, all from a polymorphic array?

  7. #7
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    Let me try and phrase it better.

    parent class: Asset
    three sub-classes: Electronics, Vehicles, Furniture. All contain the inherited variable - double value.

    I have created six objects in a polymorphic array in the test class:

    Java Code:
    Asset assetCollection[] = {new Electronics(111, 155, "TV", "A"),new Electronics(222, 300, "Dishwasher", "A+"),
    								   new Vehicles(333, 20000, "Ford Focus", "08 D 75845"), new Vehicles(444, 80000, "Audi A8", "09 D 45"),
    								   new Furniture(555, 200, "Locker", "Bedroom"), new Furniture(666, 600 , "Bed", "Bedroom")};
    I was told to create a method, depreciate(), that would depractiate the value of double value by given amounts (can be seen in last post). Because of these differing amounts of depreciation, should I create the method in the parent class and use the super. function, or give each sub-class its own method?

    Once i have this sorted, how do we call the depreciate method in the test class? and how do we do so when the variable double value is instantiated in a polymorphic array? Do i have to do it for each object individually or can i use a loop?

    all help appreciated, thanks.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Yep.
    Single method in the parent, which uses a single depreciation percentage attribute in the parent...but that value is set in the constructor of the child classes. So Electronics sets it to 20%, Vehicles to 15% etc.

    You'll call depreciate() as you did in the first code snippet you gave two posts up.

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

    Default

    Quote Originally Posted by smallmos1 View Post
    Java Code:
    public void depreciate(Asset a)
    	{
    		if(a instanceof Electronics)
    		{
    			Electronics e = (Electronics)a;
    			e.depreciate();
    		}
    		else if(a instanceof Vehicles)
    		{
    			Vehicles v = (Vehicles)a;
    			v.depreciate();
    		}
    		else if(a instanceof Furniture)
    		{
    			Furniture f = (Furniture)a;
    			f.depreciate();
    		}
    An excellent example of inheritance: those classes have a parent class (Asset) and if that parent class also has a depreciate() method the other three classes inherit that method; any of those objects are Asset objects (the extend from that class) so you can call that method on them.

    Of course the extending classes can reimplement that method.

    kind regards,

    Jos

Similar Threads

  1. Need Help with Arrays!
    By kassoli in forum New To Java
    Replies: 3
    Last Post: 06-03-2010, 05:19 AM
  2. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Arrays
    By karabo101 in forum New To Java
    Replies: 12
    Last Post: 10-11-2009, 05:02 PM
  5. Help!! With arrays
    By ookie833 in forum New To Java
    Replies: 8
    Last Post: 12-14-2008, 12:57 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
  •