QuestionQuestion

Exceptional Students

1. Create classes and interfaces as specified in the UML. The enclosing box indicates the package. (Exception is not in your package.)
2. For the purposes of this lab, students are just numbers. (Ha!) (Except see the next requirement.)
3. Student number is a String, because we’re not doing math with it. It may be either null, or a 7 digit number (first digit may not be zero.)
4. Student age must be non-negative.
5. Each exception class should extend Exception, and have two constructors: a no-arg constructor with a general message (e.g. “Invalid Student Number”) and a one arg constructor with a value to use in a more specific message (e.g. “Invalid Student age: -3”.) Call the superclass constructor in each.
6. Make your Student constructors call the setters, passing along the appropriate parameter values. This is a good practice, because it eliminates duplicate error-checking code. The no-arg constructor should explicitly do what it would do normally: attributes of reference types are set to null, booleans to false, all other primitive types are set to 0.
7. The setters check their parameter values, and throw exceptions as necessary. Throw the specific message version of the exception constructors. This might be a good time to use regular expressions!
8. No matter what their parameters, the setters should not throw NullPointerException. (In general you should guard against generating NullPointerException when methods take object parameters. Check for null objects and Do The Right Thing.)
9. In the no-arg constructor, catch and ignore the two exceptions. (You assume that you know what you’re doing, i.e. the values you are assigning are valid.) The other constructor should re-throw any exceptions generated in the setters.
10. Create an additional class named StudentMain with a main method that asks for a student number and student age and then prints info about the student. Print a message and repeat both questions if either answer is invalid. See the sample run.

Sample run
User input shown in red bold.
Enter student ID: 123456
Enter student age: 22
Invalid student number: 123456
Enter student ID: 1234567
Enter student age: -9
Invalid student age: -9
Enter student ID: 123456p
Enter student age: 2
Invalid student number: 123456p
Enter student ID: 0123456
Enter student age: 22
Invalid student number: 0123456
Enter student ID: 12345678
Enter student age: 22
Invalid student number: 12345678
Enter student ID: 123
Enter student age: -9
Invalid student number: 123
Enter student ID: 1234567
Enter student age: 22
Student 1234567 is 22 years old

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
*/
public class Student {

    private String studentID;
    private int studentAge;

    /**
    * The no-arg constructor should ​explicitly ​do what it would do normally:
    * <br>
    * attributes of reference types are set to null,booleans to false, <br>
    * all other primitive types are set to 0.
    *
    */
    public Student() {

       /**
         * In the no-arg constructor,catch and ignore the two exceptions. (You
         * assumethat you know what you’re doing, i.e. the values you are
         * assigning are valid.
         */
       try {
            this.setStudentAge(0);
            this.setStudentID(null);
       } catch (InvalidStudentAgeException | InvalidStudentIDException e) {
       }
    }

    /**
    * two arg constructor with a value
    *
    * @param studentID id of the student
    * @param studentAge age of the student
    * @throws InvalidStudentAgeException exception of age
    * @throws InvalidStudentIDException exception of ID
    */
    public Student(String studentID, int studentAge)
            throws InvalidStudentAgeException, InvalidStudentIDException {
       /**
         * Make your ​Student ​constructors call the setters, The other
         * constructor should re-throw any exceptions generated in the setters
         */
       this.setStudentAge(studentAge);
       this.setStudentID...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

50% discount

Hours
Minutes
Seconds
$25.00 $12.50
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats