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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class of Rectangle

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Consider whether you need to catch that Exception at all.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Closing thread: will be reopened when problems with yesterday's post are sorted out.
 
    Bookmark Topic Watch Topic
  • New Topic