Create a class named Student which has the following fields: • id : The id field
ID: 3815525 • Letter: C
Question
Create a class named Student which has the following fields:
• id : The id field references a String object that holds the ID of the student.
• name : The name field references a String object that holds the name of the student.
• department : The department field references a String object that holds the name of the department.
Your class should contain appropriate accessor and mutator methods for all the fields. The three instance variables should be private and the only way to access and change these variables is through the accessor and mutator methods.
The class should also have the following constructors and methods:
• public Student(String name)
The constructor that initializes instance variable name using the parameter.
• public Student(String id, String name, String department)
The constructor that initializes all the instance variables using the parameters.
• public boolean equals(Student anotherStudent)
The method returns true if the two students have the same id, name, and department. It will return false, otherwise.
• public String toString()
The method returns the String representation of the student that includes the id, name, and department of the student. When this method is called, it returns a String like:
"ID: 00467789, Name: Bobby , Department: Computer Science".
Now, write a JUnit test to test the Student class and its methods. You can create a JUnit test using the following step: 1) Right Click on the source package. 2) Select New à JUnit Test Case. 3) Provide a name for the Test Case.
Name your first Java class Student and second Java class TestStudent.
Explanation / Answer
Below is the Student Class:
public class Student {
private String id;
private String name;
private String department;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Student(String name) {
super();
this.name = name;
}
public Student(String id, String name, String department) {
super();
this.id = id;
this.name = name;
this.department = department;
}
public boolean equals(Student anotherStudent) {
boolean matched = false;
if( this.getId().equals(anotherStudent.getDepartment()) &&
this.getId().equals(anotherStudent.getId()) &&
this.getName().equals(anotherStudent.getName())){
matched = true;
}else{
matched = true;
}
return matched;
}
@Override
public String toString() {
return "Student [id= 00467789 , name= Bobby , department= Computer Science]";
}
}
Below is the tester Class for Student Class:
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class TestStudent {
private Student tester;
@Before
public void setUp() throws Exception
{
//init student objects
tester = new Student("00467789", "Bobby ", "Computer Science");
}
@Test
public void testGetterSetter() {
tester.setDepartment("Electronics");
assertEquals("error in getter/setter", "Electronics", tester.getDepartment());
}
}
You can add other test methods for other getter/setter methods.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.