(60 points): A Shape class is given. The Shape class has one data member: type (
ID: 3596066 • Letter: #
Question
(60 points):
A Shape class is given. The Shape class has one data member: type (String), which is used to store the type of shape (Rectangle, Right triangle, Square, Circle, etc.) and two abstract methods as specified in the following method signatures-both methods return a double value:
perimeter() and area()
Write a subclass named RightTriangle, which inherits from Shape. A right triangle has three sides: the longest side, opposite the right angle, is called the hypotenuse, which is labeled as c. The other two sides are called the legs of the triangle, they are labeled as a and b. All a,b,c should be declared as double data type and no any other additional data members in the right triangle class; All three sides are limited to [1.0, 100.00] and the three sides can form a right triangle (Pythagorean theorem). Otherwise, set to their default values: a=3, b=4, c=5)
provides all mutator and accessor methods
implements perimeter and area methods.
provides a constructor (takes 3 parameters: a,b,c that creates a right triangle object
Write a subclass named Square, which inherits from Shape. A square has four sides with the same length, so you need to define one variable named side (double type, a side is limited to [1.0, 100.0], otherwise, default to 10.0)
implements perimeter and area methods.
provides a constructor (takes one parameter: side) that creates a square object.
provides all mutator and accessor methods
override toString method for both subclasses:
for Right triangle class, toString returns, for example:
Right Triangle[a:3.00; b:4.00; c:5.00; Perimeter: 12.00; Area: 6.00]
for Square class, toString returns, for example:
Square[side: 10.00; Perimeter: 40.00; Area: 100.00]
given
Shape.java
public abstract class Shape{
private String type;
public Shape(String type){
setType(type);
}
public String getType(){
return type;
}
public void setType(String type){
this.type = type;
}
public abstract double perimeter();
public abstract double area();
public String toString(){
return type;
}
}
Explanation / Answer
1) Shape.java
package main.webapp;
public abstract class Shape {
private String type;
public Shape(String type) {
setType(type);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public abstract double perimeter();
public abstract double area();
public String toString() {
return type;
}
}
-----------------------------------------------------------------------------------------------------------------
2) Square.java
/**
*
*/
package main.webapp;
/**
* @author sachin.argade
*
*/
public class Square extends Shape {
private double side;
private double perimeter;
private double area;
public Square(double side) {
super("Square");
this.setSide(side);
}
/*
* (non-Javadoc)
*
* @see main.webapp.Shape#perimeter()
*/
@Override
public double perimeter() {
this.perimeter = 4 * side;
return this.perimeter;
}
/*
* (non-Javadoc)
*
* @see main.webapp.Shape#area()
*/
@Override
public double area() {
this.area = side * side;
return this.area;
}
/**
* @return the side
*/
public double getSide() {
return side;
}
/**
* @param side
* the side to set
*/
public void setSide(double side) {
if (sizeOfSide(side)) {
this.side = side;
} else {
this.side = 10.00;
}
}
private boolean sizeOfSide(double value) {
if (value >= 1.00 && value <= 100.00) {
return true;
}
return false;
}
@Override
public String toString() {
return "Side:" + this.side + "; Perimeter: " + this.perimeter + "; Area: " + this.area;
}
}
---------------------------------------------------------------------------------------
3) RightTriangle.java
/**
*
*/
package main.webapp;
/**
* @author sachin.argade
*
*/
public class RightTriangle extends Shape {
// Triangle logest side called hypotenuse
private double c;
// Triangle logest side called side legs
private double a;
// Triangle logest side called side legs
private double b;
private double perimeterofTriangle;
private double area;
public RightTriangle(double a, double b, double c) {
super("RightTriangle");
this.setA(a);
this.setB(b);
this.setC(c);
}
/*
* (non-Javadoc)
*
* @see main.webapp.Shape#perimeter()
*/
@Override
public double perimeter() {
this.perimeterofTriangle = a + b + c;
return this.perimeterofTriangle;
}
/*
* (non-Javadoc)
*
* @see main.webapp.Shape#area()
*/
@Override
public double area() {
this.area = 1 / 2 * a * b;
return this.area;
}
private boolean sizeOfSide(double value) {
if (value >= 1.00 && value <= 100.00) {
return true;
}
return false;
}
/**
* @return the c
*/
public double getC() {
return c;
}
/**
* @param c
* the c to set
*/
public void setC(double c) {
if (sizeOfSide(c)) {
this.c = c;
} else {
this.c = 5.00;
}
}
/**
* @return the a
*/
public double getA() {
return a;
}
/**
* @param a
* the a to set
*/
public void setA(double a) {
if (sizeOfSide(a)) {
this.a = a;
} else {
this.a = 3.00;
}
}
/**
* @return the b
*/
public double getB() {
return b;
}
/**
* @param b
* the b to set
*/
public void setB(double b) {
if (sizeOfSide(b)) {
this.b = b;
} else {
this.b = 4.00;
}
}
@Override
public String toString() {
return "a:"+this.a+"; b:"+this.b+"; c:"+this.c+"; Perimeter: "+this.perimeterofTriangle+"; Area:"+area;
}
}
----------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.