Question: Problem 1: A class that needs your help 8 points total; individual-only Consider the following class, which is intended to serve as a blueprint for



Problem 1: A class that needs your help 8 points total; individual-only Consider the following class, which is intended to serve as a blueprint for objects that encapsulate two pieces of data: a person's name (which should not be allowed to have the special value null) and the person's age (which must be non-negative). public class Participant { String name; int age; } 1. (5 points) This class does not employ appropriate encapsulation. Rewrite it to prevent direct access to the internals of a Participant object while allowing indirect access through appropriate methods. Your rewritten version should include: whatever changes are needed to the field declarations to prevent them from being accessed outside the class methods that can be used to obtain the value of each field (call them getName and getAge) methods that can be used to change the value of each field (call them setName and setAge). These methods should ensure that age is always non-negative, and that name is not null. Attempts to assign an invalid value should produce an IllegalArgumentException. a constructor that initializes the fields of a newly created Participant object to the values specified by the parameters of the constructor. Attempts to assign an invalid value should produce an IllegalArgumentException. Take advantage of the error- checking code that is already present in the methods that you defined for changing the values of the fields. No other methods are required. Problem 1: A class that needs your help 8 points total; individual-only Consider the following class, which is intended to serve as a blueprint for objects that encapsulate two pieces of data: a person's name (which should not be allowed to have the special value null) and the person's age (which must be non-negative). public class Participant { String name; int age; } 1. (5 points) This class does not employ appropriate encapsulation. Rewrite it to prevent direct access to the internals of a Participant object while allowing indirect access through appropriate methods. Your rewritten version should include: whatever changes are needed to the field declarations to prevent them from being accessed outside the class methods that can be used to obtain the value of each field (call them getName and getAge) methods that can be used to change the value of each field (call them setName and setAge). These methods should ensure that age is always non-negative, and that name is not null. Attempts to assign an invalid value should produce an IllegalArgumentException. a constructor that initializes the fields of a newly created Participant object to the values specified by the parameters of the constructor. Attempts to assign an invalid value should produce an IllegalArgumentException. Take advantage of the error- checking code that is already present in the methods that you defined for changing the values of the fields. No other methods are required. 2. (3 points) Now imagine that you're writing client code for your revised Participant class - i.e., code in a different class that uses Participant objects. For each of the following tasks, write a single line of client code to accomplish the task: a. Construct a Participant object for a person named Tom Brady who is 40 years old, and assign it to a properly declared variable named qb. b. The Tom Brady (the one who just won his seventh Super Bowl) is actually 43 years old! Change the age in the Participant object that you created in part (a). You should not create a new object; you should change the internals of the existing object. Important: As a result of your changes from part 1, clients no longer have direct access to the fields in a Participant object. As a result, you will need to make an appropriate method call to accomplish the change to the age. c. Get the name from the Participant object that you created in part (a), and assign it to a properly declared variable named mvp. Here again, you will need to make an appropriate method call
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
