Results 1 to 4 of 4
  1. #1
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default [SOLVED] how to print a class that extends another class

    Hello, I am very new to java and taking a Obj Oriented Prog class and have a question about a project we are doing. We have this programs that reads and prints out CD,DVD,Books, etc. Now I got it to print the complete list of everything in the Item list but not sure how to print out a specific class. So here is my code for the 3 classes:

    Item class:
    Java Code:
    public class Item
    {
        private String title;
        private int playingTime;
        private boolean gotIt;
        private String comment;
    
        /**
         * Initialise the fields of the item.
         * @param theTitle The title of this item.
         * @param time The running time of this item.
         */
        public Item(String theTitle, int time)
        {
            title = theTitle;
            playingTime = time;
            gotIt = false;
            comment = "<no comment>";
        }
    
        /**
         * Get the title.
         */
        public String getTitle()
        {
            return title;
        }
    
        /**
         * Get the playing time.
         */
        public int getPlayingTime()
        {
            return playingTime;
        }
    
    
        /**
         * @return The comment for this item.
         */
        public String getComment()
        {
            return comment;
        }
    
        /**
         * Enter a comment for this item.
         * @param comment The comment to be entered.
         */
        public void setComment(String comment)
        {
            this.comment = comment;
        }
        
        /**
         * returns whether we have it
         */
        public boolean getOwn() {
            return gotIt;
        }
    
        /**
         * Set the flag indicating whether we own this item.
         * @param ownIt true if we own the item, false otherwise.
         */
        public void setOwn(boolean ownIt)
        {
            gotIt = ownIt;
        }
        
        /**
         * @return String containing class-specific info, for search()
         */
        public String keywords() {
            return "";
        }
    
        /**
         * Print details about this item to the text terminal.
         */
        public void print()
        {
            System.out.print("title: " + title + " (" + playingTime + " mins)");        
            if(gotIt) {
                System.out.println("*");
            } else {
                System.out.println();
            }
            System.out.println("    " + "Comments: " + comment);
        }
        
        public void printNodetail()
        {
            System.out.print("Game: " + getTitle());
            if(getOwn() ) {
                System.out.println("*");
            }
        }
        
    }
    Database class:
    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    
    /**
     * The database class provides a facility to store CD and DVD 
     * objects. A list of all CDs and DVDs can be printed to the
     * terminal.
     * 
     * This version does not save the data to disk.
     * 
     * @author Michael Kolling and David J. Barnes
     * @version 2002-05-02
     */
    public class Database
    {
        // add "books" here, and initialize in the constructor -- pld
        private ArrayList<Item> items;
        
        /**
         * Construct an empty Database.
         */
        public Database()
        {
            items = new ArrayList<Item>();
        }
    
        /**
         * Add an Item to the database.
         */
        public void addItem(Item theItem)
        {
            items.add(theItem);
        }
        
        
    
        /**
         * Print a list of all currently stored CDs and DVDs to the
         * text terminal.
         */
        public void list()
        {
            // print list of items
            for(Item theItem : items ) {
                theItem.print();
                System.out.println();   // empty line between items
            }
        }
        
        public void listNoDetail()
        {
            // print list with no deatil of items
            for(Item theItem : items ) {
                theItem.printNodetail();
                System.out.println();   // empty line between items
    
            }
        }
        
        public void listCDs()
        {
            for(Item theItem : items ) {
                theItem.print();
                System.out.println();   // empty line between items
            }
        }
        
        /**
         * The following should search all the Items for anything with a title
         * that contains the string "srchkey".
         * Items with a match are printed.
         * Make the comparisons case-insensitive by converting to lowercase.
         * -- pld
         */
        public void searchTitle (String srchkey) {
            srchkey = srchkey.toLowerCase();
            System.out.println("Searching for: " + srchkey);
            for (Item it : items) {
                String title = it.getTitle().toLowerCase();
                if (title.contains(srchkey) ) {
                    it.print();
                }
            }   
            System.out.println("=========== done with search ============");
        }
        
         /**
         * The following should search all the Items for anything in either 
         * the title or the keywords() string (artist/director/etc) 
         * that contains the string srchkey. 
         * Make the comparisons case-insensitive by converting to lowercase.
         * -- pld
         */
        public void searchAll (String srchkey) {
            srchkey = srchkey.toLowerCase();
            // rest like above, except use title+keywords() instead of just title
            
            System.out.println("Searching for: " + srchkey);
            for (Item it : items) {
                String title = (it.getTitle() + " " + it.keywords()).toLowerCase();
                if (title.contains(srchkey) ) {
                    it.print();
                }
            }   
            System.out.println("=========== done with search ============");
        }
        
        /**
         * Add a few entries for demo purposes - pld
         */
        public void add_demo() {
            addItem (new CD("Blood Money", "Tom Waits", 14, 63));
            addItem (new CD("bad hair day", "Weird Al Yankovic", 12, 65));
            addItem (new CD("Songs in the Key of Life", "Stevie Wonder", 21, 120));
            addItem (new CD("The White Ablum", "The Beatles", 20, 93));
            addItem (new DVD("The Harder They Come", "Perry Henzell", "Jimmy Cliff", 104));
            addItem (new DVD("Brazil", "Terry Gilliam", "Pryce, Greist, Palin", 118));
            addItem (new DVD("Monty Python's Meaning of Life", "Terry Jones & Terry Gilliam", 
                    "Jones, Cleese, Chapman, Idle, Palin, Gilliam", 90));
            addItem (new DVD("The Secret Life of Walter Mitty", "Mark Waters", 
                    "Mike Myers", 110));
            addItem (new DVD("Life or Something Like It", "Stephen Herek", 
                    "Angelina Jolie", 98));
            addItem(new Game("Warcraft", "Blizzard", 1));
            addItem(new Game("Gears of War 1 & 2", "IGN", 2));
            addItem(new Game("Rainbow Six 1 & 2", "Ubisoft", 2));
            addItem(new Book("The Secret Life of Walter Mitty", "James Thurber"));
            addItem(new Book("The Dark Tower I - VII", "Stephen King"));
        }
    }
    CD class:
    Java Code:
    public class CD extends Item
    {
        private String artist;
        private int numberOfTracks;
    
        /**
         * Initialize the CD.
         */
        public CD(String theTitle, String theArtist, int tracks, int time)
        {
            super(theTitle, time);
            artist = theArtist;
            numberOfTracks = tracks;
        }
    
        /**
         * Gets the artist -- pld
         */
        public String getArtist() {
            return artist;
        }
        
        /**
         * Gets the number of tracks -- pld
         */
        public int getNumberOfTracks() {
            return numberOfTracks;
        }
        
        /**
         * @return String containing artist
         */
        public String keywords() {
            return artist;
        }
        
        /**
         * Print details about this CD to the text terminal.
         * This method prints all fields directly, without using the 
         * superclass (class Item) version of print() at all.
         * Instead, Item's accessors are used to get the necessary data.
         */
        public void print()
        {
            System.out.print("CD: " + getTitle() + " (" + getPlayingTime() + " mins)");
            if(getOwn() ) {
                System.out.println("*");
            } else {
                System.out.println();
            }
            System.out.println("    " + "Artist: " + artist);
            System.out.println("    tracks: " + numberOfTracks);
            System.out.println("    " + "Comments: " + getComment());
        }
        
        public void printNodetail()
        {
            System.out.print("CD: " + getTitle());
            if(getOwn() ) {
                System.out.println("*");
            }
            //System.out.println();   // empty line between items
        }
    }
    Now CD extends Item so I thought I would create a new method (printCD) and use the same print method in the Database class but not sure how to tell it to use the CD class. Any help would be most appreciated.

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    use instanceof keyword.

    Java Code:
            for(Item theItem : items ) {
              if(theItem instanceof CD){
                // println code here.
                }
            }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Holy crap that was perfect, you are a genius, thank you so much. I will definitely coming back if i get stuck again.

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you're welcome :)
    if the thread is solved, please mark it as solved by using the thread toolbar at the top. (below the ads)
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  2. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  3. Replies: 0
    Last Post: 05-13-2008, 06:16 AM
  4. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:12 PM
  5. Replies: 0
    Last Post: 04-02-2008, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •