• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Java Illegal Argument Exception

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey ya'll so I was un sure how to edit and add this to the post so I made a completely new one!


****Here is the assignment ****
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods.

Details:
Create a class called Rectangle containing the following:
Two instance variables,
An instance variable of type double used to hold the rectangle’s width.
An instance variable of type double used to hold the rectangle’s height.
Provide a constructor with two parameters used to initializes each instance variable. The constructor should verify that the specified width and height values are greater than 0.0 and less than or equal to 20.0. If they are not, indicate an exception has occurred.
Provide get methods that return the values of each instance variables.
Provide set methods that set the instance variables to new values. The methods should also verify that the specified width and height values are greater than 0.0 and less than or equal to 20.0. If they are not, indicate an exception has occurred.
Provide a method called calculatePerimeter that calculates the perimeter of the rectangle and return that value as a double.
Provide a method called calculateArea that calculates the area of the rectangle and returns that value as a double.

Create a second class called RectangleTest that contains the main method, and thoroughly tests the Rectangle class’s methods. This test class does not need to ask users for input. Just create the needed Rectangle objects to ensure that you test the Rectangle class’s methods well. The thoroughness of your testing in will impact your grade.


Note: Ensure that your program is properly formatted and it follows all Java naming conventions.


*****we are suppose to take this old code and update it to this ****
Details:
Rewrite the Rectangle class you wrote for Chapter 8 programming assignment to throw an IllegalArgumentException indicating there is a problem with the input parameters in the constructor and the set methods.

Also, rewrite the RectangleTest class from Chapter 8 to handle the exceptions thrown by the Rectangle class. Thoroughly tests the Rectangle class’s methods. This test class does not need to ask users for input. Just create the needed Rectangle objects to ensure that you test the Rectangle class’s methods well. The thoroughness of your testing in will impact your grade.



*************Rectangle class**********************










********Rectangle Test ********


 
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is it, length or width? Hint, answer is in 'e'.
 
Marr Yenn
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The constructor and method should verify that the specified width and height values are greater than 0.0 and less than or equal to 20.0. So both ^_^ I believe thats what you're asking ?
 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are catching this exception. The exception comes with some information. You are throwing it away and writing a new (incomplete) error message.
 
Marr Yenn
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would be the correct way to close it ?
 
Carey Brown
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not talking about closing, I'm pointing out that the error message you present the user could be more specific. You  already have a better error message in 'e' but you're not using it.

See the documentation for an Exception constructor.
 
Saloon Keeper
Posts: 14501
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't catch unchecked exceptions like that.

Check that the user entered valid values using a classical if-statement. To avoid code duplication, add static methods to your Rectangle class that return whether a given width or height are valid.

IllegalArgumentException is for when you, the programmer, did something wrong.
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic