OOP lab 8
Exercise 1
Design and implement a subclass “EquilateralTriangle” having a double variable side denoting
the three sides of the equilateral triangle [Note that since all the 3 sides
are equal, the constructor will have only one parameter]. The area and perimeter
of the equilateral triangle are given as follows:
Area = ¼**(side)2
Perimeter = 3*side
Provide accessor methods for the
sides. Test your class using the TestShapes and DownCastingShapes classes.
Exercise 1
By looking at the formulae for an
ellipse, provide the missing code for all of the methods in the class Ellipse including the toString() method. Test your program using the TestShapes.java
class. Your output should look as follows (for an ellipse with a = 10
and b = 7) (values are
randomly generated).
Square
Area=100.0
Perimeter=40.0
Ellipse
Area=219.9114857512855
Perimeter=53.8212680240788
Eccentricity=0.714142842854285
Press any key to continue...
Exercise 2
How about the following class
Circle.Since a Circle is a special case of an Ellipse, will the output of
TestShapes.java be affected if the following class is used instead of the class
Circle used previously:
Circle.java
public
class Circle extends Ellipse {
public Circle(double radius){
super(radius, radius);
}
}
With this modification, the class diagram would look as
follows:
![]() |
SOURCE CODE:
public
abstract class shape
{
public
String name(){
return getClass().getName();
}
public abstract double area();
public abstract double perimeter();
public String toString() {
return "\n" + name() +
"\nArea="+area()+
"\nPerimeter="+perimeter();
}}
public class
circle extends shape {
private double radius;
public circle(double r){
radius = r;
}
public double area(){
return Math.PI * (radius
* radius);
}
public double perimeter(){
return 2.0 * Math.PI *
radius;
}
public double getRadius(){
return radius;
}}
public class
rectangle extends shape {
private double length;
private double width;
public rectangle(double length,
double width){
this.length = length;
this.width = width;
}
public double area(){
return length * width;
}
public double perimeter(){
return 2*(length+width);
}
public double getLength(){
return length;}
public double getWidth(){
return width;
}}
public class
ellipse extends shape implements eccentric
{
double a, b;
public ellipse(double s1, double
s2){
if(s1 < s2) {// 2 5
a = s2;//5
b = s1;//2
}
else {
a = s1;
b = s2;
}
}
public double perimeter(){
return Math.PI;
}
public double area(){
return
Math.PI*a*b;
}
public double eccentricity(){
return b/a;
}
public String toString(){
return "\n" +
name() + "\nArea="+area()+
"\nPerimeter="+perimeter()
+ "\nEccentricity="+eccentricity();
}
}
public class
square extends rectangle{
public square(double length){
super(length, length);
}}
public class
equilateraltriangle extends shape {
private double side;
equilateraltriangle(double side)
{
this.side=side;
}
public double area()
{
return 0.25*(side*side);
}
public double perimeter()
{
return 3*side;
}
public double getSide() {
return side;
}
}
interface
eccentric {
double eccentricity();
}
public class
downcastingshapes{
public static
void main(String[] args){
shape[]
randomShapes = testshape.createShape();
for(int i =
0; i < randomShapes.length; i++){
System.out.println(randomShapes[i]);
if(randomShapes[i]
instanceof circle)
{
System.out.println("Radius=" +
((circle)
randomShapes[i]).getRadius());
}
else
if(randomShapes[i] instanceof square){
System.out.println("Length="
+
((square)
randomShapes[i]).getLength());
}
else
if(randomShapes[i] instanceof rectangle){
System.out.println("Length=" +
((rectangle)
randomShapes[i]).getLength()
+
"\nWidth=" +
((rectangle)
randomShapes[i]).getWidth());}
else
if(randomShapes[i] instanceof equilateraltriangle)
{
System.out.println("Side=" +
((equilateraltriangle)
randomShapes[i]).getSide());
}
}}}
public class
testshape {
public static shape[] createShape() {
final int SIZE = 5;
final double DIMENSION = 100;
final int NUMBEROFSHAPES = 3;
Random generator = new Random();
//create an array having b/w 1 and SIZE
entries
shape[] randomShapes = new
shape[generator.nextInt(SIZE) + 1];
for(int i = 0; i <
randomShapes.length; i++)
{
//randomly generate values b/w 0
and NUMBEROFSHAPES - 1
int assigner =
generator.nextInt(NUMBEROFSHAPES);
switch(assigner) {
case 0: randomShapes[i] =
new
rectangle(generator.nextDouble()*DIMENSION,
generator.nextDouble()*DIMENSION);
break;
case 1: randomShapes[i] =
new
circle(generator.nextDouble()*DIMENSION);
break;
case 2: randomShapes[i] =
new
square(generator.nextDouble()*DIMENSION);
break;
}
}
return randomShapes;
}
public static void main(String[]
args) {
shape[] randomShapes =
testshape.createShape();
for(int i = 0; i <
randomShapes.length; i++)
System.out.println(randomShapes[i]);
}
}

Comments
Post a Comment