Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q: Assume you have a class Square. This class has an instance variable, side tha

ID: 3652591 • Letter: Q

Question

Q:
Assume you have a class Square. This class has an instance variable, side that is protected and of type double.

Write the class definition of a subclass of Square called FancySquare that has a method called getDiagonal. The getDiagonal method gets no arguments but returns the value of side multiplied by the square root of two.
My Code:
class Square {
protected double side = 0;
public Square(double sideLen)
this.side = sideLen;
}
public class FancySquare extends Square {
public FancySquare(double sideLen) {
super(sideLen);
}
double getDiagonal() {
return (side) * Math.sqrt(2);
}
public static void main(String args[]){
FancySquare fs = new FancySquare(2);
System.out.println(fs.getDiagonal());
}
}

Explanation / Answer

class Square { protected double side = 0; public Square(double sideLen) { this.side = sideLen; } } public class FancySquare extends Square { public FancySquare(double sideLen) { super(sideLen); } double getDiagonal() { return (side) * Math.sqrt(2); } public static void main(String args[]){ FancySquare fs = new FancySquare(2); System.out.println(fs.getDiagonal()); } }