Link to home
Start Free TrialLog in
Avatar of rachelee
racheleeFlag for Germany

asked on

Making subclass by inherence

Hello Dear experts,

I am making a university library system programming, I have a below question abt that.

I have created a class named "Media class", which is my main class, and I have Media list in it,how i will code it to get an media list as media list may consists of Title,Id Number,Edition,Author etc etc.

please do help me ...
thnk you so much
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is fair to say that CEHJ idea is more elegant and better practise, make a MediaList class and contain a hash map would be a better approach. Could looke something like this ....(usiing "Media" class as above)


import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

class testbed {
      
      public static void main(String[] args) {
            MediaList ml = new MediaList();
            //create some records  
            ml.addMedia(new Media("1","19 Eighty 4","1","Orwell"));
            ml.addMedia(new Media("2","Animal Farm","1","Orwell"));
            ml.addMedia(new Media("3","Java Cert","1.4","Kahil"));
            ml.addMedia(new Media("44","Coming up 4 O2","5","Orwell"));
            
            System.out.println(ml.getMedia("3").getTitle());
            
            String[] OrwellBooks= ml.getAuthorsIDs("ORWELL");
            System.out.println("There are "+OrwellBooks.length+" Orwell Title(s)");
            
            //print out collection
            for (Iterator iter = ml.getIds().iterator(); iter.hasNext();) {
                  String id = (String) iter.next();
                  Media aMedia = ml.getMedia(id);
                  System.out.println(aMedia.getIdNumber()+"\t"+
                              aMedia.getTitle()+"\t\t"+
                              aMedia.getAuthor()+"\t"+
                              aMedia.getEdition());
            }
            
      }

}

////MediaList class follows...

class  MediaList {

      private HashMap dataMap;

      public MediaList() {
            dataMap = new HashMap();
            
      }
      
      public void addMedia(Media aMediaItem) {
            dataMap.put(aMediaItem.getIdNumber(),aMediaItem);
      }
//some example query methods
      public Media getMedia(String iDtoFind){
            return (Media) dataMap.get(iDtoFind);
      }
      
      public String[] getAuthorsIDs(String authorToFind){
            String result="";//yes I did delphi a lot :-)
            for (Iterator iter = dataMap.keySet().iterator(); iter.hasNext();) {
                  String id = (String) iter.next();
                  Media aMedia = (Media) dataMap.get(id);
                  if (authorToFind.equalsIgnoreCase(aMedia.getAuthor())) {
                        result+= id+",";
                  }
                  
            }
            return result.split(",");
      }
      
      public HashSet getIds(){
            HashSet result = new HashSet();
            for (Iterator iter = dataMap.keySet().iterator(); iter.hasNext();) {
                  String id = (String) iter.next();
                  result.add(id);
            }
            return result;
      }
      
      /// and so on
      
      

      public HashMap getDataMap() {
            return dataMap;
      }
}

//using Media class as defined earlier!!!!


class Media {
      private String IdNumber;
      private String Title;
      private String Edition;
      private String Author;
      
      public Media(String anIdNumber, String aTitle, String anEdition, String anAuthor) {
            // TODO Auto-generated constructor stub
            setIdNumber(anIdNumber);
            setTitle(aTitle);
            setEdition(anEdition);
.....etc as above

Avatar of rachelee

ASKER

thnx a lot for this, will u please tell me, if i would like to make Media as an main class what shall i do?
Hi rachelee,

Media, like Media list should not be in the Main class. In mine, I have called mine testbed. which just simplys creates a MediaList object (called ml)  and populates it and query. Why dont you post whta you have done so far and we will see if we can help (if you dont want to public broadcast your code, email it to Q21@daweb.plus.com).

Just as a point, making list/Collection out inheritance (ie. subclassing, is-a) is not a good idea. Aggreation (has-a)is much better.

g2d
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
more stuff as like what?

i have done like below


public class Media {
      private String mediaId;

      private String mediaTitle;

      private String mediaAuthor;

      private String mediaAbstract;

      private String mediaIsbn;

      private String mediaDueDate;

      private boolean mediaStatus;

      public Media() {
            super();
            // TODO Auto-generated constructor stub
      }

      /**
       * @param mediaId
       * @param mediaTitle
       * @param mediaAuthor
       * @param mediaAbstract
       * @param mediaIsbn
       * @param mediaDueDate
       * @param mediaStatus
       */
      public Media(String mediaId, String mediaTitle, String mediaAuthor,
                  String mediaAbstract, String mediaIsbn, String mediaDueDate,
                  boolean mediaStatus) {            
            this.mediaId = mediaId;
            this.mediaTitle = mediaTitle;
            this.mediaAuthor = mediaAuthor;
            this.mediaAbstract = mediaAbstract;
            this.mediaIsbn = mediaIsbn;
            this.mediaDueDate = mediaDueDate;
            this.mediaStatus = mediaStatus;
      }

      /**
       * @return Returns the mediaAbstract.
       */
      public String getMediaAbstract() {
            return mediaAbstract;
      }

      /**
       * @param mediaAbstract
       *            The mediaAbstract to set.
       */
      public void setMediaAbstract(String mediaAbstract) {
            this.mediaAbstract = mediaAbstract;
      }

      /**
       * @return Returns the mediaAuthor.
       */
      public String getMediaAuthor() {
            return mediaAuthor;
      }

      /**
       * @param mediaAuthor
       *            The mediaAuthor to set.
       */
      public void setMediaAuthor(String mediaAuthor) {
            this.mediaAuthor = mediaAuthor;
      }

      /**
       * @return Returns the mediaDueDate.
       */
      public String getMediaDueDate() {
            return mediaDueDate;
      }

      /**
       * @param mediaDueDate
       *            The mediaDueDate to set.
       */
      public void setMediaDueDate(String mediaDueDate) {
            this.mediaDueDate = mediaDueDate;
      }

      /**
       * @return Returns the mediaId.
       */
      public String getMediaId() {
            return mediaId;
      }

      /**
       * @param mediaId
       *            The mediaId to set.
       */
      public void setMediaId(String mediaId) {
            this.mediaId = mediaId;
      }

      /**
       * @return Returns the mediaIsbn.
       */
      public String getMediaIsbn() {
            return mediaIsbn;
      }

      /**
       * @param mediaIsbn
       *            The mediaIsbn to set.
       */
      public void setMediaIsbn(String mediaIsbn) {
            this.mediaIsbn = mediaIsbn;
      }

      /**
       * @return Returns the mediaStatus.
       */
      public boolean getMediaStatus() {
            return mediaStatus;
      }

      /**
       * @param mediaStatus
       *            The mediaStatus to set.
       */
      public void setMediaStatus(boolean mediaStatus) {
            this.mediaStatus = mediaStatus;
      }

      /**
       * @return Returns the mediaTitle.
       */
      public String getMediaTitle() {
            return mediaTitle;
      }

      /**
       * @param mediaTitle
       *            The mediaTitle to set.
       */
      public void setMediaTitle(String mediaTitle) {
            this.mediaTitle = mediaTitle;
      }

}
Ah yes, but i thought you required, in the class in question, a collection of items? That looks like the code for an item itself, and as such, it's fine. Elsewhere, you'd probably need a list of these items
I would like to know if i can make it in one line?rather than putting it different all the time

public class Media {
     private String mediaId;

     private String mediaTitle;

     private String mediaAuthor;

     private String mediaAbstract;

     private String mediaIsbn;

     private String mediaDueDate;

     private boolean mediaStatus;

     public Media() {
          super();
          // TODO Auto-generated constructor stub
     }

Isn't that partly the question in your new one? Can you prompt me over there again please?
Mr GTwOD,

Let me know if i can make media and media list also in my media class
or, how shall i put my code in ur format,

CEHJ,
 if u  can please tell me other way to do the same thing, short coding system from my programm
>>Isn't that partly the question in your new one? Can you prompt me over there again please?

am sorry, did not understand what do u want, will u please make me understand
>>if u  can please tell me other way to do the same thing, short coding system from my programm

Please prompt me in your new question that you posted
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)
Thanks for the points and the A grade!

Good luck with you studies

:-)

g2d