Java - Sample Exercise of testing the objects..
Sample Exercise
Create an encapsulated class with 4 fields and the respective methods to access and edit those fields. Then go ahead and create a test class to verify.
- Class Name : Student
- Field Names : studentId, studentName, collegeName, address
- Test Class Name : TestStudent
package Oopdemo;
class Student {
private int studentId;
private String studentName;
private String collegeName;
private String address;
/**
* @return the studentId
*/
public int getStudentId() {
return studentId;
}
/**
* @param studentId the studentId to set
*/
public void setStudentId(int studentId) {
this.studentId = studentId;
}
/**
* @return the studentName
*/
public String getStudentName() {
return studentName;
}
/**
* @param studentName the studentName to set
*/
public void setStudentName(String studentName) {
this.studentName = studentName;
}
/**
* @return the collegeName
*/
public String getCollegeName() {
return collegeName;
}
/**
* @param collegeName the collegeName to set
*/
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
}
public class TestStudent {
public static void main(String[] args) {
Student s1=new Student();
s1.setStudentId(111);
s1.setStudentName("Salahuddin Ayyubi");
s1.setCollegeName("Royal College");
s1.setAddress("213 Straight Street Wonderland 12345 Earth ");
System.out.println( s1.getStudentId());
System.out.println(s1.getStudentName() );
System.out.println(s1.getCollegeName() );
System.out.println(s1.getAddress() );
}
}
Comments
Post a Comment