Answered You can buy a ready-made answer or pick a professional tutor to order an original one.
Triangle 2.0 (The name is a hint) Description: For the task we'll build a class representing a Rectangle. The tests for this task will not check what's happening inside the class you create, so it'
Triangle 2.0 (The name is a hint) Description:
For the task we'll build a class representing a Rectangle. The tests for this task will not check what's happening inside the class you create, so it's up to you as to what data members you use. The tests will also assume you can write methods and constructors correctly. This means that you have to get the names, returns and parameters correct before the tests will even run.
Okay, now the task itself.
Your company is doing some consulting work and the client has asked for a new, more exciting version of their existing Triangle. They want a Triangle, but with an extra side, which marketing are calling Triangle 2.0.
To meet the specification you need to create a class called Rectangle with the following methods:
- A no-parameter constructor that sets all side lengths to 1.
- A one parameter constructor that takes an int, and sets all side lengths to that value.
- A two parameter constructor, that takes two ints, and sets two opposite sides to one length, and the other pair of opposite sides to the other length.
- A method getShortSide that takes no parameters and returns the length of the shortest side as an int.
- A method getLongSide that takes no parameters and returns the length of the longest side as an int.
- A method isSquare that takes no parameters and returns true if the rectangle is a square, and false otherwise.
- A method area that takes no parameters and returns the area of the rectangle as an int.
- A method diagonal that takes no parameters and returns the length of the diagonals as a double. The library function Math.sqrt(double) may be useful here.
- A method bigger that takes a Rectangle as a parameter and returns true if the current Rectangle has an area strictly greater than the area of one given in the parameter.
You don't have to worry about not-right-angled quadrilaterals (so there's only squares and rectangles, no parallelograms, rhombi, trapezoids etc.).
You also start with nothing in the scaffold. You will have to create a suitable file for the class. The run button will assume that Rectangle has a main method, but it's not part of the tests, so you don't have to write it if you don't care about it.
- @
- ANSWER
import java.lang.Math;
public class Rectangle {
// sides variables
int sideA ;
int sideAA ;
int sideB ;
int sideBB ;
//A no-parameter constructor that sets all side lengths to 1.
public Rectangle() {
sideA =1 ;
sideAA =1;
sideB =1;
sideBB =1 ;
}
//A one parameter constructor that takes an int, and sets all side lengths to that value.
public Rectangle( int set) {
sideA =set ;
sideAA =set;
sideB =set;
sideBB =set ;
}
/*
A two parameter constructor, that takes two ints, and sets two opposite sides to one length,
and the other pair of opposite sides to the other length.
*/
public Rectangle(int w , int l) {
sideA =w ;
sideAA =w;
sideB =l;
sideBB =l ;
}
//A method getShortSide that takes no parameters and returns the length of the shortest side as an int.
public int getShortSide(){
if( sideA < sideAA && sideA < sideB && sideA < sideBB)
return sideA;
else if( sideAA < sideA && sideAA < sideB && sideAA < sideBB)
return sideAA;
else if( sideB < sideA && sideB < sideAA && sideB < sideBB)
return sideB;
else
return sideBB;
}
//A method getLongSide that takes no parameters and returns the length of the longest side as an int.
public int getLongSide(){
if( sideA > sideAA && sideA > sideB && sideA > sideBB)
return sideA;
else if( sideAA > sideA && sideAA > sideB && sideAA > sideBB)
return sideAA;
else if( sideB > sideA && sideB > sideAA && sideB > sideBB)
return sideB;
else
return sideBB;
}
//A method isSquare that takes no parameters and returns true if the rectangle is a square, and false otherwise.
public boolean isSquare(){
if( sideA == sideAA && sideB == sideBB && sideAA == sideBB)
return true;
return false ;
}
//A method area that takes no parameters and returns the area of the rectangle as an int.
public int area(){
return sideA *sideB ;
}
/*
A method diagonal that takes no parameters and returns the length of the diagonals as a double.
The library function Math.sqrt(double) may be useful here.
*/
public double diagonal(){
return Math.sqrt( (sideA* sideB) +(sideAA *sideBB ) ) ;
}
/*
A method bigger that takes a Rectangle as a parameter and returns true
if the current Rectangle has an area strictly greater than the area of one given in the parameter.
*/
public boolean bigger(Rectangle rect){
if( this.area() > rect.area() )
return true ;
return false ;
}
// main to driver and test
public static void main (String[] args) {
// default Rectangle with length 1
Rectangle rectang = new Rectangle() ;
System.out.println( "default Rectangle test ");
// tests methods
System.out.println( rectang.getShortSide() );
System.out.println( rectang.getLongSide() );
System.out.println( rectang.isSquare() );
System.out.println( rectang.area() );
System.out.println( rectang.diagonal() );
Rectangle rectang2 = new Rectangle(2) ;
// compare Rectangle
System.out.println( rectang2.bigger(rectang) );
}
}