Spaces:
Running
Running
return <ur printout>
Browse files
Week 5: Class hierarchies/12B. Add a method toString
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
In the program, a class named 'Movie' is defined.
|
| 2 |
+
Add a toString method to the class that returns the movie's details as a string in the following format:
|
| 3 |
+
|
| 4 |
+
MovieName (Director's Name), duration min.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
For example:
|
| 8 |
+
public static void main(String[] args) {
|
| 9 |
+
Movie m = new Movie("Java and Me", "James Java", 93);
|
| 10 |
+
System.out.println(m);
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
The program prints:
|
| 14 |
+
James Java (Java and Me), 93 min.
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
import java.util.Random;
|
| 20 |
+
|
| 21 |
+
public class Test{
|
| 22 |
+
public static void main(String[] args){
|
| 23 |
+
final Random r = new Random();
|
| 24 |
+
|
| 25 |
+
System.out.println("Creating movie object with parameters");
|
| 26 |
+
System.out.println("(Senior Spielbergo, A.T., 111");
|
| 27 |
+
Movie m = new Movie("Senior Spielbergo", "A.T.", 111);
|
| 28 |
+
System.out.println("Printout: " + m);
|
| 29 |
+
System.out.println("");
|
| 30 |
+
|
| 31 |
+
System.out.println("Creating movie object with parameters");
|
| 32 |
+
System.out.println("(George Lucas, Jonas of Impivaara, 142");
|
| 33 |
+
m = new Movie("George Lucas", "Jonas of Impivaara", 142);
|
| 34 |
+
System.out.println("Printout: " + m);
|
| 35 |
+
System.out.println("");
|
| 36 |
+
|
| 37 |
+
System.out.println("Creating movie object with parameters");
|
| 38 |
+
System.out.println("(Speed Johnson, Uno in Armenia, 93");
|
| 39 |
+
m = new Movie("Speed Johnson", "Uno in Armenia", 93);
|
| 40 |
+
System.out.println("Printout: " + m);
|
| 41 |
+
System.out.println("");
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
class Movie {
|
| 46 |
+
private String director;
|
| 47 |
+
private String name;
|
| 48 |
+
int duration;
|
| 49 |
+
|
| 50 |
+
public Movie(String director, String name, int duration) {
|
| 51 |
+
this.director = director;
|
| 52 |
+
this.name = name;
|
| 53 |
+
this.duration = duration;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
//MovieName (Director's Name), duration min.
|
| 58 |
+
//ADD
|
| 59 |
+
@Override
|
| 60 |
+
public String toString() {
|
| 61 |
+
return this.name + " (" + this.director + "), " + this.duration + " min.";
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
Creating movie object with parameters
|
| 71 |
+
(Senior Spielbergo, A.T., 111
|
| 72 |
+
Printout: A.T. (Senior Spielbergo), 111 min.
|
| 73 |
+
|
| 74 |
+
Creating movie object with parameters
|
| 75 |
+
(George Lucas, Jonas of Impivaara, 142
|
| 76 |
+
Printout: Jonas of Impivaara (George Lucas), 142 min.
|
| 77 |
+
|
| 78 |
+
Creating movie object with parameters
|
| 79 |
+
(Speed Johnson, Uno in Armenia, 93
|
| 80 |
+
Printout: Uno in Armenia (Speed Johnson), 93 min.
|
| 81 |
+
|
| 82 |
+
|