Question: Java: Question 1: Question 2: Implement an immutable class called Animal with the following attributes: type (String value) and age (int value). Implement a custom
Java:
Question 1:
Question 2:

Implement an immutable class called Animal with the following attributes: type (String value) and age (int value). Implement a custom checked exception class for InvalidAgeException. This exception is thrown when there is an attempt to set an invalid age range for an instance of class Person. Below is the implementation of class Person that would use this exception. public class Person { private String firstName; private String lastName; private int age; // constructor: Person(String fn, String in, int age) { this.firstName = fn; this. lastName = ln; this.age = age; } // getters: public String getFirstName() {return this.firstName;} public String getLastName() {return this. lastName;} public int getAge() {return this.age;} // setters: public void setFirstName (String fn){this.firstName = fn;} public void setLastName (String In){this. lastName = ln;} public void setAge(int age) { if(age 120){ throw new InvalidAgeException ("The age is outside allowed range."); }else{ this. age = age; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
