This code provides the implementation of a Rectangle class in Java. It includes methods to calculate the area and perimeter of a rectangle, as well as check if it is a square. The code also includes a test class to demonstrate the functionality of the Rectangle class.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
Code Below is the code for the required classes. Simple Rectangle Class importjavax.swing.JOptionPane; /* Initial Rectangle Class */ publicclassRectangle { privatefloatlength, width; // define the variables for the dimensions publicRectangle () { length = 1; //* Variable initialised to 1 width =1; } publicfloatarea () // calculate the area { floatarea = (length * width); returnarea; /* JOptionPane.showMessageDialog(null, "Area of the Rectangle is "+ area);*/ //* get the area of the rectangle } publicfloatperimeter () //calculate theperimeter { floatperimeter; //perimetervariable perimeter = (2* length) + (2 * width); returnperimeter;
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
/* JOptionPane.showMessageDialog(null, "perimeterof the rectangle is " + perimeter); */ //* display theperimeterof the rectangle } publicRectangle (floatl,floatw) //set the length and width of the rectangle { length = ((l > 0.0 && l <20.0) ? l:(float) 0.0); // validate the dimensions of the length and width, set to 0 if outside defined ranges width = ((w > 0.0 && w < 20.0) ? w:(float) 0.0); } publicfloatgetlength () //get length { returnlength; } publicfloatgetwidth () //get width { returnwidth; } publicString toString () //set values into a string format so they can be called { if(length ==0 || width ==0 ) //check errors, if length / width entered are 0 display error message. { return("Incorrect information has been submitted. Please re assess the values of length and width"); } else { returnString.format( "%s: %f\n%s: %f\n%s: %f\n%s: %f", /* string format to output the required data */ "Length", length, "Width", width, "Perimeter", perimeter(), "Area", area() ); } }
} // end method setCoordinates publicfloatlength(floatLx1,floatLy1,floatLx2,floatLy2 ) //get the length of the sides. Equation used is the square root of (the sides squares) { return(float) Math.sqrt( ( Math.pow( Lx1 - Lx2, 2 ) + Math.pow( Ly1 - Ly2, 2 ) ) ); } publicbooleanisRectangle() //check to ensure the coordinates are a rectangle { floatsideA = length( x1, y1, x2, y2 ); floatsideB = length( x2, y2, x3, y3 ); floatsideC = length (x3,y3, x4, y4); floatsideD= length (x4,y4, x1,y1); if( (sideA + sideB == sideB + sideC) ) returntrue;
else returnfalse; } publicbooleanisSquare () //check to see if the object is a square { return(getLength() == getWidth()); } publicfloatgetLength() //get the length of the rectangle { floatlength1; floatsideA = length( x1, y1, x2, y2 ); floatsideB = length( x2, y2, x3, y3 ); length1 = ( sideA > sideB ? sideA : sideB ); //sides compared to ensure the length is the longest returnlength1; } // end method getLength
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
// get width of rectangle publicfloatgetWidth() //getting the width of the rectangle { floatwidth1; floatsideA = length( x1, y1, x2, y2 ); floatsideB = length( x2, y2, x3, y3 ); width1 = ( sideA < sideB ? sideA : sideB ); //ensure the width is returned returnwidth1; } // end method getWidth // publicfloatarea () // calculate the area { floatarea = ((getLength()) * (getWidth())); returnarea;
} publicfloatperimeter () //calculate theperimeter { floatperimeter; //perimetervariable perimeter = (2* (getLength())) + (2 * getWidth()); returnperimeter; /* JOptionPane.showMessageDialog(null, "perimeterof the rectangle is " + perimeter); */ //* display theperimeterof the rectangle } publicString toString () //set values into a string format so they can be called {
if(getLength() ==0 || getWidth() ==0 ) //check errors, if length / width entered are 0 display error message. { return("Incorrect information has been submitted. Please re assess the values of length and width"); } elseif( !isRectangle()) { return( "This is not a rectangle"); } elseif(isSquare()) { return("This is a square"); } else { returnString.format( "%s: %f\n%s: %f\n%s: %f\n%s: %f", /* string format to output the required data */ "Length", getLength(), "Width", getWidth(), "Perimeter", perimeter(), "Area", area() ); } }
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser