creating list of strings together

Hello ,

Here I have created a main class Media where i would like to  assign private strings together ,rather than taking everytime private string, private string id,, how shall i do it?


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
      }

            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;
      }
racheleeAsked:
Who is Participating?
 
objectsCommented:
If you add getter and setter method you can  vat ste the troperties of your bean as you have dome for address.
See the link I posted in previous q about implmenting beans.

0
 
CEHJCommented:
That's  OK - what makes you doubtful? I would call it MediaItem though
0
 
racheleeAuthor Commented:
oh sorry, i just missunderstood and open that old one


i would really like to know if there is another way to represent the same code above.
I want it to be shortened or if i can represent by another way

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
CEHJCommented:
You could do this:

public class Media {
     private String mediaId, mediaTitle, mediaAuthor; // (and the rest)
...

but personally i wouldn't - it's less readable. However you could make and call a ctor using a String[]


public Media(String[] attributes, boolean mediaStatus) {
      this(attributes[0],
            attributes[1],
            attributes[2],
            attributes[3],
            attributes[4],
            attributes[5],
            mediaStatus); // This could be simply 'status'
}
0
 
racheleeAuthor Commented:
will u please tell me instead of using super (Autogenerated stub)

what we can use else?
0
 
CEHJCommented:
You should leave that there, so it's bean-compliant, although it can be

public Media() {
}
0
 
racheleeAuthor Commented:
please will u tell me another way to make this , i have to show it today twelve clock, otherwise loosing all marks,

Now I have two class , named ,Customer and Media, but i will have to make attributes  there in it, let me know how? please some other way, i want other shortened way
shall I post u both class so that u can give me the short way than this????
0
 
CEHJCommented:
>>please some other way, i want other shortened way

There isn't one AFAIK. What's the problem anyway - you've already written it?

>>shall I post u both class

You could post the Customer class, but i'm not sure if it'd be relevant.
0
 
racheleeAuthor Commented:
my media class is as follw

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 mediaAbs


Customer class is as follow


public class Customer {
      private String id;

      private String name;

      private String address;

      private String email;

      private String dateOfBirth;

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

      public Customer(String id, String name, String address, String email,
                  String dateOfBirth) {
            this.id = id;
            this.name = name;
            this.address = address;
            this.email = email;
            this.dateOfBirth = dateOfBirth;
      }

      /**
       * @return Returns the address.
       */
      public String getAddress() {
            return address;
      }

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

      /**
       * @return Returns the dateOfBirth.
0
 
CEHJCommented:
That's fine. The only way to shorten it i showed above

http:Q_21866416.html#16778811
0
 
racheleeAuthor Commented:
will u please tell us another way to present it as my teacher does not want it to be like this,

thnx
0
 
hoomanvCommented:
> as my teacher does not want it to be like this,
there is no way in Java

the C++ way:

class Media {
    private:
        string mediaId;
        ...
};
0
 
racheleeAuthor Commented:
no i want in java, atleast if i can do changes in same or not?

He told , do it without using super
0
 
CEHJCommented:
I told you above, just remove super()
0
 
racheleeAuthor Commented:
ok, and all rest , i have to keep as it is or???
0
 
hoomanvCommented:
public Customer() {
    super();
}

super is redundant in this case, because Customer is merely inherited from Object, and the super class's constructor does not need any parameters
0
 
GTwoDCommented:

http://www.experts-exchange.com/Q_21866174.html 

have a look at the way I did it for you with accessor methods, why dont you postr the assignment question, I got a feeling that you are missing something out about the question

0
 
Nguyen Huu PhuocSenior ManagerCommented:
Please try my code:
import java.util.*;
public class Media {
     private ArrayList properties;
  public Media()
  {
  properties = Collections.EMPTY_LIST;    
  }
 public Media(ArrayList properites)
{
  this.properties=properties;
}
public setProperties(ArrayList properties)
{
  this.properties=properties;
}
public ArrayList getProperties()
{
 return properties;
}
}
I think my code is short enough for you.
When you want to access properties:
Arraylist media = m.getProperties();
String mediaId = (String)media.get(0);
String mediaTitle = (String)media.get(1);
------------------------------------------------------------------
boolean mediaStatus = (Boolean)media.get(someindex).booleanValue();
For convience, you can use HashMap instead of ArrayList.
When you use HashMap you  can use key instead of index which isnot memorizable.
import java.util.*;
public class Media {
   private HashMap properties;
  public Media()
  {
  properties = Collections.EMPTY_MAP;    
  }
 public Media(HashMap properites)
{
  this.properties=properties;
}
public setProperties(HashMap properties)
{
  this.properties=properties;
}
public HashMap getProperties()
{
 return properties;
}
}

HashMap media =    m.getProperties();
String mediaId =     (String)media.get("media.ID");
String mediaTitle = (String)media.get("media.TITLE");
---------------------------------------------------------------------------------
boolean mediaStatus = (Boolean)media.get("media.STATUS").booleanValue();
Phuoc H. Nguyen

0
 
CEHJCommented:
:-)

>>i want other shortened way

Why then did you accept a much *longer* way of setting the attributes as the 'answer'? ;-)
0
 
Nguyen Huu PhuocSenior ManagerCommented:
An anwser of genius!
:-)
Phuoc
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.