PDA

Click to See Complete Forum and Search --> : Help with inheritance....


ddmeightball
Feb 15th, 2005, 08:34 AM
My teacher in Advanced Java class gave us an assignment on inheritance yesterday, and it is due tomorrow. We just started learning about this and I am not totally sure how to do it, I dont know even if I am doing it right. I would like some help with this assignment if thats alright with everyone.

Because pasting all of my code here in the forums would be very long, i decided to add them to this message in a zip file. I will be working on this continously all day and checking here periodically. Any help would be appreciated.

BTW:the text below is what we are supposed to do in the assignment.


Create an inheritance hierarchy for the program from Assignment 2. Make the Product class the abstract super class. Create classes for movie DVDs and music CD objects. The CD object includes the artist and the number of tracks. The DVD object includes the director. Each has a playing time value as well. Change the ProductList class to include these objects instead of the generic Product objects, if needed.

Include in the Product class a showInfo() method. Return a String which includes the data from the Product object. In the subclasses override this method to return all the data.

Write a test class to show polymorphism by retrieving Products from the ProductList object and executing the showInfo() method for each. Use a loop.

ddmeightball
Feb 15th, 2005, 08:38 AM
I was thinking, and it would probably help the rest of you just to see the code, just in case you dont have winzip or winrar or some other compression utility. In the Next 4 posts i will be posting the classes that i have written for this assignment.


/**
* Title: Assign 2
* @author Matthew Davis
* School SECC
* Class Advanced Java
*/
package AssignmentTwo;

public abstract class Product{
private String code;
private String name;
private double price;
private String time;


public Product(){
}

public Product(String code, String name, double price, String time)
{
this.code = code;
this.name = name;
this.price = price;
this.time = time;
}

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)
{
time = t;
}

public String getPlayingTime()
{
return time;
}

public String toString()
{
return code + "\t" + name + "\t" + price + "\t";
}

/* public String showInfo()
{
return string a;
}*/


}

ddmeightball
Feb 15th, 2005, 08:40 AM
My productlist class


package AssignmentTwo;

import java.util.*;

public class ProductList extends Product{
ArrayList products = new ArrayList();

public ProductList() {

}

public void add(Product p)
{//add a product to the list
//products.add(p);

String key = ((Product)p).getCode();
if(find(key) == null)
{
products.add(p);
}
}

public void add(String id, String name, double price)
{//create a Product and add it to the list
for(int i = 0 ; i<products.size(); i++)
{
Product prod1 = new Product(id,name,price);
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 Product("5248","trumpet" , 50);
Product prod2 = new Product("5420","Saxaphone" , 100);
Product prod3 = new Product("5248" , "trumpet", 50);

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.toString());
}


}
}

ddmeightball
Feb 15th, 2005, 08:41 AM
My musicCD class


package AssignmentTwo;

public class musicCD extends Product{
private String artist;
private int numberOfTracks;

public musicCD()
{
artist="";
numberOfTracks = "";
playingTime = "";

}


public musicCD(String a, int n, String p)
{
artist=a;
numberOfTracks = n;
playingTime=p;
}


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 toString(){
return artist + " " + numberOfTracks + " " + playingTime;
}

}

ddmeightball
Feb 15th, 2005, 08:42 AM
my movieDVD class


package AssignmentTwo;

public class movieDVD extends Product{
private String director;

public movieDVD()
{
director="";
playingTime = "";
}


public movieDvd(String d, String p)
{
director=d;
playingTime = p;
}


public String getDirector()
{
return director;
}

public void setDirector(String d)
{
director=d;
}

public String toString(){
return director + " " + playingTime;
}

}

NoteMe
Feb 15th, 2005, 08:46 AM
Sure we will help you on any particular question. But not do the whole assignment for you. I didn't see any questions, does that mean that you don't have any yet? If you do, please rephrase it so we can try to help.



ии

CornedBee
Feb 15th, 2005, 08:51 AM
ProductList should not inherit from Product, but be a standalone class. You have some weird capitalization inconcistencies. And my OOP teacher would criticise you for having too many public getters and setters. But for a simple programming class such as what your "Advanced Java" seems to be, that's not relevant.

NoteMe
Feb 15th, 2005, 09:09 AM
At least what I would have done is to set pointers to NULL and so on in the class the members belong too and not in the inherited class of that class. Like in your class musicCD, I can't see where you have defined playingTime.



As a comment to CBs post about Get and Set functions. I guess it can be argued up and down actualy. My teacher ment that there was no right and wrong here, but at least he need Get and Set for all the variables, he will access from the outside. It that is all of them, then at least it is better to have Get and Set functions then just making them public and access them directly.

visualAd
Feb 15th, 2005, 09:15 AM
CB is right, PrductList cannot be dreived from a product. I like to think of inheritance using the caluse is a if the statement "ProductList is a Product" holds true then you can say that Product list can inherit from product. This doesn't however.

The satement "A MusicCD is a Product" is true and therefore it can inhhreit from Product.

ddmeightball
Feb 15th, 2005, 10:00 AM
At least what I would have done is to set pointers to NULL and so on in the class the members belong too and not in the inherited class of that class. Like in your class musicCD, I can't see where you have defined playingTime.

For playingTime, this is something that both musicCD and movieDVD have in common. SO i tried to put it in the product class, so that both may have access to it since both cds and dvds have playing times. Am i doing that right in the product class?

ddmeightball
Feb 15th, 2005, 10:05 AM
ProductList should not inherit from Product, but be a standalone class. You have some weird capitalization inconcistencies. And my OOP teacher would criticise you for having too many public getters and setters. But for a simple programming class such as what your "Advanced Java" seems to be, that's not relevant.

weird capitilization inconsistencies? I changed it so productlist doesnt inherit from product anymore.

NoteMe
Feb 15th, 2005, 10:10 AM
For playingTime, this is something that both musicCD and movieDVD have in common. SO i tried to put it in the product class, so that both may have access to it since both cds and dvds have playing times. Am i doing that right in the product class?


I still can't see playingTime in the Product class though. I can see time. Is that it? Well if it is, then thats the way to go. But you should ad playingTime = ""; in the constructor of the base class (the Poduct class in this example) not in all the inherited classes. That way you don't have to do it everytime, you can just use the base class constructor.

It would also be a good thing to set a value for all your standard constructors to "" or NULL in the standard constructor. Even if you are not using it at the time. That is good programming practice.


ии

NoteMe
Feb 15th, 2005, 10:12 AM
weird capitilization inconsistencies?


I think he is talking about the capitilization of your classes. You have some that starts with a big letter, and some that starts with a small letter. Usualy in Java, classes starts with a big letter.


I changed it so productlist doesnt inherit from product anymore.


Good. Looks like you got the hang of it after all. Keep up the good work.


ии

ddmeightball
Feb 15th, 2005, 10:48 AM
ok, so here is my productList class. I am still instantiating from the product class. what would i have to do to not do that. i tried to compile the file and it said "AssignmentTwo.Product is abstract; cannot be instantiated"


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)
{//create a Product and add it to the list
for(int i = 0 ; i<products.size(); i++)
{
Product prod1 = new Product(id,name,price);
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 Product("5248","trumpet" , 50);
Product prod2 = new Product("5420","Saxaphone" , 100);
Product prod3 = new Product("5248" , "trumpet", 50);

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.toString());
}


}
}

ddmeightball
Feb 15th, 2005, 11:01 AM
also, my teacher metioned something about calling the super class in my MusicCD class, and my MovieDVD class.(btw, i corrected the capitilaization errors)

here they are.

In the MusicCD class i am getting the error that code, name, playingTime, and price has private access in AssignmentTwo.Product. He said to call the six items from the product class here

public MusicCD()
{

artist="";
numberOfTracks = 0;
code= this.getCode();
name=this.getName();
price=this.getPrice();
playingTime=this.getPlayingTime();

}


then use super() to get the Product class. I dont think i am doing what he means. What am i doing wrong? the code below is the method that i am trying to access from the Product.java class. I am trying to do that by using the super() method, but I believe i am doing it incorrectly.

public Product(String code, String name, double price, String playingTime)
{
this.code = code;
this.name = name;
this.price = price;
this.playingTime = playingTime;
}



public MusicCD(String a, int n, String p)
{
super(code, name, price,playingTime);
artist=a;
numberOfTracks = n;
}


also, this is the overridded method of the showInfo that i have been working on.

public String showInfo(){
return this.getName() + " " + this.getPrice() + " " +
this.getPlayingTime()+ artist + " " + numberOfTracks + " " + this.getCode();
}


}

CornedBee
Feb 15th, 2005, 11:30 AM
You've marked Product abstract (as the assignment requires you to), thus you can't directly instantiate it with new, i.e.
new Product(...);
is invalid. You must instantiate the subclasses, like MusicCD or MovieDVD.

ddmeightball
Feb 15th, 2005, 01:21 PM
You've marked Product abstract (as the assignment requires you to), thus you can't directly instantiate it with new, i.e.
new Product(...);
is invalid. You must instantiate the subclasses, like MusicCD or MovieDVD.

You mean like
code,name,price,playingTime,artist,numberOfTracks
Product product = new MusicCD("666","Highway to Hell",15,"1 Hour", "AC/DC", 11);

visualAd
Feb 15th, 2005, 01:33 PM
An abstract class is not a proper class, it is only partial therefore you cannot have a variable of type Product. But you cna have a music CD. So:

MusicCD product = new MusicCD("666","Highway to Hell",15,"1 Hour", "AC/DC", 11);

Would work.

ddmeightball
Feb 15th, 2005, 01:50 PM
An abstract class is not a proper class, it is only partial therefore you cannot have a variable of type Product. But you cna have a music CD. So:

MusicCD product = new MusicCD("666","Highway to Hell",15,"1 Hour", "AC/DC", 11);

Would work.
but would that still work with my ProductList CLass

ddmeightball
Feb 15th, 2005, 02:02 PM
Ok, so here is my code so far.

Product superclass


/**
* Title: Assign 3
* @author Matthew Davis
* School SECC
* Class Advanced Java
*/
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();


}

MusicCD subclass.

/**
* Title: Assign 3
* @author Matthew Davis
* School SECC
* Class Advanced Java
*/

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();
}


}


MovieDVD subclass. I am getting this error here.

invalid method declaration; return type required


/**
* Title: Assign 3
* @author Matthew Davis
* School SECC
* Class Advanced Java
*/

package AssignmentTwo;

public class MovieDVD extends Product{

private String director;

public MovieDVD()
{
director="";
code= this.getCode();
name=this.getName();
price=this.getPrice();
playingTime=this.getPlayingTime();
}


public MovieDvd(String code, String name, double price, String playingTime, String d, String p) //return type required
{
super(code, name, price,playingTime);
director=d;
}


public String getDirector()
{
return director;
}

public void setDirector(String d)
{
director=d;
}

public String showInfo(){
return this.getName() + " " + this.getPrice() + " " +
this.getPlayingTime()+ director + " " + this.getCode();
}
}

ddmeightball
Feb 15th, 2005, 02:39 PM
Ok, i am having problems with the ProductList class

i am getting the error

//AssignmentTwo.Product is abstract; cannot be instantiated

i know what that means, but how would i change my ProductList class to use MusicCD objects and MovieDVD objects instead of just generic product objects. I am supposed to still incorporate the ProductList class that i created. and I am supposd to create the objects in a test class.


Change the ProductList class to include these objects instead of the generic Product objects, if needed.
Write a test class to show polymorphism by retrieving Products from the ProductList object and executing the showInfo() method for each. Use a loop.




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)
{//create a Product and add it to the list
for(int i = 0 ; i<products.size(); i++)
{
Product prod1 = new Product(id,name,price);//AssignmentTwo.Product is abstract; cannot be instantiated
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 Product("5248","trumpet" , 50);//AssignmentTwo.Product is abstract; cannot be instantiated
Product prod2 = new Product("5420","Saxaphone" , 100);//AssignmentTwo.Product is abstract; cannot be instantiated
Product prod3 = new Product("5248" , "trumpet", 50);//AssignmentTwo.Product is abstract; cannot be instantiated

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.toString());
}


}


}

visualAd
Feb 15th, 2005, 04:19 PM
An abstract class is not a proper class, it is only partial therefore you cannot have a variable of type Product. But you cna have a music CD. So:

MusicCD product = new MusicCD("666","Highway to Hell",15,"1 Hour", "AC/DC", 11);

Would work.
I explained in the above post why this wouldn't work. You cannot create an intance of the product class (i.e. var = new Product). An abstract class is like a naked skeleton, its incomplete.

NoteMe
Feb 15th, 2005, 04:51 PM
Have they taught you about "interface" in your java course? If so, I think that is the way to go about it if you want to do it like this. That way it doesn't matter what kind of object you are using.



ии

System_Error
Feb 15th, 2005, 05:36 PM
you can't instantiate an abstract class. I haven't read much of this thread, so I don't know too much about what's going on, but I can tell you this, if it's going to be an abstract class, you probably should create an interface, implement it, and then override the methods in it.

CornedBee
Feb 15th, 2005, 06:04 PM
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.

ddmeightball
Feb 15th, 2005, 08:06 PM
Here is an update on my code since i havent posted here in a while.

My Product superclass. No compiler errors

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.

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.


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.

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.


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());
}


}


}

ddmeightball
Feb 15th, 2005, 08:20 PM
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.


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?


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???


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);
}
}

ddmeightball
Feb 15th, 2005, 08:28 PM
Have they taught you about "interface" in your java course? If so, I think that is the way to go about it if you want to do it like this. That way it doesn't matter what kind of object you are using.

Interface? The only interface that I know about is a Graphical User Interface and I know thats not what u are talking about.

NoteMe
Feb 15th, 2005, 09:31 PM
It is a bit late to make you a simple example here. But I found an old example of mine on Linked Lists that uses interface. Have attached the sample. But unfortunatly the class names and the comments and so on are in Norwegian. Tell me if I should try to comment some parts (or all of it to English) and I will try to do it before I go to sleep.


If you look at the "class" NoekkelType (KeyType in English) it is an interface. If has two functions. If you look at the file that is called Heltall (Integer in English) you will see that there is a class called "heltall" about 20 lines down, it is a class that implements this interface called Noekeltype. That means that it has to have defined the functions that is in Noekeltype. If it does you can say that "Heltall" is a kind of "NoekelType" and I can use it as if it was a "NoekelType". That way I can make this linked list work with all kinds of classes. No matter if it is objects of type car, or house, or cd, or what ever.


But if the teacher havn't thought you this yet. Then I am guessing (and recomend) that you go for CornedBees solution in his last post. It is probably what your teacher is after, since they havn't thought you about interfaces yet.

ddmeightball
Feb 16th, 2005, 10:10 AM
DO you mean like actionListeners and windowListeners, etc?

NoteMe
Feb 16th, 2005, 10:33 AM
DO you mean like actionListeners and windowListeners, etc?


No, that is more for events like clicking on a button and so on.

This is to make all classes that meats the requirement of the "KeyType" look like the same type of object for Java. Has nothing to do with GUI or events at all.



ии

ddmeightball
Feb 16th, 2005, 03:02 PM
Ok, this is the code that i have been working on to add CD and DVD objects to my arrayList. The only problem is in my ProdTest class, it isnt printing out anything. help please


public void addCD(Product p, String code, String name, double price, String playingTime, String a, int n){
String key = ((Product)p).getCode();
if(find(key) == null)
{
Product prod1 = new MusicCD(code, name, price, playingTime, a, n);
products.add(prod1);
}

}

public void addDVD(Product p, String code, String name, double price, String playingTime, String d, int s){
String key = ((Product)p).getCode();
if(find(key) == null)
{
Product prod1 = new MovieDVD(code, name, price, playingTime, d, s);
products.add(prod1);
}

}



Here is my ProdTest class
this is what i am supposed to do with it


Write a test class to show polymorphism by retrieving Products from the ProductList object and executing the showInfo() method for each. Use a loop.


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());
}


}


}

visualAd
Feb 16th, 2005, 06:53 PM
Forgive me if I am wrong - but it looks to me as if you have nothing in your product list. If you have nothing in the list then nothing will print out.

ddmeightball
Feb 16th, 2005, 07:06 PM
Forgive me if I am wrong - but it looks to me as if you have nothing in your product list. If you have nothing in the list then nothing will print out.

I have the Product objects in the main method part of my ProductList class.

ddmeightball
Feb 16th, 2005, 07:32 PM
here is an updated Zip file with all my code on it.

visualAd
Feb 16th, 2005, 07:48 PM
I discussed this briefly with NoteMe and he said that you can only have one main function - in the code you last posted, it will execute the main function in the Prodtest class.

Looking at your code in the ZIP file your ProductList class is still incorrectly derived from the Product class, like CornedBee said in an earlier post, this is incorrect.

If you edit your ProdTest class to this then it should work.

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);

pr.add(prod1);
pr.add(prod2);

Iterator it = pr.it();

while (it.hasNext())
{
Product p = (Product)it.next();

System.out.println(p.showInfo());
}
}

I am not a Java programmer so I cannot test this code - but the line:

Product p = (Product)it.next();

Which casts object to a product may give you problems, esentially a MusicCD ad MusicDVD are both Products so the cast is not necessary and may well be illegal. If it does throw an error try to remove the type cast:

Product p = it.next();

ddmeightball
Feb 16th, 2005, 08:00 PM
I discussed this briefly with NoteMe and he said that you can only have one main function - in the code you last posted, it will execute the main function in the Prodtest class.

Looking at your code in the ZIP file your ProductList class is still incorrectly derived from the Product class, like CornedBee said in an earlier post, this is incorrect.

If you edit your ProdTest class to this then it should work.

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);

pr.add(prod1);
pr.add(prod2);

Iterator it = pr.it();

while (it.hasNext())
{
Product p = (Product)it.next();

System.out.println(p.showInfo());
}
}

I am not a Java programmer so I cannot test this code - but the line:

Product p = (Product)it.next();

Which casts object to a product may give you problems, esentially a MusicCD ad MusicDVD are both Products so the cast is not necessary and may well be illegal. If it does throw an error try to remove the type cast:

Product p = it.next();

I know, it would make it easier do this program if I could put the objects that I created in the main method of the ProdTest class. But I have to access them from the ProductList class. So i need to take them out of the ProductList main method and put the somewhere else. Could I put them somewhere in the ProductList class??

here is the requirements for the assignment so you can see what the teachers wants me to do in it.


Create an inheritance hierarchy for the program from Assignment 2. Make the Product class the abstract super class. Create classes for movie DVDs and music CD objects. The CD object includes the artist and the number of tracks. The DVD object includes the director. Each has a playing time value as well. Change the ProductList class to include these objects instead of the generic Product objects, if needed.

Include in the Product class a showInfo() method. Return a String which includes the data from the Product object. In the subclasses override this method to return all the data.

Write a test class to show polymorphism by retrieving Products from the ProductList object and executing the showInfo() method for each. Use a loop.



here is what I am supposed to do in the test class.

Write a test class to show polymorphism by retrieving Products from the ProductList object and executing the showInfo() method for each. Use a loop.

ddmeightball
Feb 16th, 2005, 09:56 PM
Well, i would like to thank everyone for the help that you all have given me. I have gotten the assignment to work correctly. I had totally wrong ideas of polymorphism and what it was about. Thanks to all of your help.

BTW: anyone who wants it, there is a zip file with my finished assignment, working and all.

visualAd
Feb 17th, 2005, 05:27 AM
The idea behind polymophism is the ability of two objects to be able to respond to the same message independantly but with the same outcome. You can apply the real world anaology of a cheetah and a bird.

Both of these are subclasses of the animal superclass and both understand the message "move faster". They also both respond to this message with the same outcome, which is to move a greater distance in a smaller amount of time. However, both object achieve this in a different way; the bird flaps its wings faster and the cheetha moves its legs faster.

Back to your assignment, you use polymorphism to in the showinfo method. The requiments of the method is to output all information held by the product object. As the MusicCD and MusicDVD objects both have different attributes the showinfo method has been overridden to show the information specific to that object too.

Another programming task in which polymophism can be used is in a database abstraction layer. Where two objects, each for a different database management system, implement the same interface. However their methods for say executing a query will be different as the API for different databases are different. The beauty of this is you can transparently switch bwetween diffeerent databases without changing a line of code which uses them.

P.s: Don't forget to edit your first post and change its icon to a http://www.vbforums.com/images/icons/completeclear.gif to show its resolved.

NoteMe
Feb 17th, 2005, 05:53 AM
Just one last comment in addition to the others. If you see in your special constructor. You are calling super:


public MusicCD(String code, String name, double price, String playingTime, String a, int n){
super(code, name, price , playingTime);
artist=a;
numberOfTracks = n;
}



That will make the constructor do the work for you. But in the standard constructor, you are not doing this.


public MusicCD(){
artist="";
numberOfTracks = 0;
code= this.getCode();
name=this.getName();
price=this.getPrice();
playingTime=this.getPlayingTime();
}


Imagine now that you have 100 classes like MusicCD and MusicDVD and so on. If you then want to change one of the member variables names or something, then you have to change that in all the 100 classes. It is much better to just call super and let it handle all the extra work for you. It even saves some extra lines of code for you.


Sorry that we didn't have more time to help you out this time. But you are welcome back if you have more questions.


PS: When a thread is resolved here at VBF. We always add the green checkmark to the first post. So everyone else can see that it is resolved. I can do it for you this time though.



ии

ddmeightball
Feb 17th, 2005, 07:58 AM
thanks. I didnt know about the Green check mark. I havent been in the forums long. Yeah, I was thinking about that. that I would have to change all 100 variables in the classes, but the idea of this assignment was just to show polymorphism on a small scale. I asked the teacher. He said that we will be dealing with it on a much larger scale in coming assignments.