944,122 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2785
  • Java RSS
Oct 6th, 2004
0

help with polymorphism and inheritance...getting null values

Expand Post »
im trying to call a showInfo() method from a superclass MediaTypes, the subclass is CD and the driver class is Assn2. the problem im having is that for all the values i inherit from the MediaTypes class im getting only null values and for the gregorian date im getting a real long string...any input in to this would be great....

Java Syntax (Toggle Plain Text)
  1. import java.util.Date;
  2. import javax.swing.*;
  3.  
  4.  
  5. public class Assn2{
  6.  
  7. private MediaTypes[] myMedia;
  8. private Date release;
  9. private boolean go = true;
  10.  
  11. public static void main(String[] args)
  12. {
  13. new Assn2();
  14. }
  15.  
  16. //-------------------------------------------------------------------------------
  17. // Procedure: Assn2
  18. //
  19. // Summary: main driver method for program
  20. //-------------------------------------------------------------------------------
  21. public Assn2()
  22. {
  23.  
  24. //initialize data members
  25. myMedia = new MediaTypes[5]; //provide a dimension for the array
  26.  
  27.  
  28. while(go == true)
  29. {
  30. //main menu for navigating program
  31. Object[] possibleValues = { "Make New Entry",
  32. "View Entries",
  33. "Clear Entires",
  34. "Oldest Entries First",
  35. "Newest Entries First",
  36. "Exit"};
  37.  
  38. Object selectedValue = JOptionPane.showInputDialog(null,
  39. "Please select one of the following:",
  40. "Input",
  41. JOptionPane.INFORMATION_MESSAGE,
  42. null,
  43. possibleValues,
  44. possibleValues[0]);
  45.  
  46. if(selectedValue.equals(possibleValues[0]))
  47. {
  48.  
  49. //select Media Type to enter new
  50. Object[] possibleMedia = {"Music", "Movies", "Print"};
  51.  
  52. Object selectedMedia = JOptionPane.showInputDialog(null, "Please select your media type:",
  53. "Input",
  54. JOptionPane.INFORMATION_MESSAGE,
  55. null,
  56. possibleMedia,
  57. possibleMedia[0]);
  58.  
  59.  
  60.  
  61. if(selectedMedia.equals(possibleMedia[0]))
  62. {
  63. Object[] musicChoices = {"CD", "Cassette"};
  64.  
  65. Object selectedMusic = JOptionPane.showInputDialog(null, "Please select your music type:",
  66. "Input",
  67. JOptionPane.INFORMATION_MESSAGE,
  68. null,
  69. musicChoices,
  70.  
  71. musicChoices[0]);
  72. //make new cd
  73. if(selectedMusic.equals(musicChoices[0]))
  74. {
  75. makeNewCD();
  76. }
  77. //make new cassette
  78. else {makeNewCassette();}
  79.  
  80. }
  81.  
  82. if(selectedMedia.equals(possibleMedia[1]))
  83. {
  84. Object[] movieChoices = {"DVD", "VHS"};
  85.  
  86. Object selectedMovie = JOptionPane.showInputDialog(null, "Please select your Movie type:",
  87. "Input",
  88. JOptionPane.INFORMATION_MESSAGE,
  89. null,
  90. movieChoices,
  91. movieChoices[0]);
  92. //make new DVD
  93. if(selectedMovie.equals(movieChoices[0]))
  94. {
  95. makeNewDVD();
  96. }
  97. //make new VHS
  98. else {makeNewVHS();}
  99. }
  100.  
  101. if(selectedMedia.equals(possibleMedia[2]))
  102. {
  103. Object[] printChoices = {"Magazine", "Book"};
  104.  
  105. Object selectedPrint = JOptionPane.showInputDialog(null, "Please select your Print type:",
  106. "Input",
  107. JOptionPane.INFORMATION_MESSAGE,
  108. null,
  109. printChoices,
  110. printChoices[0]);
  111. //make new Magazine
  112. if(selectedPrint.equals(printChoices[0]))
  113. {
  114. makeNewMagazine();
  115. }
  116. //make new Book
  117. else {makeNewBook();}
  118. }
  119.  
  120. }
  121.  
  122.  
  123. //view all entries in the MediaTypes array
  124. if(selectedValue.equals(possibleValues[1]))
  125. {
  126. showInfo();
  127. }
  128.  
  129. //clear all entries in the MediaTypes array
  130. if(selectedValue.equals(possibleValues[2]))
  131. {
  132. clearAll();
  133. }
  134.  
  135. //sort by oldest first and
  136. //show the entries in that order
  137. if(selectedValue.equals(possibleValues[3]))
  138. {
  139. JOptionPane.showMessageDialog(null,"Entries have been sorted by Oldest to Newest");
  140. sortByOldest();
  141. }
  142.  
  143. //sort by newest first and
  144. //show entriest in that order
  145. if(selectedValue.equals(possibleValues[4]))
  146. {
  147. JOptionPane.showMessageDialog(null,"Entries have been sorted by Newest to Oldest");
  148. sortByNewest();
  149. }
  150.  
  151. //user selected exit the program....
  152. //make sure they really want to exit
  153. if(selectedValue.equals(possibleValues[5]))
  154. {
  155. exitProgram();
  156. }
  157. }
  158.  
  159. }
  160.  
  161. //-------------------------------------------------------------------------------
  162. // Procedure: makeNewCD
  163. //
  164. // Summary: creates and adds a new CD entry into media types array
  165. //-------------------------------------------------------------------------------
  166. public void makeNewCD()
  167. {
  168. if(MediaTypes.getCounter() <=4)
  169. {
  170. //prompt user for entries
  171. String title = JOptionPane.showInputDialog("Enter name of CD");
  172. String genre = JOptionPane.showInputDialog("Enter type of music");
  173. String label = JOptionPane.showInputDialog("Enter label the artist is on");
  174. String artist = JOptionPane.showInputDialog("Enter artist name");
  175. try
  176. {
  177.  
  178. String day = JOptionPane.showInputDialog("Enter day of release");
  179. int d = Integer.parseInt(day);
  180.  
  181. String month = JOptionPane.showInputDialog("Enter month of release");
  182. int m = Integer.parseInt(month);
  183.  
  184. String year = JOptionPane.showInputDialog("Enter year of release");
  185. int y = Integer.parseInt(year);
  186.  
  187. myMedia[MediaTypes.getCounter()] = new CD(artist, title, d, m, y, genre, label );
  188.  
  189. // if(myMedia[counter] != null){counter++;}
  190.  
  191. }
  192. catch(Exception Ex)
  193. {
  194.  
  195. JOptionPane.showMessageDialog(null, "Invalid input entered");
  196. }
  197.  
  198. }
  199. else JOptionPane.showMessageDialog(null, "The MediaTypes array is full, you cannot add more MediaTypes's");
  200. }
  201.  
  202. //-------------------------------------------------------------------------------
  203. // Procedure: makeNewCassette
  204. //
  205. // Summary: creates and adds a new Cassette entry into media types array
  206. //-------------------------------------------------------------------------------
  207. public void makeNewCassette()
  208. {
  209. if(MediaTypes.getCounter() <=4)
  210. {
  211. //prompt user for entries
  212. String title = JOptionPane.showInputDialog("Enter name of Cassette");
  213. String genre = JOptionPane.showInputDialog("Enter type of music");
  214. String label = JOptionPane.showInputDialog("Enter label the artist is on");
  215. String mixtape = JOptionPane.showInputDialog("Is it a mixtape?");
  216. try
  217. {
  218.  
  219. String day = JOptionPane.showInputDialog("Enter day of release");
  220. int d = Integer.parseInt(day);
  221.  
  222. String month = JOptionPane.showInputDialog("Enter month of release");
  223. int m = Integer.parseInt(month);
  224.  
  225. String year = JOptionPane.showInputDialog("Enter year of release");
  226. int y = Integer.parseInt(year);
  227.  
  228. myMedia[MediaTypes.getCounter()] = new Cassette(title, d, m, y, label, genre, mixtape );
  229.  
  230. // if(myMedia[counter] != null){counter++;}
  231.  
  232. }
  233. catch(Exception Ex)
  234. {
  235.  
  236. JOptionPane.showMessageDialog(null, "Invalid input entered");
  237. }
  238.  
  239. }
  240. else JOptionPane.showMessageDialog(null, "The MediaTypes array is full, you cannot add more MediaTypes's");
  241. }
  242.  
  243. //-------------------------------------------------------------------------------
  244. // Procedure: makeNewDVD
  245. //
  246. // Summary: creates and adds a new DVD entry into media types array
  247. //-------------------------------------------------------------------------------
  248. public void makeNewDVD()
  249. {
  250. if(MediaTypes.getCounter() <=4)
  251. {
  252. //prompt user for entries
  253. String title = JOptionPane.showInputDialog("Enter title of DVD");
  254. String genre = JOptionPane.showInputDialog("Enter genre of movie");
  255. String dir = JOptionPane.showInputDialog("Enter the director of the movie");
  256. String pirated = JOptionPane.showInputDialog("Is this movie pirated?");
  257. try
  258. {
  259.  
  260. String day = JOptionPane.showInputDialog("Enter day of release");
  261. int d = Integer.parseInt(day);
  262.  
  263. String month = JOptionPane.showInputDialog("Enter month of release");
  264. int m = Integer.parseInt(month);
  265.  
  266. String year = JOptionPane.showInputDialog("Enter year of release");
  267. int y = Integer.parseInt(year);
  268.  
  269. myMedia[MediaTypes.getCounter()] = new DVD(title, dir, d, m, y, genre, pirated );
  270.  
  271. // if(myMedia[counter] != null){counter++;}
  272.  
  273. }
  274. catch(Exception Ex)
  275. {
  276.  
  277. JOptionPane.showMessageDialog(null, "Invalid input entered");
  278. }
  279.  
  280. }
  281. else JOptionPane.showMessageDialog(null, "The MediaTypes array is full, you cannot add more MediaTypes's");
  282. }
  283.  
  284. //-------------------------------------------------------------------------------
  285. // Procedure: makeNewVHS
  286. //
  287. // Summary: creates and adds a new VHS entry into media types array
  288. //-------------------------------------------------------------------------------
  289. public void makeNewVHS()
  290. {
  291. if(MediaTypes.getCounter() <=4)
  292. {
  293. //prompt user for entries
  294. String title = JOptionPane.showInputDialog("Enter title of VHS tape");
  295. String genre = JOptionPane.showInputDialog("Enter genre of the movie");
  296. String dir = JOptionPane.showInputDialog("Enter the director of the movie");
  297.  
  298. try
  299. {
  300.  
  301. String day = JOptionPane.showInputDialog("Enter day of release");
  302. int d = Integer.parseInt(day);
  303.  
  304. String month = JOptionPane.showInputDialog("Enter month of release");
  305. int m = Integer.parseInt(month);
  306.  
  307. String year = JOptionPane.showInputDialog("Enter year of release");
  308. int y = Integer.parseInt(year);
  309.  
  310. myMedia[MediaTypes.getCounter()] = new VHS(title, dir, d, m, y, genre);
  311.  
  312. // if(myMedia[counter] != null){counter++;}
  313.  
  314. }
  315. catch(Exception Ex)
  316. {
  317.  
  318. JOptionPane.showMessageDialog(null, "Invalid input entered");
  319. }
  320.  
  321. }
  322. else JOptionPane.showMessageDialog(null, "The MediaTypes array is full, you cannot add more MediaTypes's");
  323. }
  324.  
  325. //-------------------------------------------------------------------------------
  326. // Procedure: makeNewMagazine
  327. //
  328. // Summary: creates and adds a new magazine entry into media types array
  329. //-------------------------------------------------------------------------------
  330. public void makeNewMagazine()
  331. {
  332. if(MediaTypes.getCounter() <=4)
  333. {
  334. //prompt user for entries
  335. String title = JOptionPane.showInputDialog("Enter name of Magazine");
  336. String genre = JOptionPane.showInputDialog("Enter type of magazine");
  337.  
  338. try
  339. {
  340. String issue = JOptionPane.showInputDialog("Enter issue number");
  341. int i = Integer.parseInt(issue);
  342.  
  343. String day = JOptionPane.showInputDialog("Enter day of release");
  344. int d = Integer.parseInt(day);
  345.  
  346. String month = JOptionPane.showInputDialog("Enter month of release");
  347. int m = Integer.parseInt(month);
  348.  
  349. String year = JOptionPane.showInputDialog("Enter year of release");
  350. int y = Integer.parseInt(year);
  351.  
  352. myMedia[MediaTypes.getCounter()] = new Magazine(title, d, m, y, i, genre);
  353.  
  354. // if(myMedia[counter] != null){counter++;}
  355.  
  356. }
  357. catch(Exception Ex)
  358. {
  359.  
  360. JOptionPane.showMessageDialog(null, "Invalid input entered");
  361.  
  362. }
  363.  
  364. }
  365. else JOptionPane.showMessageDialog(null, "The MediaTypes array is full, you cannot add more MediaTypes's");
  366. }
  367.  
  368. //-------------------------------------------------------------------------------
  369. // Procedure: makeNewBook
  370. //
  371. // Summary: creates and adds a new book entry into media types array
  372. //-------------------------------------------------------------------------------
  373. public void makeNewBook()
  374. {
  375. if(MediaTypes.getCounter() <=4)
  376. {
  377. //prompt user for entries
  378. String title = JOptionPane.showInputDialog("Enter name of Book");
  379. String author = JOptionPane.showInputDialog("Enter author's name");
  380. String genre = JOptionPane.showInputDialog("Enter Fiction or NonFiction");
  381. String publisher = JOptionPane.showInputDialog("Enter publisher");
  382. String hardorpaper = JOptionPane.showInputDialog("Enter hard back or paper back");
  383.  
  384. try
  385. {
  386.  
  387. String day = JOptionPane.showInputDialog("Enter day of release");
  388. int d = Integer.parseInt(day);
  389.  
  390. String month = JOptionPane.showInputDialog("Enter month of release");
  391. int m = Integer.parseInt(month);
  392.  
  393. String year = JOptionPane.showInputDialog("Enter year of release");
  394. int y = Integer.parseInt(year);
  395.  
  396. myMedia[MediaTypes.getCounter()] = new Book(author, title, publisher, d, m, y, genre, hardorpaper);
  397.  
  398. // if(myMedia[counter] != null){counter++;}
  399.  
  400. }
  401. catch(Exception Ex)
  402. {
  403.  
  404. JOptionPane.showMessageDialog(null, "Invalid input entered");
  405. }
  406.  
  407. }
  408. else JOptionPane.showMessageDialog(null, "The MediaTypes array is full, you cannot add more MediaTypes's");
  409. }
  410.  
  411. //-------------------------------------------------------------------------------
  412. // Procedure: showInfo
  413. //
  414. // Summary: displays all entries in the media types array
  415. //-------------------------------------------------------------------------------
  416. public void showInfo()
  417. {
  418.  
  419. String output = "";
  420.  
  421. for(int count = 0; count < myMedia.length; count++)
  422. {
  423. if(myMedia[count] != null)
  424. output += myMedia[count].showInfo() + "\n";
  425.  
  426. }
  427.  
  428. JOptionPane.showMessageDialog(null, output, "All MediaTypess in list", JOptionPane.INFORMATION_MESSAGE);
  429. }
  430.  
  431. //-------------------------------------------------------------------------------
  432. // Procedure: clearAll
  433. //
  434. // Summary: clears all entries in the media types array
  435. //-------------------------------------------------------------------------------
  436. public void clearAll()
  437. {
  438. String clear = JOptionPane.showInputDialog("Clear all Entries in MediaTypes Collection?");
  439.  
  440. Object[] option = { "Yes",
  441. "No"
  442. };
  443.  
  444. Object decision = JOptionPane.showInputDialog(null,
  445. "Please select one of the following:",
  446. "Input",
  447. JOptionPane.INFORMATION_MESSAGE,
  448. null,
  449. option,
  450. option[0]);
  451.  
  452. if(decision.equals(option[0]))
  453. {
  454. for(int j = 0; j < myMedia.length; j++)
  455. {
  456. if(myMedia[j] != null)
  457. {
  458. myMedia[j] = null;
  459. }
  460. }
  461. }
  462. }
  463.  
  464. //-------------------------------------------------------------------------------
  465. // Procedure: sortByNewest
  466. //
  467. // Summary: sorts array values newest to oldest
  468. //-------------------------------------------------------------------------------
  469. public void sortByNewest()
  470. {
  471. if(MediaTypes.getCounter() > 1)
  472. {
  473. MediaTypes extra = new MediaTypes();
  474.  
  475. for(int outer = 0; outer < MediaTypes.getCounter() - 1; outer++)
  476. {
  477. for(int inner = 0; inner < MediaTypes.getCounter() - 1; inner++)
  478. {
  479.  
  480. if( myMedia[inner].getreleaseDate().before(myMedia[inner + 1].getreleaseDate()))
  481. {
  482. //this part i got help from robert
  483. extra = myMedia[inner] ;
  484. myMedia[inner] = myMedia[inner + 1];
  485. myMedia[inner + 1] = extra;
  486. }
  487. }
  488. }
  489. }
  490. }
  491.  
  492. //-------------------------------------------------------------------------------
  493. // Procedure: sortByOldest
  494. //
  495. // Summary: sorts array values oldest to newest
  496. //-------------------------------------------------------------------------------
  497. public void sortByOldest()
  498. {
  499. if(MediaTypes.getCounter() > 1)
  500. {
  501. MediaTypes extra = new MediaTypes();
  502.  
  503. for(int outer = 0; outer < MediaTypes.getCounter() - 1; outer++)
  504. {
  505. for(int inner = 0; inner < MediaTypes.getCounter() - 1; inner++)
  506. {
  507.  
  508. if( myMedia[inner].getreleaseDate().after(myMedia[inner + 1].getreleaseDate()))
  509. {
  510. //this part i got help from robert
  511. extra = myMedia[inner] ;
  512. myMedia[inner] = myMedia[inner + 1];
  513. myMedia[inner + 1] = extra;
  514. }
  515. }
  516. }
  517. }
  518. }
  519.  
  520. //-------------------------------------------------------------------------------
  521. // Procedure: exitProgram
  522. //
  523. // Summary: provides user the means to exit the program
  524. //-------------------------------------------------------------------------------
  525. public void exitProgram()
  526. {
  527.  
  528. Object[] option = { "Yes",
  529. "No"
  530. };
  531.  
  532. Object decision = JOptionPane.showInputDialog(null,
  533. "Are you shure you want to exit?",
  534. "Input",
  535. JOptionPane.INFORMATION_MESSAGE,
  536. null,
  537. option,
  538. option[0]);
  539.  
  540. if(decision.equals(option[0]))
  541. {
  542.  
  543. System.exit( 0 );
  544. }
  545. }
  546.  
  547. }//end class Assn2
  548.  
  549. import javax.swing.*;
  550. import java.util.*;
  551.  
  552.  
  553. public class CD extends MediaTypes
  554. {
  555.  
  556. private String artist;
  557. private String title;
  558. private Calendar releaseDate;
  559. private String label;
  560. private String genre;
  561. public static int counter = 0;
  562.  
  563. public CD(String a, String t, int d, int m, int y, String l, String g)
  564. {
  565. super(m, d, y, t, g);
  566. setArtist(a);
  567. // setTitle(t);
  568. // setreleaseDate(m,d,y) ;
  569. setLabel(l);
  570. // setGenre(g);
  571. counter++;
  572.  
  573. }
  574. public CD(){
  575. }
  576.  
  577. public static void resetCounter(){
  578. counter = 0;
  579.  
  580. }
  581.  
  582. //set methods
  583. /* public void setTitle(String t)
  584. {
  585. title = t;
  586. }
  587. public void setreleaseDate(int m, int d, int y)
  588. {
  589. Calendar releaseDate = new GregorianCalendar(y,m,d);
  590. this.releaseDate = releaseDate;
  591.  
  592. }*/
  593.  
  594. public void setArtist(String a)
  595. {
  596. artist = a;
  597.  
  598. }
  599.  
  600. public void setLabel(String l)
  601. {
  602. label = l;
  603.  
  604. }
  605.  
  606. /* public void setGenre(String g)
  607. {
  608. genre = g;
  609.  
  610. }*/
  611.  
  612. //get methods
  613. /* public String getTitle()
  614. {
  615. return title;
  616. }
  617.  
  618. public Calendar getreleaseDate()
  619. {
  620. return releaseDate;
  621. }*/
  622.  
  623. public String getArtist()
  624. {
  625. return artist;
  626. }
  627.  
  628. public String getLabel()
  629. {
  630. return label;
  631. }
  632.  
  633. /* public String getGenre()
  634. {
  635. return genre;
  636. }
  637.  
  638. public static int getCounter()
  639. {
  640. return counter;
  641. }*/
  642.  
  643. public String showInfo()
  644. {
  645.  
  646. String output= "";
  647.  
  648. output += super.showInfo() + " " + this.getArtist() + " " + this.getLabel();
  649.  
  650. return output;
  651. }
  652.  
  653.  
  654. }
  655.  
  656. import javax.swing.*;
  657. import java.util.*;
  658.  
  659.  
  660. public class MediaTypes
  661. {
  662.  
  663.  
  664. private Calendar releaseDate;
  665. private String title;
  666. private String genre;
  667. public static int counter = 0;
  668.  
  669.  
  670. public MediaTypes(int d, int m, int y, String t, String g)
  671. {
  672. setreleaseDate(m,d,y);
  673. setTitle(t);
  674. setGenre(g);
  675. counter++;
  676. }
  677.  
  678. public MediaTypes(){
  679. }
  680.  
  681. public static void resetCounter(){
  682. counter = 0;
  683.  
  684. }
  685.  
  686. //set methods
  687.  
  688. public void setreleaseDate(int m, int d, int y)
  689. {
  690. Calendar releaseDate = new GregorianCalendar(y,m,d);
  691. this.releaseDate = releaseDate;
  692. }
  693.  
  694. public void setTitle(String t)
  695. {
  696. this.title = title;
  697. }
  698.  
  699. public void setGenre(String g)
  700. {
  701. this.genre = genre;
  702.  
  703. }
  704.  
  705.  
  706. //get methods
  707. public Calendar getreleaseDate()
  708. {
  709. return releaseDate;
  710. }
  711.  
  712. public String getTitle()
  713. {
  714. return title;
  715. }
  716.  
  717. public String getGenre()
  718. {
  719. return genre;
  720. }
  721.  
  722. public static int getCounter()
  723. {
  724. return counter;
  725. }
  726.  
  727.  
  728. public String showInfo()
  729. {
  730.  
  731. String output= "";
  732. output += "Title of CD: " + this.getTitle() + " " + "Release Date: " + this.getreleaseDate() + " " + "Genre: " + this.getGenre();
  733.  
  734. return output;
  735. }
  736.  
  737.  
  738. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joebanks is offline Offline
7 posts
since Sep 2004

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: Random numbers...really random??
Next Thread in Java Forum Timeline: doubly linked list implementation





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


Follow us on Twitter


© 2011 DaniWeb® LLC