In JAVA please Write a class Student that encapsulates the concept of a student
ID: 3688175 • Letter: I
Question
In JAVA please
Write a class Student that encapsulates the concept of a student where the attributes are:
studentID
student_first_Name
student_last_name
stdeunt_birth_date
Make sure that the class includes:
A default constructor with all fields set to null. An overridden constructor that allows first and last name inserted at once. Another overridden constructor that allows first, last names and birth date inserted once an object is created.
The class must have mutators and accessors to allow access or changes for the attribute values.
The class must have a toString method to print all of the student’s information
The class must have an equals method to compare to students.
Write a test application to test all of the methods of the object of type Student.
JAVA Thankyou
Explanation / Answer
Student.java
package org.students;
public class Student
{
//decalring instance varibles
private int studentID;
private String student_first_Name;
private String student_last_name;
private String student_birth_date;
//default constructor
public Student() {
studentID=0;
student_first_Name=null;
student_last_name=null;
student_birth_date=null;
}
//parameterized constructor
public Student(String student_first_Name, String student_last_name) {
super();
this.student_first_Name = student_first_Name;
this.student_last_name = student_last_name;
}
//parameterized constructor
public Student(String student_first_Name, String student_last_name,
String date) {
super();
this.student_first_Name = student_first_Name;
this.student_last_name = student_last_name;
this.student_birth_date = date;
}
//Accessors and Mutator methods
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public String getStudent_first_Name() {
return student_first_Name;
}
public void setStudent_first_Name(String student_first_Name) {
this.student_first_Name = student_first_Name;
}
public String getStudent_last_name() {
return student_last_name;
}
public void setStudent_last_name(String student_last_name) {
this.student_last_name = student_last_name;
}
public String getStudent_birth_date() {
return student_birth_date;
}
public void setStudent_birth_date(String student_birth_date) {
this.student_birth_date = student_birth_date;
}
//toString() method.
@Override
public String toString() {
return "Student [studentID=" + studentID + ", student_first_Name="
+ student_first_Name + ", student_last_name=" + student_last_name
+ ", student_birth_date=" + student_birth_date + "]";
}
//Equals method.
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (studentID != other.studentID)
return false;
if (student_birth_date == null) {
if (other.student_birth_date != null)
return false;
} else if (!student_birth_date.equals(other.student_birth_date))
return false;
if (student_first_Name == null) {
if (other.student_first_Name != null)
return false;
} else if (!student_first_Name.equals(other.student_first_Name))
return false;
if (student_last_name == null) {
if (other.student_last_name != null)
return false;
} else if (!student_last_name.equals(other.student_last_name))
return false;
return true;
}
}
_________________________________________________________________________________________
Test.java
package org.students;
public class Test {
public static void main(String[] args)
{
//Creating Student Object
Student st1=new Student("kathey","williams");
//Creating Student Object
Student st2=new Student("tony","richard","12/05/1984");
//Displaying the contents of Student Object referenced by st1.
System.out.println(st1.toString());
//Displaying the contents of Student Object referenced by st2.
System.out.println(st2.toString());
//comparing the two Students objects st1 and st2.
System.out.println(st1.equals(st2));
}
}
_______________________________________________________________________________________
output:
Student [studentID=0, student_first_Name=kathey, student_last_name=williams, student_birth_date=null]
Student [studentID=0, student_first_Name=tony, student_last_name=richard, student_birth_date=12/05/1984]
false
_______________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.