|
-
Feb 15th, 2005, 09:34 AM
#1
Thread Starter
Addicted Member
Help with inheritance....
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.
Last edited by NoteMe; Feb 17th, 2005 at 06:54 AM.
-
Feb 15th, 2005, 09:38 AM
#2
Thread Starter
Addicted Member
Re: Help with inheritance....
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.
Code:
/**
* 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;
}*/
}
-
Feb 15th, 2005, 09:40 AM
#3
Thread Starter
Addicted Member
Re: Help with inheritance....
My productlist class
Code:
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());
}
}
}
-
Feb 15th, 2005, 09:41 AM
#4
Thread Starter
Addicted Member
Re: Help with inheritance....
My musicCD class
Code:
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;
}
}
-
Feb 15th, 2005, 09:42 AM
#5
Thread Starter
Addicted Member
Re: Help with inheritance....
my movieDVD class
Code:
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;
}
}
-
Feb 15th, 2005, 09:46 AM
#6
Re: Help with inheritance....
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.
ØØ
-
Feb 16th, 2005, 04:02 PM
#7
Thread Starter
Addicted Member
Re: Help with inheritance....
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
Code:
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.
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());
}
}
}
-
Feb 16th, 2005, 07:53 PM
#8
Re: Help with inheritance....
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.
-
Feb 16th, 2005, 08:06 PM
#9
Thread Starter
Addicted Member
Re: Help with inheritance....
 Originally Posted by visualAd
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.
-
Feb 16th, 2005, 08:32 PM
#10
Thread Starter
Addicted Member
Re: Help with inheritance....
here is an updated Zip file with all my code on it.
-
Feb 16th, 2005, 08:48 PM
#11
Re: Help with inheritance....
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.
Code:
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();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|