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

this is an example. I need to do this with the shape: circle to cylinder and con

ID: 3679662 • Letter: T

Question

this is an example. I need to do this with the shape: circle to cylinder and cone and Rectangle to pyramid

import java.io.*; import java.util.*;

public class ShapeTester {

//---------------------------------------------------------------------------

// Read the dimensions of various 3-D shapes from an input file, then // displays pertinent information about each shape. //---------------------------------------------------------------------------

public static void main (String[] args)

{

try {

Scanner scan = new Scanner (new File("shapes.dat")); double width, length, height, side, radius;

// Read the data from the input file while (scan.hasNext())

{

String shapeType = scan.next();

if (shapeType.equalsIgnoreCase("tetrahedron")) {

side = scan.nextDouble();

height = scan.nextDouble();

System.out.println (new Tetrahedron (side, height));

} }

}

catch (Exception except) {

System.err.println(except); }

} }

/* Shape.java Preparer: Your Name

* Start-up Solution to Programming Project 8.11

*/

public abstract class Shape

{

abstract public double computeArea(); abstract public double computePerimeter(); }

Current contents of fileshapes.dat:

Tetrahedron 4 5

Sample Run:

Triangle Pyramid: Height is 5 perimeter of base is 12, area is 37.72 volume is 11.55

1

/* Triangle.java 0 Preparer: Your Name

* Start-up Solution to Programming Project 8.11 (

*/

import java.text.*;

public class Triangle extends Shape {

protected double side;

protected static DecimalFormat form = new DecimalFormat("0.##"); //---------------------------------------------------------------------------

// Sets up the triangle by entering the length of a side //---------------------------------------------------------------------------

public Triangle (double sid)

{

side = sid; }

//--------------------------------------------------------------------------- // Returns the double value of the side //--------------------------------------------------------------------------- public double getSide()

{

return side;

} //--------------------------------------------------------------------------- // Returns the double value of the height of the triangle //--------------------------------------------------------------------------- public double getHeight()

{

return Math.sqrt(Math.pow(side, 2) - Math.pow(side / 2, 2)); }

//--------------------------------------------------------------------------- // Returns the calculated value of the area //--------------------------------------------------------------------------- public double computeArea()

{

return side * getHeight() / 2;

} //--------------------------------------------------------------------------- // Returns the calculated value of the perimeter //--------------------------------------------------------------------------- public double computePerimeter()

{

return side * 3; }

//--------------------------------------------------------------------------- // Returns pertinent information about the triangle //--------------------------------------------------------------------------- public String toString()

{

return "Triangle: side length is " + form.format(side) +

" perimeter is " + form.format(computePerimeter()) +

", area is " + form.format(computeArea()); }

}

2

/*Tetrahedron.java Preparer: Your Name

* Start-up Solution to Programming Project 8.11

*/

public class Tetrahedron extends Triangle {

private double height;

// Sets up the pyramid by entering its base side length and height //---------------------------------------------------------------------------

public Tetrahedron (double sid, double hei)

{

super(sid);

height = hei; }

// Returns the double value of the height //--------------------------------------------------------------------------- public double getPyramidHeight()

{

return height; }

// Returns the calculated value of a face height //--------------------------------------------------------------------------- public double faceHeight()

{

return Math.sqrt(Math.pow(height, 2) + Math.pow(getHeight() / 2, 2)); }

// Returns the calculated value of a face area //--------------------------------------------------------------------------- public double faceArea()

{

return faceHeight() * side / 2; }

// Returns the calculated value of the surface area //---------------------------------------------------------------------------

public double computeArea() {

return 3 * faceArea() + super.computeArea(); }

// Returns the calculated value of the volume //---------------------------------------------------------------------------

public double computeVolume() {

return super.computeArea() * height / 3; }

// Returns pertinent information about the pyramid //---------------------------------------------------------------------------

public String toString() {

return "Triangle Pyramid: Height is " + form.format(height) + " perimeter of base is " + form.format(computePerimeter()) + ", area is " + form.format(computeArea()) +

" volume is " + form.format(computeVolume()) + " ";

} }

3

Explanation / Answer

/* Implement a Circle, Triangle, and Rectangle class which extend the class Shape.

* Ex: public class Circle extends Shape ... etc

*/

public abstract class Shape {

    public abstract double area();

    public abstract double perimeter();

}

Rectangle.java

public class Rectangle extends Shape {

    private final double width, length; //sides

    public Rectangle() {

        this(1,1);

    }

    public Rectangle(double width, double length) {

        this.width = width;

        this.length = length;

    }

    @Override

    public double area() {

        // A = w * l

        return width * length;

   }

    @Override

    public double perimeter() {

        // P = 2(w + l)

        return 2 * (width + length);

    }

}

Circle.java

public class Circle extends Shape {

    private final double radius;

    final double pi = Math.PI;

    public Circle() {

        this(1);

    }  

    public Circle(double radius) {

        this.radius = radius;

    }

    @Override

    public double area() {

        // A = r^2

        return pi * Math.pow(radius, 2);

    }

    public double perimeter() {

        // P = 2r

        return 2 * pi * radius;

    }

}

triangle.java

public class Triangle extends Shape {

    private final double a, b, c; // sides

    public Triangle() {

        this(1,1,1);

    }

    public Triangle(double a, double b, double c) {

        this.a = a;

        this.b = b;

        this.c = c;

    }

    @Override

    public double area() {

        // Heron's formula:

        // A = SquareRoot(s * (s - a) * (s - b) * (s - c))

        // where s = (a + b + c) / 2, or 1/2 of the perimeter of the triangle

        double s = (a + b + c) / 2;

        return Math.sqrt(s * (s - a) * (s - b) * (s - c));

    }

    @Override

    public double perimeter() {

        // P = a + b + c

        return a + b + c;

    }

}

Testshape.java

public class TestShape {

    public static void main(String[] args) {

        // Rectangle test

        double width = 5, length = 7;

        Shape rectangle = new Rectangle(width, length);

        System.out.println("Rectangle width: " + width + " and length: " + length

                + " Resulting area: " + rectangle.area()

                + " Resulting perimeter: " + rectangle.perimeter() + " ");

        // Circle test

        double radius = 5;

        Shape circle = new Circle(radius);

        System.out.println("Circle radius: " + radius

            + " Resulting Area: " + circle.area()

            + " Resulting Perimeter: " + circle.perimeter() + " ");

        // Triangle test

        double a = 5, b = 3, c = 4;

        Shape triangle = new Triangle(a,b,c);

        System.out.println("Triangle sides lengths: " + a + ", " + b + ", " + c

                + " Resulting Area: " + triangle.area()

                + " Resulting Perimeter: " + triangle.perimeter() + " ");

    }

}

Note- by modifying or by using the above code the given question can be done.