CSC1110 - Week 13 Exercise
Overview
In this exercise, you will gain practice with using Exceptions in different contexts.
Circle
Create a Circle class that contains the following:
- A single instance attribute called circumference.
- A single argument constructor that takes in a circumference. If the circumference is non-positive, this method should throw and IllegalArgumentException.
- A toString() method that prints out "Circle" and the circle's circumference. See the sample output.
Homework13
This driver class must contain the following methods:
- makeCircle
- This method takes in a radius as a double and creates and returns a new Circle.
- fillList
- This method takes in an ArrayList of Circle objects, a Scanner for user input, and an int for the number of tries to create and add a Circle to the list.
- If the number of tries is non-positive, this method should throw an IllegalArgumentException.
- This method should ask the user for the radius of a Circle, create a new Circle with the makeCircle() method,
and then add that new Circle to the passed-in list. If there is an error anywhere in this process, you should print
an error message to the user and move onto making the next Circle if any.
- You should be using catches for InputMismatchException (or NumberFormatException if you are using next() and Integer.parseInt()) and IllegalArgumentException to detect the errors.
- main()
- This method creates the Scanner used for the user input and an empty ArrayList of Circle objects.
- It then asks the user for a non-negative number for the number of Circles to create and uses the
fillList() method to fill the ArrayList of Circles. It then prints out the ArrayList of Circles.
- If there is an error with the user's input of a whole number or an error is thrown by fillList(), print a descriptive warning message to the user.
- You should be detecting errors through the catching of the relevant Exceptions.
Sample Output
Example 1
Enter a non-negative number for how many times you want to make a Circle. 3 Enter the radius for the Circle. 1 Enter the radius for the Circle. 2 Enter the radius for the Circle. 3 Circles entered: [Circle{circumference=6.28}, Circle{circumference=12.57}, Circle{circumference=18.85}]
Example 2
Enter a non-negative number for how many times you want to make a Circle. 3 Enter the radius for the Circle. no Error radius must be a valid double. Skipping this input. Enter the radius for the Circle. -1 Error radius must be positive. Skipping this input. Enter the radius for the Circle. 3 Circles entered: [Circle{circumference=18.85}]
Example 3
Enter a non-negative number for how many times you want to make a Circle. no Error, number of Circles must be a whole number.
Example 4
Enter a non-negative number for how many times you want to make a Circle. -1 Error, number of Circles must not be negative number.
Submission Instructions
Commit and push your code to your repository.