Results 1 to 40 of 41

Thread: Help with inheritance....

Hybrid View

  1. #1
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Help with inheritance....

    You can, however, let a base reference point to a derived object:
    Product pr = new MusicCD(...);

    This is how you make the ProductList work. You can have it store a mixture of all Product subclasses.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  2. #2

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Re: Help with inheritance....

    Here is an update on my code since i havent posted here in a while.

    My Product superclass. No compiler errors
    Code:
    package AssignmentTwo;
    
    public abstract class Product{
    	protected String code;
    	protected String name;
    	protected double price;
    	protected String playingTime;
    
    
    	public Product(){
    	}
    
    	public Product(String code, String name, double price, String playingTime)
    		{
    			this.code = code;
    			this.name = name;
    			this.price = price;
    			this.playingTime = playingTime;
    		}
    
    	public void setCode(String c)
    		{
    			code = c;
    		}
    		
    	public String getCode()
    		{ 
    			return code;
    		}
    
    	public void setName(String n)
    		{
    			name = n;
    		}
    		
    	public String getName()
    		{ 
    			return name;
    		}
    
    	public void setPrice(double p)
    		{
    			price = p;
    		}
    		
    	public double getPrice()
    		{ 
    			return price; 
    		}
    		
    	public void setPlayingTime(String t)
    		{
    			playingTime = t;
    		}
    		
    	public String getPlayingTime()
    		{ 
    			return playingTime; 
    		}
    
        public String showInfo()
        	{
    			return this.code + "\t" + this.name + "\t" + this.price + "\t" + this.playingTime + "\t" ;
    		}
    		
    //	protected abstract String showInfo();
    My ProductList class. I get a compiler error at line 26. Saying the Product class cannot be instantiated. I know this, but how would I create a product then add it to the list if I can instantiate the Product class. I thought about doing it from the subclasses, either MusicCD or MovieDVD. But then I could only do one or the other, not both. I have marked the error in red.
    Code:
    package AssignmentTwo;
    
    import java.util.*;
    
    public class ProductList{
    	ArrayList products = new ArrayList();
    
    	public ProductList() {
    
    	}
    
    	public void add(Product p)
    		{//add a product to the list
    			
    		String key = ((Product)p).getCode();
    			if(find(key) == null)
    				{
    					products.add(p);
    				}
    		}
    
    	public void add(String id, String name, double price, String playingTime)
    		{//create a Product and add it to the list
    			for(int i = 0 ; i<products.size(); i++)
    				{
    					Product prod1 = new Product(id,name,price, playingTime);//Cannot Instantiate Product
    					products.add(prod1);
    				}
    		}
    
    	public int size()
    		{//return how many are in the list
    			return products.size();
    		}
    	
    	public Object get(int index)
    		{//return the Product at position index
    			return products.get(index);
    		}
    		
    	public void remove(int index)
    		{//remove the Product at position index
    			products.remove(index);
    		}
    	
    	public Object find(String find)
    		{//return a Product based on the ID#
    			Iterator it = products.iterator();
    			
    			while(it.hasNext())
    				{				
    					Product q = (Product)it.next();
    					if (q.getCode().equals(find))
    					{
    						return q;
    					}
    				}
    			return null;
    		}
    		
    	public Iterator it()
    	    {
    	        return products.iterator();
    	    }		
    	    
    	  	public static void main(String[] args) 
    		{
    			ProductList pr = new ProductList();
                
    			Product prod1 =  new MusicCD("111","Highway to Hell",15,"1 Hour", "AC/DC", 11);
                Product prod2 =  new MusicCD("222","Back In Black",15,"3 Hours", "AC/DC", 17);
    //            Product prod3 =  new MovieDVD("333","Rocky V",20,"2 Hours", "Stalone",);
                
                pr.add(prod1);
                pr.add(prod2);
    //            pr.add(prod3);
                /*
                Iterator it = pr.it();
                
                while (it.hasNext())            
                {
                    Product p = (Product)it.next();
                    System.out.println(p.showInfo());
                }*/
       
    			
    		}
     
    	    
    		}
    My MusicCD class. No compiler errors.

    Code:
    package AssignmentTwo;
    
    public class MusicCD extends Product{
    	
    	private String artist;
    	private int numberOfTracks;
    
    	public MusicCD() 
    		{
    			
    			artist="";
    			numberOfTracks = 0;
    			code= this.getCode();
    			name=this.getName();
    			price=this.getPrice();
    			playingTime=this.getPlayingTime();
    
    		}	
    	
    	
    	public MusicCD(String code, String name, double price, String playingTime, String a, int n) 
    		{
    			super(code, name, price , playingTime);
    			artist=a;
    			numberOfTracks = n;
    		}	
    		
    				
    	public String getArtist()
    		{
    			return artist;
    		}
    	
    	public void setArtist(String a)
    		{
    			artist=a;
    		}
    		
    	public int getNumberOfTracks()
    		{
    			return numberOfTracks;
    		}
    	
    	public void setNumberOfTracks(int n)
    		{
    			numberOfTracks = n;
    		}
    
    	public String showInfo(){
    		return this.getName() + " " + this.getPrice() + " " + 
    					this.getPlayingTime()+ artist + " " + numberOfTracks + " " + this.getCode();
    	}
    
    
    }
    My MovieDVD class. No compiler errors.
    Code:
    package AssignmentTwo;
    
    public class MovieDVD extends Product{
    	
    	private String director;
    	private int scenes;
    
    	public MovieDVD() 
    	{
    			director="";
    			scenes=0;
    			code= this.getCode();
    			name=this.getName();
    			price=this.getPrice();
    			playingTime=this.getPlayingTime();
    	}	
    	
    	
    	public MovieDVD(String code, String name, double price, String playingTime, String d, int s)
    		{
    			super(code, name, price , playingTime);
    			director=d;
    		}	
    		
    				
    	public String getDirector()
    		{
    			return director;
    		}
    	
    	public void setDirector(String d)
    		{
    			director=d;
    		}
    		
    	public int getScenes()
    		{
    			return scenes;
    		}
    	
    	public void setScenes(int s)
    		{
    			scenes=s;
    		}
    
    		
    	public String showInfo(){
    		return this.getCode()+" " + this.getName() + " " + this.getPrice() + " " + 
    					this.getPlayingTime()+ director + " " + scenes ;
    	}
    }
    And finally my ProdTest class. No compiler errors.

    Code:
    package AssignmentTwo;
    import java.util.*;
    
    public class ProdTest {
    	  	public static void main(String[] args) 
    		{
    			ProductList pr = new ProductList();
                
                
                Iterator it = pr.it();
                
                while (it.hasNext())            
                {
                    Product p = (Product)it.next();
                    System.out.println(p.showInfo());
                }
       
    			
    		}
    
    
    }
    Last edited by ddmeightball; Feb 15th, 2005 at 09:22 PM.

  3. #3

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Re: Help with inheritance....

    I wrote a test class to show polymorphism by retrieving Products from the ProductList object and executing the showInfo() method for each. I believe I got that part right, but am not sure. Someone correct me if I am wrong.

    I have been working on this on and off all day and I did what CB said. Infact, when you were posting it I had just got done implementing this in my class.

    Now, for the last problem(hopefully) for my program. I am supposed to Change the ProductList class to include these objects, the MusicCD and MovieDVD objects, instead of the generic Product objects, if needed. But how would I go about doing that?

    Someone on another site suggested this bit of code to add to my ProductList class.

    Code:
    public void add(Product p) {
    if (find(p.getCode()) == null)
    products.add(p);
    }
    The problem with this is the getCode method is used to get the code for the product. The way i coded this, is to keep duplicate products from being added to the arraylist. What they suggested is pretty much what I have done here correct?

    Code:
    	public void add(Product p)
    		{//add a product to the list
    			
    		String key = ((Product)p).getCode();
    			if(find(key) == null)
    				{
    					products.add(p);
    				}
    		}
    the method that I need to figure out how to change is this add method that creates the object and then adds it to the arraylist. The only thing is though, how would I get it to distinguish between a MusicCD and a MovieDVD???

    Code:
    	public void add(String id, String name, double price, String playingTime)
    		{//create a Product and add it to the list
    			for(int i = 0 ; i<products.size(); i++)
    				{
    					Product prod1 = new Product(id,name,price, playingTime);
    					products.add(prod1);
    				}
    		}

Posting Permissions

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



Click Here to Expand Forum to Full Width