Create a class Student with the following attributes: name, age and grade.
1. Define a method called inputName(Stringname)that verifies the validity of the name passed as a parameter. The name must contain between 4 to 10 alphabets otherwise the exception IllegalArgumentException that displays the message “The name must contain between 4 to 10 alphabets” is generated.
2. Define a method called inputAge(int age) that verifies the validity of the age passed as a parameter. The age must be between 18 and 26 otherwise the exception IllegalArgumentException that displays the message "The age must be between 18 and 30" is generated.
3. Define a method called inputGrade(double grade) that verifies the validity of the grade passed as a parameter. The grade must be between 0 and 20 otherwise the exception IllegalArgumentException is generated which displays the message "The grade must be between 0 and 20".
public class Student {
private String name;
private int age;
private double grade;
public void inputName(String name) {
if (name.length() < 4 || name.length() > 10) {
throw new IllegalArgumentException("The name must contain between 4 to 10 alphabets");
}
for (int i = 0; i < name.length(); i++) {
if (!Character.isAlphabetic(name.charAt(i))) {
throw new IllegalArgumentException("The name must contain between 4 to 10 alphabets");
}
}
}
public void inputAge(int age) {
if (age < 18 || age > 26) {
throw new IllegalArgumentException("The age must be between 18 and 30");
}
}
public void inputGrade(double grade) {
if (grade < 0 || grade > 20) {
throw new IllegalArgumentException("The grade must be between 0 and 20");
}
}
}
Comments
Leave a comment