• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Missing set and get items

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all! So my code is listed below but apparently I am missing the following :


Missing testing of the set methods without errors: -2

Missing testing of the set methods for errors: -2

Missing testing of constructor for errors: -1




****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.








*** Rectangle Test  ****



***Rectangle ****


 
Rancher
Posts: 4315
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to test each part of the requirements in yoiur test class:

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.


I only see one use of the constructor, which is for the "happy path" (ie no exception).
I only see one use of one setter, which ought to throw an error (I can't quite tell from the requirements how you are supposed to show an error, are you supposed to throw an exception?).
I see no other method calls.
 
Greenhorn
Posts: 6
3
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your assignment asks you to thoroughly test all of your methods. I assume that you should at least call every method with both valid and invalid values.

In your code you only call the constructor with valid values; the -1 is because you did not test that it handles invalid values correctly. You did call setHeight() once, testing it with an invalid value, but did not test that it does work for valid values. You did neither for setWidth(). So -2 for not testing both set methods with valid values, and -2 for not testing them with invalid values.
 
Marshal
Posts: 14337
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marr Yenn wrote:*** Rectangle Test  ****


Is this really how you were instructed to write tests for your code? Are you sure you're not supposed to use something like JUnit?

If this is indeed how you're expected to write tests, how are you going to know when your code passes or fails the "tests" you have there? A test should either pass, fail, or cause an exception to occur that you didn't account for. I don't see your "test code" doing any of that. All it does really is exercise some parts of the main code (we call it "production code") but just executing code is not testing. You have to check whether something you expected to happen actually did.
 
Marr Yenn
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I handle the invaild values ?
 
Dave Tolls
Rancher
Posts: 4315
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, your requirements state that the setter and constructor need to check the values are valid and
" If they are not, indicate an exception has occurred. "

Now, that says to me "throw an exception", but that all depends on what you've been taught.
 
Junilu Lacar
Marshal
Posts: 14337
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:... check the values are valid and " If they are not, indicate an exception has occurred. "

Now, that says to me "throw an exception", but that all depends on what you've been taught.


Not unreasonable. I think the problem statement is poorly worded at best. "Indicate an exception" could certainly mean "throw an exception" but why not say "throw an exception" then? That would be less ambiguous. It seems that OP thinks "indicate an exception" simply means to "display an error message" based on the comments and the System.out.println statements scattered all over the code. Maybe that's what they were taught to do or maybe not.
 
A berm makes a great wind break. And Iwe all like to break wind once in a while. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!