Bookmark Topic Watch Topic
  • New Topic

Class of Rectangle  RSS feed

 
David Tran
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Report post to moderator
Create a class Rectangle. The class has attributes length and width, each of which defaults to 1. It has methods that calculate the perimeter and the area of the rectangle. It has set and get methods for both length and width. The set methods should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program to test class Rectangle.
Name your testing class Assign8. Call JOptionPane.showInputDialog to get user input. Call JOptionPane.showMessageDialog to show the result or error.
Your class Rectangle is similar to Time1. Hint: Use Eclipse to generate get/set methods for the instance variables length and width.
In the two set methods throw IllegalArgumentExceltion if the input is beyond the valid range.
Add two more methods getPerimeter() and getArea(), in which call the get methods of length and width.
In Assign8 the main() method should keep prompting for input and show the result or error until the user clicks Cancel in the input dialog. Hint: Use while(true){...} loop and break it when JOptionPane.showInputDialog returns null.
You should catch IllegalArgumentExceltion and display error message. After that, keep prompting for input. Hint: put try/catch in the loop body.
When user enters a string other than a number also display error message. Hint: When the input is invalid, Double.parseDouble() will throw NumberFormatException which is a subclass of IllegalArgumentExceltion. To catch it, put the statements inside the try block. Your catch(IllegalArgumentExceltion e) will catch from both Rectangle and Double classes.
I'm get stuck about it. Can anyone help me Thanks!
Here is my code so far:

 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Report post to moderator
Welcome. Please indent code and use [CODE] tags when including code to make it readable.
Regarding why it doesn't work - re-read your assignment instructions for handling the cancel condition because looks like you haven't accurately followed what it says there.

 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Report post to moderator
Welcome to the Ranch David

As you have been suggested, please UseCodeTags (<- link) when you post actual code.
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please format your code properly and put it in code tags. It's really hard to read otherwise.

There's a lot for you to do on this assignment. What have you done already? What has you stuck? Try to break it down into little pieces and get one piece working at a time.
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Report post to moderator
David Tran,

Please do not create multiple threads in multiple forum categories. I have fixed that.
Also please UseCodeTags when you post your actual code. I have added them for you too.

As you see, there are still lots of code indentation and formatting issues, which I didn't manage to fix now.
You should be aware, that posting code in that way, does not people attract to look into it. People here are quite nitpicky on code indentation and formating, so, please try to improve your next posts.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Report post to moderator
Liutauras Vilda wrote:People here are quite nitpicky on code indentation and formating, so, please try to improve your next posts.


"Nitpicky" connotes being needlessly picky. I'd say that proper indentation and formatting is an important tool for revealing the structure of the code, and avoiding issues resulting from code that is improperly nested or structured. Urging people, especially greenhorns, to use proper indentation is as important as urging them to use proper naming conventions and other good practices that help make Java code robust and high quality.
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
And of course you're right, and here is a good example, how my wrongly chosen word "nitpicky" to describe the need of indenting code properly and choosing meaning variable names lead to a misunderstanding of my original intention, same as it can happen by choosing wrong variable name.
 
David Tran
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thank you for merging!
I already done the class Rectangle, but i still get stuck at RectangleTest class.
For example :
I don't know how to catch IllegalArgumentException and display Error message ( use try/ catch)
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Report post to moderator
David,
The try/catch goes this way:

note that in your assignment you have a specific Exception to use "IllegalArguementException". You can check the API to see what exceptions are thrown by specific methods you want to use, or a lot of the time your IDE will tell you.

David Tran wrote:Thank you for merging!
I already done the class Rectangle, but i still get stuck at RectangleTest class.
For example :
I don't know how to catch IllegalArgumentException and display Error message ( use try/ catch)
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Report post to moderator
Consider whether you need to catch that Exception at all.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Report post to moderator
Closing thread: will be reopened when problems with yesterday's post are sorted out.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!