aakash2310

Untitled

Mar 13th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class P4 {
  4. String name;
  5. int marks;
  6.  
  7. public Student(String name, int marks) {
  8. this.name = name;
  9. this.marks = marks;
  10. }
  11.  
  12. public boolean equals(Object s) {
  13. if (this == s) return true;
  14. if (s == null || getClass() != s.getClass()) return false;
  15. Student student = (Student) s;
  16. return Objects.equals(name, student.name);
  17. }
  18.  
  19. public int hashCode() {
  20. return Objects.hash(name);
  21. }
  22.  
  23. public String toString() {
  24. return "Student{name='" + name + "', marks=" + marks + "}";
  25. }
  26.  
  27. public static void main(String[] args) {
  28. Student s1 = new Student("Alice", 85);
  29. Student s2 = new Student("Bob", 90);
  30. Student s3 = new Student("Alice", 85);
  31.  
  32. System.out.println("s1.equals(s2): " + s1.equals(s2));
  33. System.out.println("s1.equals(s3): " + s1.equals(s3));
  34. System.out.println("s1.hashCode(): " + s1.hashCode());
  35. System.out.println("s2.hashCode(): " + s2.hashCode());
  36. System.out.println("s3.hashCode(): " + s3.hashCode());
  37. System.out.println("s1.toString(): " + s1.toString());
  38. System.out.println("s2.toString(): " + s2.toString());
  39. System.out.println("s3.toString(): " + s3.toString());
  40. }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment