question archive A Student object should validate its own data
Subject:Computer SciencePrice:2.89 Bought3
A Student object should validate its own data. The client runs this method, called
validateData(), with a Student object, as follows:
String result = student.validateData();
if (result == null)
else
System.out.println(result);
If the student's data are valid, the method returns the value null; otherwise, the method returns a string representing an error message that describes the error in the data. The client can then examine this result and take the appropriate action.
A student's name is invalid if it is an empty string. A student's test score is invalid if it lies outside the range from 0 to 100. Thus, sample error messages might be
"SORRY: name required" and
"SORRY: must have 0 <= test score <= 100".
Implement and test this method.
public class Student {
String name = " ";
int score = -1;
public Student()
{
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String validateData()
{
if(name.equals(" ") || name.isEmpty())
{
System.out.println("SORRY name required");
if(score score > 100)
{
System.out.println("SORRY score must be 0 );
return null;
}
return null;
}
return ("name " + name + " Score: "+score);
}
}
public class Test {
public static void main(String[] args) {
Student objstd = new Student();
String result = objstd.validateData();
System.out.println(result);
Student objstd1 = new Student();
objstd1.setName("Ali");
objstd1.setScore(70);
result = objstd1.validateData();
System.out.println(result);}}