Week 12 Exercise
Overview
In this exercise, you will gain practice with using Exceptions in different contexts.
Resources
- Accept the GitHub Classroom assignment invitation in Canvas and then, in IntelliJ, create a new project from Version Control using the repository URL.
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 anIllegalArgumentException. - A
toString()method that prints out "Circle" and the circle's circumference. See the sample output.
Exercise12
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.
- This method takes in a radius as a double and creates and returns a new
fillList- This method takes in an
ArrayListofCircleobjects, aScannerfor user input, and anintfor the number of tries to create and add aCircleto 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
Circlewith themakeCircle()method, and then add that newCircleto 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 nextCircleif any.- You should be using catches for
InputMismatchException(orNumberFormatExceptionif you are usingnext()andInteger.parseInt()andIllegalArgumentExceptionto detect the errors.
- You should be using catches for
- This method takes in an
main- This method creates the
Scannerused for the user input and an emptyArrayListofCirclereferences calledcircles. - It then asks the user for a non-negative number for the number of circles to create and uses the
fillList()method to fill theArrayListofCircles. It then prints out theArrayListofCircles.- 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.
- If there is an error with the user's input of a whole number or an error is thrown by
- This method creates the
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.