Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSE114 Spring 2016
Lab Exercise 2 of Week 15
Implementing Exceptions
Chen-Wei Wang
Problem
A person has a first name, a weight (in kilograms), and a height (in meters). A person collector has a
collection of persons, and may either: 1) add a person via their first name, weight, and height; or 2)
return the BMI ( weightheight2 ) of the person identified by a name. It is considered as an error if you attempt
to add a person whose first name already exists in the collector. Symmetrically, it is considered as an
error if you attempt to return the BMI of a person whose name does not exist in the collector.
Your Tasks
1. Translate the above problem statements into Java classes.
2. Implement the two kinds of errors as Java classes that extend Exception. Make sure that objects
of these exception classes are thrown when appropriate (i.e., when adding a person to the collector
and when returning the BMI of a person in the collector).
3. Define the PersonCollectorTester class with a main method that interacts with the user as
follows:
3.1 Initialize an empty person collector.
3.2 Prompt the user for a command: addPerson, getBMI, or exit. You can assume that the user
will always type a valid command.
3.3 If the user types addPerson, prompt the user further for the person’s name, weight, and height,
one at a time.
3.3.1 Then, add the person to the person collector if the name does not exist, by calling the
appropriate method in the person collector class.
3.4 If the user types getBMI, prompt the user further for the person’s name.
3.4.1 Then, return that person’s BMI if the name exists, by calling the appropriate method in
the person collector class.
3.5 If the user types exit, terminate the interaction and print Bye!.
4. When you first write 3.3.1 and 3.4.1, they would not even compile, why? How do you fix them?
Hint: Refer to the catch-or-specify requirement for checked exceptions.
5. When any of the two kinds of errors occurs and an exception object is thrown, implement your main
method such that it will just go back to Step 3.2 and prompt the user to enter a new command
again.
1