| | |
Class and Sub Class
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
I thought I understood how this worked, but maybe I do not.
I intentionally left a field out of my compactdisk class (artist) so that I could create a sub class later, impliment it, and see how that whole thing worked.
Sounded simple, I still think it is, I just think I am doing something wrong.
Here is my original class:
[CODE][import java.lang.Comparable;
public class Compactdisk implements Comparable
{// begin class
//InventoryCD class has 5 fields
private String name; // Name of cd
private float price; // price of cd
private int itemno; // item number of cd
private int nstock; // how many units in stock
private int i; // cd counter for array
private float value; // value for single cd inventory
//Compact disk class constructor
public Compactdisk()
// 4 fields need to be set up
{
name = "";
price = 0;
itemno = 0;
nstock = 0;
i = 0;
value = 0;
}
// set values
public void setName(String diskName)
{
name = diskName;
}
public void setPrice(float cdPrice)
{
price = cdPrice;
}
public void setItemno(int cdItemno)
{
itemno = cdItemno;
}
public void setNstock(int cdStock)
{
nstock = cdStock;
}
public void setValue(float cdValue)
{
value = cdValue;
}
public void seti(int Count)
{
i = Count;
}
// return values
public String getName()
{
return (name);
}
public float getPrice()
{
return (price);
}
public int getItemno()
{
return (itemno);
}
public int getNstock()
{
return (nstock);
}
public int compareTo(Object in)
{
return ((Comparable)name.toLowerCase()).compareTo((Comparable)((Compactdisk)in).getName().toLowerCase());
}
// returns indivudual inventory value for a disk
public float getValue()
{
return(price * nstock);
}
}// end class
/CODE]
It works ok, does what I want it to do.
I then created this sub class just for cd artist that I thought would just add to it.
My understanding was that this new sub class is just an extention of the original and could be used with little or no extra effort. So I changed my Inventory application to include this object accordingly.
I am getting a cannot find symbol error on the new code. This usually means there is a problem with that object, I am guessing Inventory cannot find it. Do I have it set up wrong, or is my logic screwy?
Thanks for any help.
I intentionally left a field out of my compactdisk class (artist) so that I could create a sub class later, impliment it, and see how that whole thing worked.
Sounded simple, I still think it is, I just think I am doing something wrong.
Here is my original class:
[CODE][import java.lang.Comparable;
public class Compactdisk implements Comparable
{// begin class
//InventoryCD class has 5 fields
private String name; // Name of cd
private float price; // price of cd
private int itemno; // item number of cd
private int nstock; // how many units in stock
private int i; // cd counter for array
private float value; // value for single cd inventory
//Compact disk class constructor
public Compactdisk()
// 4 fields need to be set up
{
name = "";
price = 0;
itemno = 0;
nstock = 0;
i = 0;
value = 0;
}
// set values
public void setName(String diskName)
{
name = diskName;
}
public void setPrice(float cdPrice)
{
price = cdPrice;
}
public void setItemno(int cdItemno)
{
itemno = cdItemno;
}
public void setNstock(int cdStock)
{
nstock = cdStock;
}
public void setValue(float cdValue)
{
value = cdValue;
}
public void seti(int Count)
{
i = Count;
}
// return values
public String getName()
{
return (name);
}
public float getPrice()
{
return (price);
}
public int getItemno()
{
return (itemno);
}
public int getNstock()
{
return (nstock);
}
public int compareTo(Object in)
{
return ((Comparable)name.toLowerCase()).compareTo((Comparable)((Compactdisk)in).getName().toLowerCase());
}
// returns indivudual inventory value for a disk
public float getValue()
{
return(price * nstock);
}
}// end class
/CODE]
It works ok, does what I want it to do.
I then created this sub class just for cd artist that I thought would just add to it.
Java Syntax (Toggle Plain Text)
import java.util.*; public class Cdartist extends Compactdisk { private String artist; // artist performing on cd // Artist constructor public Cdartist() { artist = ""; } // set value public void setCdArtist(String cdArtist) { artist = cdArtist; } // return value public String getCdArtist() { return (artist); } } //End Class
My understanding was that this new sub class is just an extention of the original and could be used with little or no extra effort. So I changed my Inventory application to include this object accordingly.
import java.util.*;
public class Inventory
{// begin class Inventory
public static int maxlength = 0;
public static Compactdisk[] sort(Compactdisk[] cds)
{
Arrays.sort(cds, 0, maxlength);
return cds;
}
public static String toString(Compactdisk[] cds)
{
String toSend = "\n\n";
for(int i = 0; i < maxlength; i ++)
toSend = toSend + cds[i].getName() + "\n";
return toSend;
}
public static void main(String[] args)
{//begin method main
// create cd Array
Compactdisk[] cds = new Compactdisk[100];
cds[0] = new Compactdisk();
float totalValue = 0;
Scanner input = new Scanner(System.in); // create scanner
// begin display method
System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
String nameInput = input.nextLine(); //read cd name
maxlength ++;
for(int i = 1; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)
{// begin main While
cds[i] = new Compactdisk();
cds[i].setName(nameInput);
System.out.print("Enter CD Artist Name: "); // prompt for artist name
// input=new Scanner(System.in);
cds[i].setCdArtist(input.nextLine()); // artist name input from user
System.out.print("Enter Price of this CD: "); // prompt for price
cds[i].setPrice(input.nextFloat()); // price input from user.
while (cds[i].getPrice()<= 0)
{// begin while
System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
cds[i].setPrice(input.nextFloat()); // cd price loop from user.
} // End while
System.out.print("Enter CD Item Number: "); // prompt for cd item number
cds[i].setItemno(input.nextInt()); // cds item number input from user
System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
cds[i].setNstock(input.nextInt()); // cds in stock input from user
System.out.print("\n\nCD "+cds[i].getName()+", Item Number "+cds[i].getItemno()+","); // display name
System.out.printf(" is worth %c%.2f.",'$', + cds[i].getPrice());//
System.out.print("\nWe have "+ cds[i].getNstock()+" copies in stock,");
System.out.printf(" making our inventory for this cd worth %c%.2f.\n", '$', + cds[i].getValue()); //inventory value
if(cds[i].getValue() != -1) totalValue = totalValue + cds[i].getValue();
System.out.printf("Combined Inventory for all CDs is Worth %c%.2f.\n\n\n", '$', + totalValue);
System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
input=new Scanner(System.in); // internal loop prompt
nameInput = input.nextLine(); //name input from user
maxlength ++;
} // End main While
//System.out.println(toString(cds));
System.out.println(toString(sort(cds)));
System.out.print("Ending Program.");
}// end method main
} // end class PayrollThanks for any help.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
cds is an array of CompactDisk, so it didn't have method like setCdArtist, (or getCdArtist)
you can first create a Cdartist, then assign it to a place in cds
you can first create a Cdartist, then assign it to a place in cds
Last edited by tonakai; Jul 18th, 2007 at 10:47 am.
tonakai is correct.
If you are confused about how subclasses act in Java, the vehicle example usually clarifies things:
A vehicle in this definition is something that moves or transports people. An automobile is a type of vehicle with wheels. A car is a type of automobile. A truck is also a type of automobile. A car is not a truck, and a truck is not a car. An automobile is not necessarily a car or truck (it could be a van). A boat is a type of vehicle but not a type of automobile, the reverse is true of automobile.
In terms of hierarchy of classes:
Vehicle is a superclass which only knows that it moves people. Automobile is a subclass of Vehicle and a superclass of car, truck, and van, and only knows that it has wheels (not what type it is). Car, truck, and van are the most specific types of automobiles and know the most about what they can do. Boats are also specific, knowing they cannot use wheels but instead use rudders.
In that way, you cannot assume vehicle can do anything that it's subtypes (automobile or boat) can do, but it's subtypes(automobile and boat) can do anything vehicle can do (as they are vehicles).
If you are confused about how subclasses act in Java, the vehicle example usually clarifies things:
A vehicle in this definition is something that moves or transports people. An automobile is a type of vehicle with wheels. A car is a type of automobile. A truck is also a type of automobile. A car is not a truck, and a truck is not a car. An automobile is not necessarily a car or truck (it could be a van). A boat is a type of vehicle but not a type of automobile, the reverse is true of automobile.
In terms of hierarchy of classes:
Vehicle is a superclass which only knows that it moves people. Automobile is a subclass of Vehicle and a superclass of car, truck, and van, and only knows that it has wheels (not what type it is). Car, truck, and van are the most specific types of automobiles and know the most about what they can do. Boats are also specific, knowing they cannot use wheels but instead use rudders.
In that way, you cannot assume vehicle can do anything that it's subtypes (automobile or boat) can do, but it's subtypes(automobile and boat) can do anything vehicle can do (as they are vehicles).
I must be getting better at this, because I actually understood that. :o)
Two questions.
1. Concerning the setCdArtist method, is that not being created in the extended class? I thought the whole purpose of that class was to create that method for the set and get calls in Inventory.
2. I too thought I should assign it somewhere in cds. I thought my bolded code above would do that.
It is my understanding that I do not want to alter Compactdisk, or else it takes away from the purpose of extending a sub class. So if I do not add it in to the Inventory class as I thought I was doing, where would that insert happen?
Two questions.
1. Concerning the setCdArtist method, is that not being created in the extended class? I thought the whole purpose of that class was to create that method for the set and get calls in Inventory.
2. I too thought I should assign it somewhere in cds. I thought my bolded code above would do that.
It is my understanding that I do not want to alter Compactdisk, or else it takes away from the purpose of extending a sub class. So if I do not add it in to the Inventory class as I thought I was doing, where would that insert happen?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
TheGathering,
Thanks for that. I do think I have the concept of subclasses down, and to use your analogy, Compactdisk would be my vehicle, and Cdartist would be a car or truck.
When I run my application (Inventory) it pulls information from Compactdisk, and therefore Cdartist as an extented class, right? I should not have to alter Compactdisk to add objects in CdArtist, if I understand correctly.
So I am trying to figure out what I need to do in the Inventory class to get cds to recognize this new object.
Thanks for that. I do think I have the concept of subclasses down, and to use your analogy, Compactdisk would be my vehicle, and Cdartist would be a car or truck.
When I run my application (Inventory) it pulls information from Compactdisk, and therefore Cdartist as an extented class, right? I should not have to alter Compactdisk to add objects in CdArtist, if I understand correctly.
So I am trying to figure out what I need to do in the Inventory class to get cds to recognize this new object.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
•
•
Join Date: Sep 2006
Posts: 162
Reputation:
Solved Threads: 14
•
•
•
•
TheGathering,
Compactdisk would be my vehicle, and Cdartist would be a car or truck.
The car or truck is the object that you instantiate e.g.
Java Syntax (Toggle Plain Text)
Compactdisk cd = new Compactdisk();
So then what is my vehicle class?
If Compactdisk is car, and CdArtis is truck, then neither is really a subclass of the other? I was thinking that extending Compactdisk with CdArtist takes everthing as it is, and just adds one more detail.
Am I not doing what I think I am doing?
I want to make my CdArtist a sub class of Compactdisk, and just add that extra object into my array.
If Compactdisk is car, and CdArtis is truck, then neither is really a subclass of the other? I was thinking that extending Compactdisk with CdArtist takes everthing as it is, and just adds one more detail.
Am I not doing what I think I am doing?
I want to make my CdArtist a sub class of Compactdisk, and just add that extra object into my array.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
•
•
•
•
So then what is my vehicle class?
If Compactdisk is car, and CdArtis is truck, then neither is really a subclass of the other? I was thinking that extending Compactdisk with CdArtist takes everthing as it is, and just adds one more detail.
Am I not doing what I think I am doing?
I want to make my CdArtist a sub class of Compactdisk, and just add that extra object into my array.
The way I'm understanding your hierarchy with the code I wrote is: Comparable is vehicle, Compactdisk is your automobile, and CdArtist is your car.
It seems like a better name would be CDwithArtist for that class.
Last edited by TheGathering; Jul 18th, 2007 at 12:55 pm.
I see. So then I need to add everything that is in Compactdisk in to CdArtist along with my new Artist object, and then change my
Compactdisk cd = new Compactdisk(); to something like
CdArtist cd = new Compactdisk();
in my Inventory application and go from there.
I am not totally sure how to add all that into the new class, so I will get my face in a book here shortly. Thanks for the direction. Let me know if I am off base with this.
Thanks again.
Compactdisk cd = new Compactdisk(); to something like
CdArtist cd = new Compactdisk();
in my Inventory application and go from there.
I am not totally sure how to add all that into the new class, so I will get my face in a book here shortly. Thanks for the direction. Let me know if I am off base with this.
Thanks again.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
•
•
•
•
I see. So then I need to add everything that is in Compactdisk in to CdArtist along with my new Artist object, and then change my
Compactdisk cd = new Compactdisk(); to something like
CdArtist cd = new Compactdisk();
in my Inventory application and go from there.
I am not totally sure how to add all that into the new class, so I will get my face in a book here shortly. Thanks for the direction. Let me know if I am off base with this.
Thanks again.
Java Syntax (Toggle Plain Text)
public class CdArtist extends Compactdisk { Artist person; public CdArtist(Artist in) { person=in; } //however you're adding artists public Artist getArtist() {return person;} }
From there you can use:
Java Syntax (Toggle Plain Text)
CdArtist CDwArtists=new CdArtist();
You can also use:
Java Syntax (Toggle Plain Text)
Compactdisk CDwArtists = new CdArtist();
If your Compactdisk class needs paramters, then in the constructor for CdArtist you can use something like:
Java Syntax (Toggle Plain Text)
public CdArtist(Artist in, String nameIn) { super.name=nameIn; person=in; }
Last edited by TheGathering; Jul 18th, 2007 at 1:15 pm.
![]() |
Similar Threads
- java and using observable class (Java)
- Accessing functions from base class (C)
- Circle class (C++)
- "missing storage-class or type specifiers" error (C++)
- first math class in uni (Geeks' Lounge)
Other Threads in the Java Forum
- Previous Thread: array limits
- Next Thread: Plz help me in my project.... I am doing a project of filling the examination form.
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel jtextarea julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows





