We have introduced Abstract Classes and Interfaces in the lectures. In this lab,
ID: 3802389 • Letter: W
Question
We have introduced Abstract Classes and Interfaces in the lectures. In this lab, we would like to provide few questions to review these topics and hands-on to solve them. Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all eight sides of the octagon arc of equal length. The area can be computed using the following formula: area = (2 + 4/Squareroot 2) * side * side Write a test program that creates an Octagon object with side value 9 and displays its area and perimeter. Create a new object using the clone () method and compare the two objects using the compareTo () method. Expected results: Area is 391.10 Perimeter is 72.0 Compare the methods 0Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class Octagon //extends GeometricObject implements Comparable,Cloneable
{
double area;
double perimeter;
private Octagon(double side)
{
area = ((2+4/Math.sqrt(2))*side*side);//calculating area with formula
perimeter =side*8;//calculating perimeter
}
public static void main(String argv[])
{
//test program
Octagon o = new Octagon(9.0);
System.out.println("Area is "+Math.round(o.area*100.0)/100.0);
System.out.println("Perimeter is "+Math.round(o.perimeter*100.0)/100.0);
}
}
ouput:-
run:
Area is 391.1
Perimeter is 72.0
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.