943,928 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 476
  • Java RSS
May 30th, 2009
0

Trouble Extending a Class

Expand Post »
I am new to Java, I am six weeks through a nine week class. I am having problems extending a class in my program. My assignment is as follows:

Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
 Modify the output to display this additional feature you have chosen and the restocking fee.

My code compiles but does not print the contents of the ExtendDVD class. I have the three classes saved as seperate files. Can someone please help me find determine what is wrong. Thank you.
Java Syntax (Toggle Plain Text)
  1. import java.util.Arrays;
  2. public class Inventory2 // declares public class inventory
  3.  
  4. {
  5.  
  6. public static void main(String args[]) // starts the program
  7.  
  8. {
  9.  
  10. DVD[] dvd = new DVD[3];
  11.  
  12.  
  13. dvd[0] = new DVD(1, "Knocked Up", 2, 17.99, "min100");// declares a new item number, dvd title, quantity, and price
  14.  
  15. dvd[1] = new DVD(2, "Napolean Dynamite", 4, 14.99, "100 Min");
  16.  
  17. dvd[2] = new DVD(3, "Happy Gilmore", 6, 9.99, "100 Min");
  18.  
  19.  
  20.  
  21.  
  22.  
  23. Inventory2 x = new Inventory2();// declare second instance of inventory to sort
  24. x.SortDVD(dvd);
  25.  
  26. for(int i = 0; i < 3; i++)// loop to call array list and set dvd = i
  27.  
  28.  
  29. {
  30. System.out.println(dvd[i]);// print array i
  31. System.out.println();
  32.  
  33. System.out.println("The item number is " + dvd[i].getItemNumber()); // print item number
  34. System.out.println("Product Title is " + dvd[i].getDvdTitle()); //print dvd title
  35. System.out.println("The number of units in stock is " + dvd[i].getOnHand()); //print on hand quantity
  36. System.out.println("The price of each DVD is $" + dvd[i].getDvdCost()); //print cost
  37. System.out.println("The value of this Item is $" + dvd[i].value()); // print value of all on hand
  38.  
  39. }
  40.  
  41.  
  42. double total = 0;// initiate total inventory as double
  43.  
  44. for( int counter = 0; counter < dvd.length; counter++ )// calculate total inventory
  45. total += dvd[ counter ].value();
  46. System.out.println();
  47. System.out.println( "The Value of All On Hand Inventory is $" + total );//print the total value of the inventory
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. }// end main
  56.  
  57. public void SortDVD(DVD[] theDVD)// method to sort array dvd
  58.  
  59. {
  60. for (int index = 0; index < theDVD.length - 1; index++)
  61.  
  62. {
  63. String s1 = theDVD[index].getDvdTitle(); // set title to sort
  64. String s2 = theDVD[index + 1].getDvdTitle();// set title to compare in sort
  65. if (comparewords(s1, s2)) // compares titles to sort
  66.  
  67. {
  68. DVD temp = theDVD[index];// creates temporary instance of dvd
  69. theDVD[index] = theDVD[index + 1];
  70. theDVD[index + 1] = temp;
  71. index = -1;
  72. }
  73. }
  74. }
  75.  
  76. private boolean comparewords(String s1, String s2)// creates boolean and compares the two strings
  77. {
  78. boolean islarger = false; //continues to sort until true
  79.  
  80. for (int index = 0; index < s1.length(); index++) //loop to determine title length
  81.  
  82. {
  83. if (index < s2.length())
  84.  
  85. {
  86. if (s1.toLowerCase().charAt(index) > s2.toLowerCase().charAt(index))
  87. {
  88. islarger = true;//sort by longer name
  89. break;
  90. }
  91. if (s1.toLowerCase().charAt(index) < s2.toLowerCase().charAt(index))
  92. {
  93.  
  94. break;// stop
  95. }
  96. }
  97. else
  98. {
  99. return true;
  100. }
  101. }
  102.  
  103. return islarger;// return larger moves title higher in the list
  104. }
  105.  
  106.  
  107. } // end class Inventory2
  108. public class DVD // declares class inventory
  109. {
  110.  
  111.  
  112. int itemNumber;// declares item number as in
  113. String dvdTitle;// declares dvd title as string
  114. int onHand; // declares on hand as int
  115. double dvdCost;// declares cost as double
  116.  
  117. public DVD(int stockNumber, String title, int inStock, double price, String studio) // constructor
  118. {
  119. itemNumber = stockNumber;// intitiates stock number, title, instock, and
  120. // price
  121. dvdTitle = title;
  122. onHand = inStock;
  123. dvdCost = price;
  124.  
  125. }
  126.  
  127. // set DVD StockNumber //sets stock number
  128. public void setItemNumber(int stockNumber)
  129.  
  130. {
  131. itemNumber = stockNumber;
  132.  
  133. }
  134.  
  135. public int getItemNumber() // class item number
  136.  
  137. {
  138. return itemNumber; // returns item number
  139. } // end method get Dvd Item
  140.  
  141. public void setDvdTitle(String title) // set DVD Title
  142.  
  143. {
  144. dvdTitle = title;
  145. }
  146.  
  147. public String getDvdTitle() // gets and returns DVD Title
  148.  
  149. {
  150.  
  151. return dvdTitle;
  152. }
  153.  
  154. public void setOnHand(int inStock) // Set on hand
  155.  
  156. {
  157. onHand = inStock;
  158. }
  159.  
  160. public int getOnHand() // gets and returns on hand
  161.  
  162. {
  163. return onHand;
  164. } //
  165.  
  166. public void setDvdCost(double price) // sets dvd cost
  167.  
  168. {
  169. dvdCost = price;
  170. }
  171.  
  172. public double getDvdCost() // gets and returns DVD cost
  173.  
  174. {
  175. return dvdCost;
  176. }
  177.  
  178. public double value() // calculates the value of stock
  179.  
  180. {
  181. return dvdCost * onHand;
  182. }
  183.  
  184.  
  185.  
  186.  
  187.  
  188. } // end class DVD
  189. public class ExtendDVD extends DVD
  190.  
  191. {
  192.  
  193. private String time = "";
  194.  
  195. public ExtendDVD( int item, String name, int units, double price, String studio )
  196.  
  197. {
  198. super( item, name, units, price, studio);
  199. this.time = studio;
  200. }
  201.  
  202. public String getTime()
  203. {
  204. return time;
  205. }
  206.  
  207. public void setTime( String time )
  208. {
  209. this.time = time;
  210. }
  211.  
  212. public double getInventoryValue()
  213. {
  214. return 1.05 * getOnHand() * getDvdCost();
  215. }
  216.  
  217. public double fee()
  218. {
  219. return 0.05 * getOnHand() * getDvdCost();
  220. }
  221.  
  222. public String tostring()
  223. {
  224. return String.format("\nInventory Information\n%s %s\n%s\t %s\n%s\t%d\n%s\t $%, .2f \n%s\t$%, .2f",
  225. "Item Number ", getItemNumber(),
  226. "Title ", getDvdTitle(),
  227. "Quantity On Hand ", getOnHand(),
  228. "Cost ", getDvdCost(),
  229. "Running Time ", getTime(),
  230. "Fee ", fee(),
  231. "Total Value ", getInventoryValue() );
  232. }
  233.  
  234.  
  235.  
  236. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gstang95gt is offline Offline
7 posts
since May 2009
May 30th, 2009
1

Re: Trouble Extending a Class

Quote ...
My code compiles but does not print the contents of the ExtendDVD class. I have the three classes saved as seperate files.
Well just a quick Search of your code revealed that you have never created an Object of ExtendDVD so I guess its logical that nothing from it would be printed.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
May 30th, 2009
0

Re: Trouble Extending a Class

I don't see any calls to an ExtendDVD constructor, so perhaps there are no ExtendDVD obejcts to display since none are ever created? I see these objects of type DVD created here:

Java Syntax (Toggle Plain Text)
  1. DVD[] dvd = new DVD[3];
  2.  
  3.  
  4. dvd[0] = new DVD(1, "Knocked Up", 2, 17.99, "min100");// declares a new item number, dvd title, quantity, and price
  5.  
  6. dvd[1] = new DVD(2, "Napolean Dynamite", 4, 14.99, "100 Min");
  7.  
  8. dvd[2] = new DVD(3, "Happy Gilmore", 6, 9.99, "100 Min");

Quick test. Delete the ExtendDVD class and see if the program compiles. If it compiles, that means ExtendDVD isn't necessary.

[edit]
To clarify, if it compiles without the ExtendDVD class, that means that no ExtendDVD objects are created.
[/edit]
Last edited by VernonDozier; May 30th, 2009 at 3:42 am.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
May 30th, 2009
0

Re: Trouble Extending a Class

Hello Again. Your main never creates any instances of the extended class. Even though the ExtendedDVD class extends DVD they are not the same object. You would have to make a new instance of the ExtendedDVD class:

java Syntax (Toggle Plain Text)
  1. ExtendedDVD (variablename) = new ExtendedDVD();
When a class Inherits another class it has the following nifty features:

1.) can use functions from the "super" or "parent" class
2.) can "override" functions from the "super" or "parent" class
3.) is considered, as far as java goes, to be the same as its "parent" class.

The third one is pretty useful. Let's say that you have a "person" class. This person class has a method called "walk". You could have a "boy" class and a "girl" class both extend the "person" class, and they could be treated as the same data type by using "person" as the identifier. You could make a Person array (like you made the dvd array) that could hold both boy and girl objects!

This only goes one way though. Java sees this about your inherited classes:

ExtendedDVD = DVD
DVD != ExtendedDVD

This is only from a comparison standpoint though. You still have to make an ExtendedDVD object to use the ExtendedDVD functions that differ from the regular DVD functions. On a side note, a method that is named the same as a method from the parent class but performs a different action is called polymorphism. The tostring() method you created is an example of polymorphism.
Last edited by StuartMillner; May 30th, 2009 at 3:49 am.
Reputation Points: 11
Solved Threads: 6
Light Poster
StuartMillner is offline Offline
32 posts
since May 2009
May 30th, 2009
0

Re: Trouble Extending a Class

I got the Program running as intended. It seems that I need to pay closer attention to details. Thank you again for your help.
Last edited by gstang95gt; May 30th, 2009 at 6:39 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gstang95gt is offline Offline
7 posts
since May 2009

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: java test download upload limit of internet locally
Next Thread in Java Forum Timeline: how to track of sends bytes





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


Follow us on Twitter


© 2011 DaniWeb® LLC