Inheritance vs New Method

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

Inheritance vs New Method

 
0
  #1
Jul 20th, 2007
I want to learn more about how inheritance works, more like I NEED to learn more about it if I am to get any better at this.
In my code I have the value of a cd defined as how many cds are in stock times the price of the cd. I did this in my first class Compactdisk.

Now I have created a sub class that adds a new parameter in to my array, and that all makes sence to me now.

What if I want to alter the output of a method I have already done though? Value for instance. With my new subclass I want to make Value mean (price X stock X restock) where restock is going to be a 5% restocking fee.

I should not have to alter Compactdisk, as CdwArtist is a sub and inherits everything, so I added what you see below to CdwArtist. I think it is right, but how would I then get Inventory to grab this new Value instead of the old Value?
  1. import java.util.*;
  2.  
  3. public class Inventory
  4. {// begin class Inventory
  5.  
  6. public static int maxlength = 0;
  7. public static CdwArtist[] sort(CdwArtist[] cds)
  8. {
  9. Arrays.sort(cds, 0, maxlength);
  10. return cds;
  11. }
  12.  
  13. public static String toString(CdwArtist[] cds)
  14. {
  15. String toSend = "\n\n";
  16.  
  17. for(int i = 0; i < maxlength; i ++)
  18. toSend = toSend + cds[i].getName() + "\n";
  19. return toSend;
  20. }
  21.  
  22. public static void main(String[] args)
  23. {//begin method main
  24.  
  25. // create cd Array
  26. CdwArtist[] cds = new CdwArtist[100];
  27.  
  28.  
  29. float totalValue = 0;
  30.  
  31. Scanner input = new Scanner(System.in); // create scanner
  32.  
  33. // begin display method
  34. System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
  35. String nameInput = input.nextLine(); //read cd name
  36.  
  37.  
  38. for(int i = 0; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)
  39. {// begin main While
  40. cds[i] = new CdwArtist();
  41. cds[i].setName(nameInput);
  42.  
  43. System.out.print("Enter CD Artist Name: "); // prompt for artist name
  44. CdwArtist artist = new CdwArtist(input.nextLine());
  45.  
  46. System.out.print("Enter Price of this CD: "); // prompt for price
  47. cds[i].setPrice(input.nextFloat()); // price input from user.
  48. while (cds[i].getPrice()<= 0)
  49. {// begin while
  50. System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
  51. cds[i].setPrice(input.nextFloat()); // cd price loop from user.
  52. } // End while
  53.  
  54. System.out.print("Enter CD Item Number: "); // prompt for cd item number
  55. cds[i].setItemno(input.nextInt()); // cds item number input from user
  56.  
  57. System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
  58. cds[i].setNstock(input.nextInt()); // cds in stock input from user
  59.  
  60. System.out.print("\n\nCD "+cds[i].getName()+", Item Number "+cds[i].getItemno()+","); // display name
  61. System.out.printf(" is worth %c%.2f.",'$', + cds[i].getPrice());//
  62. System.out.print("\nWe have "+ cds[i].getNstock()+" copies in stock,");
  63. System.out.printf(" making our inventory for this cd worth %c%.2f.\n", '$', + cds[i].getValue()); //inventory value
  64.  
  65. if(cds[i].getValue() != -1) totalValue = totalValue + cds[i].getValue();
  66. System.out.printf("Combined Inventory for all CDs is Worth %c%.2f.\n\n\n", '$', + totalValue);
  67.  
  68. System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
  69. input=new Scanner(System.in); // internal loop prompt
  70. nameInput = input.nextLine(); //name input from user
  71.  
  72. maxlength ++;
  73. } // End main While
  74.  
  75. //System.out.println(toString(cds));
  76. System.out.println(toString(sort(cds)));
  77. System.out.print("Ending Program.");
  78.  
  79. }// end method main
  80. } // end class Payroll

My first class:
  1. import java.lang.Comparable;
  2.  
  3. public class Compactdisk implements Comparable
  4. {// begin class
  5.  
  6. //InventoryCD class has 5 fields
  7. private String name; // Name of cd
  8. private float price; // price of cd
  9. private int itemno; // item number of cd
  10. private int nstock; // how many units in stock
  11. private int i; // cd counter for array
  12. private float value; // value for single cd inventory
  13.  
  14.  
  15. //Compact disk class constructor
  16. public Compactdisk()
  17.  
  18. // 4 fields need to be set up
  19. {
  20. name = "";
  21. price = 0;
  22. itemno = 0;
  23. nstock = 0;
  24. i = 0;
  25. value = 0;
  26. }
  27.  
  28. // set values
  29. public void setName(String diskName)
  30. {
  31. name = diskName;
  32. }
  33. public void setPrice(float cdPrice)
  34. {
  35. price = cdPrice;
  36. }
  37. public void setItemno(int cdItemno)
  38. {
  39. itemno = cdItemno;
  40. }
  41. public void setNstock(int cdStock)
  42. {
  43. nstock = cdStock;
  44. }
  45. public void setValue(float cdValue)
  46. {
  47. value = cdValue;
  48. }
  49. public void seti(int Count)
  50. {
  51. i = Count;
  52. }
  53.  
  54.  
  55. // return values
  56. public String getName()
  57. {
  58. return (name);
  59. }
  60. public float getPrice()
  61. {
  62. return (price);
  63. }
  64. public int getItemno()
  65. {
  66. return (itemno);
  67. }
  68. public int getNstock()
  69. {
  70. return (nstock);
  71. }
  72. public int compareTo(Object in)
  73. {
  74. return ((Comparable)name.toLowerCase()).compareTo((Comparable)((Compactdisk)in).getName().toLowerCase());
  75. }
  76.  
  77. // returns indivudual inventory value for a disk
  78. public float getValue()
  79. {
  80. return(price * nstock);
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. }// end class
and my new sub class, with my added code in bold.
import java.util.*;

public class CdwArtist extends Compactdisk
{
 
 public String artist; // artist performing on cd
 public int restock; // restocking percentage

  
 // Artist constructor
  public CdwArtist()
 {
 artist = "";
 restock = .05
 }
 
 public CdwArtist(String in)
 {
 artist=in;
 }
 
 // get value
 public void setArtist(String in)
 {
 artist=in;
 }
 public void setRestock(float cdRestock)
 {
 restock = cdRestock;
 }
  
 // return value
 public String getArtist()
 {
 return (artist);
 }
	
 // returns indivudual inventory value for a disk
	   public float getValue()
	   {
	   return((price * nstock)* restock);
	   }	
	
} //End Class

I was hoping that I could just add a new method with the same name, and since everything is just inherited that my Inventory class would take the last inherited value for Value. It did not work. I still only get the original value that I set up in Compactdisk.
Am I making any sense?
Last edited by no1zson; Jul 20th, 2007 at 12:47 pm.
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: Inheritance vs New Method

 
0
  #2
Jul 20th, 2007
I'll give you a simple example..

Imagine a class:

  1. public class Animal{
  2. int legs;
  3. bool tail;
  4. public Animal(int legs, bool tail){
  5. this.legs = legs;
  6. this.tail = tail;
  7. }
  8. public void noise(){
  9. System.out.println("GROWL!");
  10. }
  11. }

Now we can inherit an 'Animal' and 'override' its noise method.

  1. public class Dog extends Animal{
  2. public void noise(){
  3. System.out.println("WOOF"!);
  4. }
  5. }

Dog inherits all the functionality of Animal and customises the noise method. This is approaching another subject (Polymorphism).
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Inheritance vs New Method

 
0
  #3
Jul 20th, 2007
I do not see any structural problems there. You have overridden the method correctly. There are other issues with the code though and it will not compile as you have it posted. You have defined "restock" as an int, assigned it a double value in the constructor, and also left off the semicolon on the assignment.

You will want to make restock a float or double value for your math to work correctly.
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: Inheritance vs New Method

 
0
  #4
Jul 20th, 2007
I am so stupid. The file I was running was not the file I had compiled. I was coding and compling in one file, running the other and wondering why nothing changed! :o)

Once I did correct that issue it does work, if I take out ".05" and just put in "5" or "05". So my method was right! First time! woo hoo, I am so happy.

Of course I need it to be ".05". When I put the decimal back in it erros and says possible loss of percision, found float, double required. If I change it to doube is says it found double, float required. Here is what it looks like with the syntax corrected.

  1. public String artist; // artist performing on cd
  2. public float restock; // restocking percentage
  3.  
  4.  
  5. // Artist constructor
  6. public CdwArtist()
  7. {
  8. artist = "";
  9. restock = .05;
  10. }
  11.  
  12. public CdwArtist(String in)
  13. {
  14. artist=in;
  15. }
  16.  
  17. // get value
  18. public void setArtist(String in)
  19. {
  20. artist=in;
  21. }
  22. public void setRestock(float cdRestock)
  23. {
  24. restock = cdRestock;
  25. }
  26.  
  27. // return value
  28. public String getArtist()
  29. {
  30. return (artist);
  31. }
  32.  
  33. // returns indivudual inventory value for a disk
  34. public float getValue()
  35. {
  36. return((price * nstock)* restock);
  37. }
  38.  
  39. } //End Class

This is finally fun. I feel like am learning stuff and working through NEW problems for a change!
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: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Inheritance vs New Method

 
0
  #5
Jul 20th, 2007
The compiler is warning that you are mixing float and double calculations, which affect precision. It you don't want to worry about figuring out how to apply casting in your math expression, just change the type of restock to float and assign it "0.05f". The "f" denotes that it is a float, instead of double which is the default for decimals that you type in as literals.
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: Inheritance vs New Method

 
0
  #6
Jul 20th, 2007
That is a neat trick, thanks for that tip.
Of course it showed me that my math was even more wrong than I though.
I had to change the formula to this:
return(price * nstock)+((price * nstock)* restock);

but with that I am going to say it is close enough, TheGather already pointed out the issue with the counter, so I have nothing more I want to do to this.

I am taking the weekend off to catch up on some reading. I was hoping to start some GUI stuff next week, and that was my whole reason for starting this endeavor. I hope it is as fun as I originally thought it was going to be.

Thanks so much for all your help this week guys.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC