[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

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
0
rachelee
Asked:
rachelee
  • 6
  • 5
  • 5
  • +1
5 Solutions
 
CEHJCommented:
>>how i will code it to get an media list as media list may consists of Title,Id Number,Edition,Author etc etc.

Make a MediaItem class that has those attributes. MediaList will be a List of MediaItem

List<MediaItem> media = new ArrayList<MediaItem>();
0
 
GTwoDCommented:
or use an collection, hashmap would be ideal...
have a look at the example below.

testbed.java

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

class testbed {
      
      public static void main(String[] args) {
            HashMap MediaList = new HashMap();
            //create some records  
            Media Media1 = new Media("1","1984","1","Orwell");
            Media Media2 = new Media("2","Animal Farm","1","Orwell");
            Media Media3 = new Media("3","Java Cert","1.4","Kahil");
            //add to collection
            MediaList.put(Media1.getIdNumber(),Media1);
            MediaList.put(Media2.getIdNumber(),Media2);
            MediaList.put(Media3.getIdNumber(),Media3);
            //add on the fly (not a good idea having the id part of the record)
            MediaList.put("4",new Media("4","Java Cert 5","1.5","Kahil"));
            
//print out "id:title-author"
            for (Iterator iter = MediaList.keySet().iterator(); iter.hasNext();) {
                  String id = (String) iter.next();
                  Media aMedia=(Media) MediaList.get(id);
                  System.out.println(aMedia.getIdNumber()+":"+aMedia.getTitle()+"-"+aMedia.getAuthor());
                  
            }
            
            

      }

}


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);
            setAuthor(anAuthor);
      }

      public void setIdNumber(String idNumber) {
            IdNumber = idNumber;
      }

      public String getIdNumber() {
            return IdNumber;
      }

      public void setTitle(String title) {
            Title = title;
      }

      public String getTitle() {
            return Title;
      }

      public void setEdition(String edition) {
            Edition = edition;
      }

      public String getEdition() {
            return Edition;
      }

      public void setAuthor(String author) {
            Author = author;
      }

      public String getAuthor() {
            return Author;
      }
      
}
 
0
 
objectsCommented:
What you want is a collection of beans

GTwoD has provided a good example above, here are also some tutorials to give you some background:


http://www.drbob42.com/JBuilder/jb210t.htm
http://java.sun.com/docs/books/tutorial/collections/index.html
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
GTwoDCommented:
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

0
 
racheleeAuthor Commented:
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?
0
 
GTwoDCommented:
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
0
 
CEHJCommented:
>>if i would like to make Media as an main class what shall i do?

public class Media {
      private List<MediaItem> media = new ArrayList<MediaItem>();

// More stuff
}
0
 
racheleeAuthor Commented:
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;
      }

}
0
 
CEHJCommented:
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
0
 
racheleeAuthor Commented:
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
     }

0
 
CEHJCommented:
Isn't that partly the question in your new one? Can you prompt me over there again please?
0
 
racheleeAuthor Commented:
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
0
 
racheleeAuthor Commented:
>>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
0
 
CEHJCommented:
>>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
0
 
GTwoDCommented:
Hi Rachel

Just notice you open another question class

The media list will work with your existing class with a couple changes which I have done.

Email me your assignment so I know what you tutor is trying to get you to do.

Note the test bed will be diffrent.


MediaList {

     private HashMap dataMap;

     public MediaList() {
          dataMap = new HashMap();
         
     }
     
     public void addMedia(Media aMediaItem) {
          dataMap.put(aMediaItem.getMediaId(),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.getMediaAuthor())) {
                    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;
     }
}
0
 
CEHJCommented:
:-)
0
 
GTwoDCommented:
Thanks for the points and the A grade!

Good luck with you studies

:-)

g2d
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now