?
Solved

creating list of strings together

Posted on 2006-05-28
20
Medium Priority
?
196 Views
Last Modified: 2010-03-31
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;
      }
0
Comment
Question by:rachelee
  • 7
  • 7
  • 2
  • +3
20 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16778721
That's  OK - what makes you doubtful? I would call it MediaItem though
0
 

Author Comment

by:rachelee
ID: 16778804
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 600 total points
ID: 16778811
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rachelee
ID: 16778825
will u please tell me instead of using super (Autogenerated stub)

what we can use else?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16778832
You should leave that there, so it's bean-compliant, although it can be

public Media() {
}
0
 

Author Comment

by:rachelee
ID: 16778842
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16778849
>>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
 

Author Comment

by:rachelee
ID: 16778851
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16778856
That's fine. The only way to shorten it i showed above

http:Q_21866416.html#16778811
0
 

Author Comment

by:rachelee
ID: 16778874
will u please tell us another way to present it as my teacher does not want it to be like this,

thnx
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16778908
> 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
 

Author Comment

by:rachelee
ID: 16778917
no i want in java, atleast if i can do changes in same or not?

He told , do it without using super
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16778922
I told you above, just remove super()
0
 

Author Comment

by:rachelee
ID: 16778925
ok, and all rest , i have to keep as it is or???
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16778926
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
 
LVL 1

Expert Comment

by:GTwoD
ID: 16779019

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
 
LVL 92

Accepted Solution

by:
objects earned 900 total points
ID: 16779064
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
 
LVL 6

Expert Comment

by:phuocnh
ID: 16779202
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16788794
:-)

>>i want other shortened way

Why then did you accept a much *longer* way of setting the attributes as the 'answer'? ;-)
0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16788858
An anwser of genius!
:-)
Phuoc
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question