KaiquanMah commited on
Commit
90add41
·
verified ·
1 Parent(s): cc4c404

chk same obj, not null, not expected class, values

Browse files
Week 5: Class hierarchies/13B. Equals method for class Movie [4 basic chks] ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Class 'Movie' is defined in the following program.
2
+ Write the 'equals' method to the class using the examples in this page.
3
+
4
+ 2 movies are equal, if ALL ATTRIBUTES have 'equal values';
5
+ hence, the name, director and length should be equal.
6
+
7
+
8
+
9
+
10
+ import java.util.Random;
11
+ import java.util.ArrayList;
12
+ import java.util.Arrays;
13
+ import java.util.Collections;
14
+
15
+ public class Test{
16
+ public static void main(String[] args){
17
+ final Random r = new Random();
18
+
19
+ Movie m = new Movie("Java and Me", "James Javason", 93);
20
+ System.out.println("Movie: " + m);
21
+
22
+ Movie m2 = new Movie("Java and Me", "Jane Javander", 93);
23
+ Movie m3 = new Movie("Java and Me", "James Javason", 94);
24
+ Movie m4 = new Movie("Python and me", "James Javason", 93);
25
+ Movie m5 = new Movie("Java and Me", "James Javason", 93);
26
+
27
+ ArrayList<Movie> movies = new ArrayList<>(Arrays.asList(new Movie[] {m2,m3,m4,m5,m,null}));
28
+ Collections.shuffle(movies, r);
29
+
30
+ movies.stream().forEach(mo -> compare(m, mo));
31
+
32
+ System.out.println("Comparing to String \"Java and Me\"");
33
+ System.out.println("equals: " + (m.equals("Java and Me")));
34
+
35
+
36
+ }
37
+
38
+ public static void compare(Movie m, Movie m2) {
39
+ System.out.println("Comparing to movie " + m2);
40
+ System.out.println("equals: " + (m.equals(m2)));
41
+ }
42
+ }
43
+
44
+ class Movie {
45
+ private String director;
46
+ private String name;
47
+ int length;
48
+
49
+ public Movie(String director, String name, int length) {
50
+ this.director = director;
51
+ this.name = name;
52
+ this.length = length;
53
+ }
54
+
55
+ public String toString() {
56
+ return name + " (" + director + "), " + length + " min.";
57
+ }
58
+
59
+
60
+
61
+ @Override
62
+ public boolean equals(Object m2) {
63
+ // same object
64
+ if (this == m2) {
65
+ return true;
66
+ }
67
+
68
+ // not null
69
+ if (m2 == null) {
70
+ return false;
71
+ }
72
+
73
+ // not 'Movie' class
74
+ if (m2.getClass() != Movie.class) {
75
+ return false;
76
+ }
77
+
78
+
79
+ // convert into an object
80
+ Movie other = (Movie) m2;
81
+ // same values
82
+ if (this.director.equals(other.director) && this.name.equals(other.name) && this.length == other.length) {
83
+ return true;
84
+ }
85
+ else {
86
+ return false;
87
+ }
88
+ }
89
+
90
+
91
+
92
+
93
+
94
+ }
95
+
96
+
97
+
98
+
99
+
100
+
101
+ Movie: James Javason (Java and Me), 93 min.
102
+ Comparing to movie James Javason (Python and me), 93 min.
103
+ equals: false
104
+ Comparing to movie James Javason (Java and Me), 93 min.
105
+ equals: true
106
+ Comparing to movie Jane Javander (Java and Me), 93 min.
107
+ equals: false
108
+ Comparing to movie James Javason (Java and Me), 94 min.
109
+ equals: false
110
+ Comparing to movie null
111
+ equals: false
112
+ Comparing to movie James Javason (Java and Me), 93 min.
113
+ equals: true
114
+ Comparing to String "Java and Me"
115
+ equals: false
116
+