-4

I have an array of a class type which stores videos (not real ones just objects). Each video has a title and an author. The videos have their data set using set methods like these:

 public class Clip {

 private String title;
 private String author;

     public void setTitle (String s1) {

        title = s1;

    }

     public void setAuthor (String s2) {

        title = s2;

    }

I then have a different class which creates the array and uses these set methods to enter data. The program then asks the user to select an element from the array, for example the user selects element [3]. Now the program must print the title and author of that element(video).

This is the part I am not sure how to do? Can I please have some help?

7
  • You are creating new strings in setters. Please don't do that. That should be this.title = s1; Commented Oct 18, 2013 at 10:43
  • what's the issue? Search for override toString(). Commented Oct 18, 2013 at 10:44
  • can you please show an example? Commented Oct 18, 2013 at 10:45
  • 2
    This is exactly the same question as stackoverflow.com/questions/19446929/… just with a different example Commented Oct 18, 2013 at 10:47
  • 1
    @Suresh atta - How is he creating new strings? What dou you mean by that. He's passing a string reference and then assign it to the private field. There is no String constructor included, so basically it's all about references. Commented Oct 18, 2013 at 10:55

8 Answers 8

2

Override toString() then simply call

System.out.println(element[3])

If you need help creating the toString method eclipse can show you how it's done.

Press Alt+Shift+S then S , or right click -> Source -> generate toString()

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, thats what I thought too, seemingly that doesn't answer the OP's question
1

Your implementation of Clip class is wrong. It should be something like this:

public class Clip {
        private String title;
        private String author;

        public Clip(String title, String author) {
            this.title = title;
            this.author = author;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }
    }

And you can retrieve your objects from array like this:

@Test
    public void testClipArray() throws Exception {
        // Lets assume our array contains 2 elements
        Clip[] clipArray = new Clip[2];
        clipArray[0] = new Clip("First", "Clip");
        clipArray[1] = new Clip("Second", "Clip");

        // Lets retrieve 2nd object (with index: 1)
        Clip secondObject = clipArray[1];

        System.out.println(secondObject.getAuthor());
        System.out.println(secondObject.getTitle());
    }

Comments

0

Basically you have two options:

  1. Create getters in the Video-Class, e.g.public String getTitle() { return title; }
  2. Override the toString function to output the value you want, e.g.

    @Override public String toString() { return "Author: " + author + ", Title: " + title"; }

Comments

0
public class Clip {

private String title;
private String author;

 public void setTitle (String s1) {

    title = s1;

}

 public void setAuthor (String s2) {

    author = s2;

}

@Override
public String toString(){
    return author + ": " + title;
}

Comments

0

Override toString method of Clip. Every invocation of print* (e.g. println) method would call toString method of an object.

    @Override
    public String toString() {
        return "[Title: " + this.title +", Author: " + this.author + "]";
    }

Example after overriding method:

    Clip clip1 = new Clip();
    Clip clip2 = new Clip();
    Clip[] clipArray = {clip1, clip2};

    for (Clip clip : clipArray) {
        System.out.println(clip);
    }

Comments

0

Please override toString () of Clip class as below

@Override
public String toString() {
     return "Title:"+ this.s1 + "  Author:" + this.s2;
}


Then print the Title and Author by using the below code.
System.out.println(objectClip.toString());

Comments

0

Override toString() method of Clip class. For example,

public class Clip {

    private String title;
    private String author;

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

    public void setAuthor (String s2) {
        title = s2;
    }

    @Override
    public String toString() {
        return title + " by " + author;
    }
}

Then print the array of clips as follows:

for (Clip aClip : arrayOfClips) {
    System.out.println(aClip);
}

Comments

0

Override toString() method in Clip class as follows,

 @override
   public String toString(){ 

    return "Title: "+ title +"  ,author"+author;

   }

and invocation code invoke following statement,

System.out.println(element[3]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.