Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class P4 {
- String name;
- int marks;
- public Student(String name, int marks) {
- this.name = name;
- this.marks = marks;
- }
- public boolean equals(Object s) {
- if (this == s) return true;
- if (s == null || getClass() != s.getClass()) return false;
- Student student = (Student) s;
- return Objects.equals(name, student.name);
- }
- public int hashCode() {
- return Objects.hash(name);
- }
- public String toString() {
- return "Student{name='" + name + "', marks=" + marks + "}";
- }
- public static void main(String[] args) {
- Student s1 = new Student("Alice", 85);
- Student s2 = new Student("Bob", 90);
- Student s3 = new Student("Alice", 85);
- System.out.println("s1.equals(s2): " + s1.equals(s2));
- System.out.println("s1.equals(s3): " + s1.equals(s3));
- System.out.println("s1.hashCode(): " + s1.hashCode());
- System.out.println("s2.hashCode(): " + s2.hashCode());
- System.out.println("s3.hashCode(): " + s3.hashCode());
- System.out.println("s1.toString(): " + s1.toString());
- System.out.println("s2.toString(): " + s2.toString());
- System.out.println("s3.toString(): " + s3.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment