Answer to Question #284872 in Java | JSP | JSF for Jade

Question #284872

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".





1
Expert's answer
2022-01-05T01:40:29-0500
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");
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog