Creating Rectangle Class in Java

Problem:

Write 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 20.0. Include a predicate method isSquare which determines if the rectangle is a square.

Solution:

 public class sophiRectangle
{
 private float length, width;
 
 public sophiRectangle()
 {
  length = 1;
  width = 1;
 }
 
 public float doPerimeter()
 {
 return (length+width)*2;
 }

 
 public float doArea()
 {
 return length*width;
 }
 
 public void setValues(float length,float width)
 {
  if (length > 0 && length < 20)
   this.length = length;
  if (width > 0 && width < 20)
   this.width = width;
 }
 
 public float doLength()
 {
  return length;
 }
 
 public float doWidth()
 {
  return width;
 }
 
 public boolean isSquare()
 {
  if (length == width)
   return true;
   else 
    return false;
 }
 }


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact