Class and Sub Class

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Class and Sub Class

 
0
  #1
Jul 18th, 2007
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.
  1. import java.util.*;
  2.  
  3. public class Cdartist extends Compactdisk
  4. {
  5. private String artist; // artist performing on cd
  6.  
  7. // Artist constructor
  8. public Cdartist()
  9. {
  10. artist = "";
  11. }
  12.  
  13. // set value
  14. public void setCdArtist(String cdArtist)
  15. {
  16. artist = cdArtist;
  17. }
  18.  
  19. // return value
  20. public String getCdArtist()
  21. {
  22. return (artist);
  23. }
  24.  
  25.  
  26. } //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 Payroll
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 never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: Class and Sub Class

 
0
  #2
Jul 18th, 2007
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
Last edited by tonakai; Jul 18th, 2007 at 10:47 am.
Good news, everyone!
aykutsoysal.com
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: TheGathering is an unknown quantity at this point 
Solved Threads: 10
TheGathering's Avatar
TheGathering TheGathering is offline Offline
Junior Poster

Re: Class and Sub Class

 
0
  #3
Jul 18th, 2007
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).
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Class and Sub Class

 
0
  #4
Jul 18th, 2007
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?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Class and Sub Class

 
0
  #5
Jul 18th, 2007
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Class and Sub Class

 
0
  #6
Jul 18th, 2007
Originally Posted by no1zson View Post
TheGathering,
Compactdisk would be my vehicle, and Cdartist would be a car or truck.
The Compactdisk class is really the specification for a car (or Cd no artist) and Cdartist a specification of a truck (cd with an artist).

The car or truck is the object that you instantiate e.g.

  1. Compactdisk cd = new Compactdisk();
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Class and Sub Class

 
0
  #7
Jul 18th, 2007
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: TheGathering is an unknown quantity at this point 
Solved Threads: 10
TheGathering's Avatar
TheGathering TheGathering is offline Offline
Junior Poster

Re: Class and Sub Class

 
0
  #8
Jul 18th, 2007
Originally Posted by no1zson View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Class and Sub Class

 
0
  #9
Jul 18th, 2007
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: TheGathering is an unknown quantity at this point 
Solved Threads: 10
TheGathering's Avatar
TheGathering TheGathering is offline Offline
Junior Poster

Re: Class and Sub Class

 
0
  #10
Jul 18th, 2007
Originally Posted by no1zson View Post
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.
  1. public class CdArtist extends Compactdisk
  2. {
  3. Artist person;
  4. public CdArtist(Artist in)
  5. {
  6. person=in;
  7. }
  8. //however you're adding artists
  9.  
  10. public Artist getArtist()
  11. {return person;}
  12. }

From there you can use:
  1. CdArtist CDwArtists=new CdArtist();
and be able to call any of the methods in Compactdisk as well as .getArtist() from CDwArtists.

You can also use:
  1. Compactdisk CDwArtists = new CdArtist();


If your Compactdisk class needs paramters, then in the constructor for CdArtist you can use something like:
  1. public CdArtist(Artist in, String nameIn)
  2. {
  3. super.name=nameIn;
  4. person=in;
  5. }
Last edited by TheGathering; Jul 18th, 2007 at 1:15 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC