| | |
help with polymorphism and inheritance...getting null values
![]() |
•
•
Join Date: Sep 2004
Posts: 7
Reputation:
Solved Threads: 0
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)
import java.util.Date; import javax.swing.*; public class Assn2{ private MediaTypes[] myMedia; private Date release; private boolean go = true; public static void main(String[] args) { new Assn2(); } //------------------------------------------------------------------------------- // Procedure: Assn2 // // Summary: main driver method for program //------------------------------------------------------------------------------- public Assn2() { //initialize data members myMedia = new MediaTypes[5]; //provide a dimension for the array while(go == true) { //main menu for navigating program Object[] possibleValues = { "Make New Entry", "View Entries", "Clear Entires", "Oldest Entries First", "Newest Entries First", "Exit"}; Object selectedValue = JOptionPane.showInputDialog(null, "Please select one of the following:", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]); if(selectedValue.equals(possibleValues[0])) { //select Media Type to enter new Object[] possibleMedia = {"Music", "Movies", "Print"}; Object selectedMedia = JOptionPane.showInputDialog(null, "Please select your media type:", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleMedia, possibleMedia[0]); if(selectedMedia.equals(possibleMedia[0])) { Object[] musicChoices = {"CD", "Cassette"}; Object selectedMusic = JOptionPane.showInputDialog(null, "Please select your music type:", "Input", JOptionPane.INFORMATION_MESSAGE, null, musicChoices, musicChoices[0]); //make new cd if(selectedMusic.equals(musicChoices[0])) { makeNewCD(); } //make new cassette else {makeNewCassette();} } if(selectedMedia.equals(possibleMedia[1])) { Object[] movieChoices = {"DVD", "VHS"}; Object selectedMovie = JOptionPane.showInputDialog(null, "Please select your Movie type:", "Input", JOptionPane.INFORMATION_MESSAGE, null, movieChoices, movieChoices[0]); //make new DVD if(selectedMovie.equals(movieChoices[0])) { makeNewDVD(); } //make new VHS else {makeNewVHS();} } if(selectedMedia.equals(possibleMedia[2])) { Object[] printChoices = {"Magazine", "Book"}; Object selectedPrint = JOptionPane.showInputDialog(null, "Please select your Print type:", "Input", JOptionPane.INFORMATION_MESSAGE, null, printChoices, printChoices[0]); //make new Magazine if(selectedPrint.equals(printChoices[0])) { makeNewMagazine(); } //make new Book else {makeNewBook();} } } //view all entries in the MediaTypes array if(selectedValue.equals(possibleValues[1])) { showInfo(); } //clear all entries in the MediaTypes array if(selectedValue.equals(possibleValues[2])) { clearAll(); } //sort by oldest first and //show the entries in that order if(selectedValue.equals(possibleValues[3])) { JOptionPane.showMessageDialog(null,"Entries have been sorted by Oldest to Newest"); sortByOldest(); } //sort by newest first and //show entriest in that order if(selectedValue.equals(possibleValues[4])) { JOptionPane.showMessageDialog(null,"Entries have been sorted by Newest to Oldest"); sortByNewest(); } //user selected exit the program.... //make sure they really want to exit if(selectedValue.equals(possibleValues[5])) { exitProgram(); } } } //------------------------------------------------------------------------------- // Procedure: makeNewCD // // Summary: creates and adds a new CD entry into media types array //------------------------------------------------------------------------------- public void makeNewCD() { if(MediaTypes.getCounter() <=4) { //prompt user for entries String title = JOptionPane.showInputDialog("Enter name of CD"); String genre = JOptionPane.showInputDialog("Enter type of music"); String label = JOptionPane.showInputDialog("Enter label the artist is on"); String artist = JOptionPane.showInputDialog("Enter artist name"); try { String day = JOptionPane.showInputDialog("Enter day of release"); int d = Integer.parseInt(day); String month = JOptionPane.showInputDialog("Enter month of release"); int m = Integer.parseInt(month); String year = JOptionPane.showInputDialog("Enter year of release"); int y = Integer.parseInt(year); myMedia[MediaTypes.getCounter()] = new CD(artist, title, d, m, y, genre, label ); // if(myMedia[counter] != null){counter++;} } catch(Exception Ex) { JOptionPane.showMessageDialog(null, "Invalid input entered"); } } else JOptionPane.showMessageDialog(null, "The MediaTypes array is full, you cannot add more MediaTypes's"); } //------------------------------------------------------------------------------- // Procedure: makeNewCassette // // Summary: creates and adds a new Cassette entry into media types array //------------------------------------------------------------------------------- public void makeNewCassette() { if(MediaTypes.getCounter() <=4) { //prompt user for entries String title = JOptionPane.showInputDialog("Enter name of Cassette"); String genre = JOptionPane.showInputDialog("Enter type of music"); String label = JOptionPane.showInputDialog("Enter label the artist is on"); String mixtape = JOptionPane.showInputDialog("Is it a mixtape?"); try { String day = JOptionPane.showInputDialog("Enter day of release"); int d = Integer.parseInt(day); String month = JOptionPane.showInputDialog("Enter month of release"); int m = Integer.parseInt(month); String year = JOptionPane.showInputDialog("Enter year of release"); int y = Integer.parseInt(year); myMedia[MediaTypes.getCounter()] = new Cassette(title, d, m, y, label, genre, mixtape ); // if(myMedia[counter] != null){counter++;} } catch(Exception Ex) { JOptionPane.showMessageDialog(null, "Invalid input entered"); } } else JOptionPane.showMessageDialog(null, "The MediaTypes array is full, you cannot add more MediaTypes's"); } //------------------------------------------------------------------------------- // Procedure: makeNewDVD // // Summary: creates and adds a new DVD entry into media types array //------------------------------------------------------------------------------- public void makeNewDVD() { if(MediaTypes.getCounter() <=4) { //prompt user for entries String title = JOptionPane.showInputDialog("Enter title of DVD"); String genre = JOptionPane.showInputDialog("Enter genre of movie"); String dir = JOptionPane.showInputDialog("Enter the director of the movie"); String pirated = JOptionPane.showInputDialog("Is this movie pirated?"); try { String day = JOptionPane.showInputDialog("Enter day of release"); int d = Integer.parseInt(day); String month = JOptionPane.showInputDialog("Enter month of release"); int m = Integer.parseInt(month); String year = JOptionPane.showInputDialog("Enter year of release"); int y = Integer.parseInt(year); myMedia[MediaTypes.getCounter()] = new DVD(title, dir, d, m, y, genre, pirated ); // if(myMedia[counter] != null){counter++;} } catch(Exception Ex) { JOptionPane.showMessageDialog(null, "Invalid input entered"); } } else JOptionPane.showMessageDialog(null, "The MediaTypes array is full, you cannot add more MediaTypes's"); } //------------------------------------------------------------------------------- // Procedure: makeNewVHS // // Summary: creates and adds a new VHS entry into media types array //------------------------------------------------------------------------------- public void makeNewVHS() { if(MediaTypes.getCounter() <=4) { //prompt user for entries String title = JOptionPane.showInputDialog("Enter title of VHS tape"); String genre = JOptionPane.showInputDialog("Enter genre of the movie"); String dir = JOptionPane.showInputDialog("Enter the director of the movie"); try { String day = JOptionPane.showInputDialog("Enter day of release"); int d = Integer.parseInt(day); String month = JOptionPane.showInputDialog("Enter month of release"); int m = Integer.parseInt(month); String year = JOptionPane.showInputDialog("Enter year of release"); int y = Integer.parseInt(year); myMedia[MediaTypes.getCounter()] = new VHS(title, dir, d, m, y, genre); // if(myMedia[counter] != null){counter++;} } catch(Exception Ex) { JOptionPane.showMessageDialog(null, "Invalid input entered"); } } else JOptionPane.showMessageDialog(null, "The MediaTypes array is full, you cannot add more MediaTypes's"); } //------------------------------------------------------------------------------- // Procedure: makeNewMagazine // // Summary: creates and adds a new magazine entry into media types array //------------------------------------------------------------------------------- public void makeNewMagazine() { if(MediaTypes.getCounter() <=4) { //prompt user for entries String title = JOptionPane.showInputDialog("Enter name of Magazine"); String genre = JOptionPane.showInputDialog("Enter type of magazine"); try { String issue = JOptionPane.showInputDialog("Enter issue number"); int i = Integer.parseInt(issue); String day = JOptionPane.showInputDialog("Enter day of release"); int d = Integer.parseInt(day); String month = JOptionPane.showInputDialog("Enter month of release"); int m = Integer.parseInt(month); String year = JOptionPane.showInputDialog("Enter year of release"); int y = Integer.parseInt(year); myMedia[MediaTypes.getCounter()] = new Magazine(title, d, m, y, i, genre); // if(myMedia[counter] != null){counter++;} } catch(Exception Ex) { JOptionPane.showMessageDialog(null, "Invalid input entered"); } } else JOptionPane.showMessageDialog(null, "The MediaTypes array is full, you cannot add more MediaTypes's"); } //------------------------------------------------------------------------------- // Procedure: makeNewBook // // Summary: creates and adds a new book entry into media types array //------------------------------------------------------------------------------- public void makeNewBook() { if(MediaTypes.getCounter() <=4) { //prompt user for entries String title = JOptionPane.showInputDialog("Enter name of Book"); String author = JOptionPane.showInputDialog("Enter author's name"); String genre = JOptionPane.showInputDialog("Enter Fiction or NonFiction"); String publisher = JOptionPane.showInputDialog("Enter publisher"); String hardorpaper = JOptionPane.showInputDialog("Enter hard back or paper back"); try { String day = JOptionPane.showInputDialog("Enter day of release"); int d = Integer.parseInt(day); String month = JOptionPane.showInputDialog("Enter month of release"); int m = Integer.parseInt(month); String year = JOptionPane.showInputDialog("Enter year of release"); int y = Integer.parseInt(year); myMedia[MediaTypes.getCounter()] = new Book(author, title, publisher, d, m, y, genre, hardorpaper); // if(myMedia[counter] != null){counter++;} } catch(Exception Ex) { JOptionPane.showMessageDialog(null, "Invalid input entered"); } } else JOptionPane.showMessageDialog(null, "The MediaTypes array is full, you cannot add more MediaTypes's"); } //------------------------------------------------------------------------------- // Procedure: showInfo // // Summary: displays all entries in the media types array //------------------------------------------------------------------------------- public void showInfo() { String output = ""; for(int count = 0; count < myMedia.length; count++) { if(myMedia[count] != null) output += myMedia[count].showInfo() + "\n"; } JOptionPane.showMessageDialog(null, output, "All MediaTypess in list", JOptionPane.INFORMATION_MESSAGE); } //------------------------------------------------------------------------------- // Procedure: clearAll // // Summary: clears all entries in the media types array //------------------------------------------------------------------------------- public void clearAll() { String clear = JOptionPane.showInputDialog("Clear all Entries in MediaTypes Collection?"); Object[] option = { "Yes", "No" }; Object decision = JOptionPane.showInputDialog(null, "Please select one of the following:", "Input", JOptionPane.INFORMATION_MESSAGE, null, option, option[0]); if(decision.equals(option[0])) { for(int j = 0; j < myMedia.length; j++) { if(myMedia[j] != null) { myMedia[j] = null; } } } } //------------------------------------------------------------------------------- // Procedure: sortByNewest // // Summary: sorts array values newest to oldest //------------------------------------------------------------------------------- public void sortByNewest() { if(MediaTypes.getCounter() > 1) { MediaTypes extra = new MediaTypes(); for(int outer = 0; outer < MediaTypes.getCounter() - 1; outer++) { for(int inner = 0; inner < MediaTypes.getCounter() - 1; inner++) { if( myMedia[inner].getreleaseDate().before(myMedia[inner + 1].getreleaseDate())) { //this part i got help from robert extra = myMedia[inner] ; myMedia[inner] = myMedia[inner + 1]; myMedia[inner + 1] = extra; } } } } } //------------------------------------------------------------------------------- // Procedure: sortByOldest // // Summary: sorts array values oldest to newest //------------------------------------------------------------------------------- public void sortByOldest() { if(MediaTypes.getCounter() > 1) { MediaTypes extra = new MediaTypes(); for(int outer = 0; outer < MediaTypes.getCounter() - 1; outer++) { for(int inner = 0; inner < MediaTypes.getCounter() - 1; inner++) { if( myMedia[inner].getreleaseDate().after(myMedia[inner + 1].getreleaseDate())) { //this part i got help from robert extra = myMedia[inner] ; myMedia[inner] = myMedia[inner + 1]; myMedia[inner + 1] = extra; } } } } } //------------------------------------------------------------------------------- // Procedure: exitProgram // // Summary: provides user the means to exit the program //------------------------------------------------------------------------------- public void exitProgram() { Object[] option = { "Yes", "No" }; Object decision = JOptionPane.showInputDialog(null, "Are you shure you want to exit?", "Input", JOptionPane.INFORMATION_MESSAGE, null, option, option[0]); if(decision.equals(option[0])) { System.exit( 0 ); } } }//end class Assn2 import javax.swing.*; import java.util.*; public class CD extends MediaTypes { private String artist; private String title; private Calendar releaseDate; private String label; private String genre; public static int counter = 0; public CD(String a, String t, int d, int m, int y, String l, String g) { super(m, d, y, t, g); setArtist(a); // setTitle(t); // setreleaseDate(m,d,y) ; setLabel(l); // setGenre(g); counter++; } public CD(){ } public static void resetCounter(){ counter = 0; } //set methods /* public void setTitle(String t) { title = t; } public void setreleaseDate(int m, int d, int y) { Calendar releaseDate = new GregorianCalendar(y,m,d); this.releaseDate = releaseDate; }*/ public void setArtist(String a) { artist = a; } public void setLabel(String l) { label = l; } /* public void setGenre(String g) { genre = g; }*/ //get methods /* public String getTitle() { return title; } public Calendar getreleaseDate() { return releaseDate; }*/ public String getArtist() { return artist; } public String getLabel() { return label; } /* public String getGenre() { return genre; } public static int getCounter() { return counter; }*/ public String showInfo() { String output= ""; output += super.showInfo() + " " + this.getArtist() + " " + this.getLabel(); return output; } } import javax.swing.*; import java.util.*; public class MediaTypes { private Calendar releaseDate; private String title; private String genre; public static int counter = 0; public MediaTypes(int d, int m, int y, String t, String g) { setreleaseDate(m,d,y); setTitle(t); setGenre(g); counter++; } public MediaTypes(){ } public static void resetCounter(){ counter = 0; } //set methods public void setreleaseDate(int m, int d, int y) { Calendar releaseDate = new GregorianCalendar(y,m,d); this.releaseDate = releaseDate; } public void setTitle(String t) { this.title = title; } public void setGenre(String g) { this.genre = genre; } //get methods public Calendar getreleaseDate() { return releaseDate; } public String getTitle() { return title; } public String getGenre() { return genre; } public static int getCounter() { return counter; } public String showInfo() { String output= ""; output += "Title of CD: " + this.getTitle() + " " + "Release Date: " + this.getreleaseDate() + " " + "Genre: " + this.getGenre(); return output; } }
![]() |
Similar Threads
- Null values from database causing fatal error (PHP)
- Update the null values of a particular column (MS SQL)
- Need help with NULL values (ColdFusion)
- datagrid replacing null values (VB.NET)
- how to select null values in condition (MySQL)
- Exception Data is Null. This method or property cannot be called on Null values. (VB.NET)
- Need help diplaying null values (PHP)
Other Threads in the Java Forum
- Previous Thread: Random numbers...really random??
- Next Thread: doubly linked list implementation
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card class client code collision component crashcourse css csv database eclipse ee error fractal free ftp game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linked linux list loan machine map method methods migrate mobile netbeans objects oriented output phone physics printf problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext scanner se server service set sms software sort sql string swing test textfield threads tree trolltech ubuntu utility windows





