944,135 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 5531
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 18th, 2007
0

Class and Sub Class

Expand Post »
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.
Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 18th, 2007
0

Re: Class and Sub Class

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.
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005
Jul 18th, 2007
0

Re: Class and Sub Class

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).
Reputation Points: 22
Solved Threads: 10
Junior Poster
TheGathering is offline Offline
102 posts
since Jul 2007
Jul 18th, 2007
0

Re: Class and Sub Class

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?
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 18th, 2007
0

Re: Class and Sub Class

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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 18th, 2007
0

Re: Class and Sub Class

Click to Expand / Collapse  Quote originally posted by no1zson ...
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.

Java Syntax (Toggle Plain Text)
  1. Compactdisk cd = new Compactdisk();
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 18th, 2007
0

Re: Class and Sub Class

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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 18th, 2007
0

Re: Class and Sub Class

Click to Expand / Collapse  Quote originally posted by no1zson ...
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.
Reputation Points: 22
Solved Threads: 10
Junior Poster
TheGathering is offline Offline
102 posts
since Jul 2007
Jul 18th, 2007
0

Re: Class and Sub Class

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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 18th, 2007
0

Re: Class and Sub Class

Click to Expand / Collapse  Quote originally posted by no1zson ...
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)
  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:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  1. 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)
  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.
Reputation Points: 22
Solved Threads: 10
Junior Poster
TheGathering is offline Offline
102 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: array limits
Next Thread in Java Forum Timeline: Plz help me in my project.... I am doing a project of filling the examination form.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC