0% found this document useful (0 votes)
10 views2 pages

javaTask5

The document contains Java code for a 'Main' class that creates and compares 'Student' objects based on their student numbers. It includes methods for equality checking, hash code generation, and a custom string representation that hides part of the student number. The 'Student' class encapsulates student details such as name, faculty, and age, and overrides the equals, hashCode, and toString methods.

Uploaded by

ilikemandalin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

javaTask5

The document contains Java code for a 'Main' class that creates and compares 'Student' objects based on their student numbers. It includes methods for equality checking, hash code generation, and a custom string representation that hides part of the student number. The 'Student' class encapsulates student details such as name, faculty, and age, and overrides the equals, hashCode, and toString methods.

Uploaded by

ilikemandalin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Main.

java
public class Main {
public static void main(String[] args) {
Student s1 = new Student("182342356", "Agatha", "Engineering", 18);
Student s2 = new Student("182342356", "Kao", "Engineering", 19);
Student s3 = new Student("182342359", "Mikael", "Music", 20);

System.out.println(s1);
System.out.println(s2);

if(s1.equals(s2)) {
System.out.println("the student numbers of s1 and s2 are equal");
} else {
System.out.println("the student numbers of s1 and s2 are not equal");
}

System.out.println("s1 hashCode: " + s1.hashCode());


System.out.println("s2 hashCode: " + s2.hashCode());
System.out.println("s3 hashCode: " + s3.hashCode());
}
}

//Student.java
import java.util.Objects;

public class Student {


private String studentNumber;
private String name;
private String faculty;
private int age;

public Student(String studentNumber, String name, String faculty, int age) {


this.studentNumber = studentNumber;
this.name = name;
this.faculty = faculty;
this.age = age;
}

public String getStudentNumber() {


return studentNumber;
}
public String getName() {
return name;
}
public String faculty() {
return faculty;
}
public int getAge() {
return age;
}

@Override
public boolean equals(Object obj) {
Student s = (Student) obj;
return studentNumber.equals(s.studentNumber);
}
@Override
public int hashCode() {
return Objects.hash(studentNumber);
}
@Override
public String toString() {
String hiddenNum = studentNumber.substring(0, 2) + "******" +
studentNumber.charAt(studentNumber.length() - 1);
return "Student Number: " + hiddenNum;
}
}

You might also like