Question

Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume...

Part 1:

Use principles of inheritance to write code calculating the area, surface area, and volume of rectangular objects. First, write an abstract super class RectangularShape, then write a subclass Rectangle that inherits from the super class to compute areas of rectangles, including squares if there is only one data. Finally, write another subclass Cuboid that inherits from its super class Rectangle above to compute surface areas and volumes of cuboids, including 3 equal sided cube. Must apply code-reusability in coding and understand what the code-reusability first discussed in the text. Override toString() methods in all developed classes to return the proper data from each object. Write a driver code to test your program creating at least 4 different objects in computing their areas, surface areas, and/or volumes, as applicable to the instantiated objects. Document the source code.

Part 2:

Obtain example code files Shape.java, Circle2.java, Sphere.java, and CircleShapeApp.java from the downloaded files in Ch8. Compile and execute the example and understand the polymorphism it performs. ****I have each of these pasted below.

Modify the application as follows:

  1. Code a class called Cone inherited from class Circle. Cone will override the computeArea() and computeVolume() methods from its superclass to calculate the area and volume of a Cone object. Must apply code-reusability in the calculation formula.

  2. Modify the driver code, CircleShapeApp.java, so it will also create an object of Cone with a height of 12.5 and diameter of 10.48. Modify the polymorphic calls to calculate and display the result of the calculations.

  3. Instead of hard-coded data, modify your application to be interactive using JOptionPane, thus it will ask user to enter proper data for different object, continue to utilize the Validator class you have developed before to validate all wrong data entries, including negative and non-numerical data. Your program must continue to run until the user enters “n” to terminate the execution. You must also validate the user's entry so that only “y” and “n” are accepted.

Shape.java:

//abstract class Shape

public abstract class Shape {
   protected double x1, y1, x2, y2;
   public Shape() {
       x1 = y1 = x2 = y2 = 0.0;
   }
   public Shape(double x1, double y1, double x2, double y2) {
           this.x1 = x1;
           this.y1 = y1;
           this.x2 = x2;
           this.y2 = y2;
       }

   public String toString() {
       String message = "(" + x1 + "," + y1 + "), ("
              + x2 + "," + y2 + ")\n";
       return message;
       }

   public abstract void computeArea();       //∂®“Â≥ÈœÛ∑Ω∑®
   public abstract void computeVolume();
   public abstract double getArea();
   public abstract double getVolume();
}

Circle2.java

//abstract class CircleShape2

public abstract class CircleShape2 extends Shape {       //Inheritance
   protected double radius;

public CircleShape2() {
   x1 = y1 = x2 = y2 = 0.0;
   }
public CircleShape2(double x1, double y1, double x2, double y2) {
       super(x1, y1, x2, y2);       //Call constructor of Shape
   }
public CircleShape2(double radius) {
      this.radius = radius;
   }
   public void setRadius(double radius) {
       this.radius = radius;
   }
   public double getRadius() {
       return radius;
   }
   public void computeRadius() {
       radius = Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2) * (y1 - y2));
   }
   public String toString() {
       return super.toString() + "Radius: " + radius + "\n";
   }
}

Sphere.java

//subclass class Sphere inherites Circle

class Sphere extends Circle{
   public Sphere() {
       super();           //call super class non-argument constructor
   }
   public Sphere(double radius) {
       super(radius);       //call super class one-argument constructor
   }
   public Sphere(double x1, double y1, double x2, double y2) {
       super(x1, y1, x2, y2);//call super class four-argument constructor
   }

   public void computeArea() {       //compute sphere's area
       super.computeArea();       //call super class' method
       area = 4* area;
   }

   public void computeVolume() {   //compute sphere's valume
       super.computeArea();
       volume = 4.0/3 * radius * area;
   }
}

CircleShapeApp.java

//the driver class for circle shapes

public class CircleShapeApp{

   public static void main(String[] args) {
        Circle circle = new Circle(12.98);
        Sphere sphere = new Sphere(25.55);

        Shape shape = circle;

        shape.computeArea();
        System.out.println("circle area: " + shape.getArea());
        shape.computeVolume();
        System.out.println("circle volume: " + shape.getVolume());

       shape = sphere;
        shape.computeArea();
        System.out.println("Sphere area: " + shape.getArea());
        shape.computeVolume();
        System.out.println("Sphere volume: " + shape.getVolume());
    }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your provided code having errors so for other class use this format

/*************************Shape.java*************************************/

public abstract class Shape {
   protected double x1, y1, x2, y2;

   public Shape() {
       x1 = y1 = x2 = y2 = 0.0;
   }

   public Shape(double x1, double y1, double x2, double y2) {
       this.x1 = x1;
       this.y1 = y1;
       this.x2 = x2;
       this.y2 = y2;
   }

   public String toString() {
       String message = "(" + x1 + "," + y1 + "), (" + x2 + "," + y2 + ")\n";
       return message;
   }

   public abstract void computeArea();

   public abstract void computeVolume();

   public abstract double getArea();

   public abstract double getVolume();
}

/*********************************CircleShape2.java***************************************/

public class CircleShape2 extends Shape { // Inheritance
   protected double radius;
   private double area;

   public CircleShape2() {
       x1 = y1 = x2 = y2 = 0.0;
   }

   public CircleShape2(double x1, double y1, double x2, double y2) {
       super(x1, y1, x2, y2); // Call constructor of Shape
   }

   public CircleShape2(double radius) {
       this.radius = radius;
   }

   public void setRadius(double radius) {
       this.radius = radius;
   }

   public double getRadius() {
       return radius;
   }

   public void computeRadius() {
       radius = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
   }

   public String toString() {
       return super.toString() + "Radius: " + radius + "\n";
   }

   @Override
   public void computeArea() {
  
       area = 3.14*radius*radius;
   }

   @Override
   public void computeVolume() {

   }

   @Override
   public double getArea() {
      
       return area;
   }

   @Override
   public double getVolume() {
      
       return 0;
   }
}

/*********************************************Sphere.java******************************/

public class CircleShape2 extends Shape { // Inheritance
   protected double radius;
   private double area;

   public CircleShape2() {
       x1 = y1 = x2 = y2 = 0.0;
   }

   public CircleShape2(double x1, double y1, double x2, double y2) {
       super(x1, y1, x2, y2); // Call constructor of Shape
   }

   public CircleShape2(double radius) {
       this.radius = radius;
   }

   public void setRadius(double radius) {
       this.radius = radius;
   }

   public double getRadius() {
       return radius;
   }

   public void computeRadius() {
       radius = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
   }

   public String toString() {
       return super.toString() + "Radius: " + radius + "\n";
   }

   @Override
   public void computeArea() {
  
       area = 3.14*radius*radius;
   }

   @Override
   public void computeVolume() {

   }

   @Override
   public double getArea() {
      
       return area;
   }

   @Override
   public double getVolume() {
      
       return 0;
   }
}

/***************************************CircleShapeApp.java**********************************/

public class CircleShapeApp {

   public static void main(String[] args) {
       Shape shape = new CircleShape2(12.98);
       Shape shape1 = new Sphere(25.55);

      
       shape.computeArea();
       System.out.println("circle area: " + shape.getArea());
       shape.computeVolume();
       System.out.println("circle volume: " + shape.getVolume());

       shape1.computeArea();
       System.out.println("Sphere area: " + shape1.getArea());
       shape1.computeVolume();
       System.out.println("Sphere volume: " + shape1.getVolume());
   }

}
/********************************output*****************************/

circle area: 529.0284560000001
circle volume: 0.0
Sphere area: 8199.199400000001
Sphere volume: 279319.39289333334

Thanks a lot, Please let me know if you have any problem..................

Add a comment
Know the answer?
Add Answer to:
Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is to...

    PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is too difficult you do not have to do part 3 but it would be greatly appreciated if you do ! Obtain example code files Circle.java, Shape.java, CircleShape2.java, Sphere.java, and CircleShapeApp.java from the downloaded files in Ch8. Compile and execute the example and understand the polymorphism it performs. (I have copied and pasted all of these files below) Modify...

  • Define an interface named Shape with a single method named area that calculates the area of...

    Define an interface named Shape with a single method named area that calculates the area of the geometric shape:        public double area(); Next, define a class named Circle that implements Shape. The Circle class should have an instance variable for the radius, a constructor that sets the radius, an accessor and a mutator method for the radius, and an implementation of the area method. Define another class named Rectangle that implements Shape. The Rectangle class should have instance variables...

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they...

    Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they both say: constructor Shape in class Shape cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length The directions say: The files Shape.java and TestArea.java have been finished already, YOU DONT ADD ANYTHING TO THESE TWO. According to the requirement, you need to modify in Square.java and Rectangle.java, respectively a. Implementing constructor with no...

  • Please help me to solve this source code. Use the following file: InterfaceRunner.java import java.util.ArrayList; public...

    Please help me to solve this source code. Use the following file: InterfaceRunner.java import java.util.ArrayList; public class InterfaceRunner { public static void main(String[] args) { ArrayList<GeometricSolid> shapes = new ArrayList<>(); shapes.add(new Sphere(10)); shapes.add(new Sphere(1)); shapes.add(new Cylinder(1, 5)); shapes.add(new Cylinder(10, 20)); shapes.add(new RightCircularCone(1, 5)); shapes.add(new RightCircularCone(10, 20)); /* * Notice that the array list holds different kinds of objects * but each one is a GeometricSolid because it implements * the interface. * */ for (GeometricSolid shape : shapes) { System.out.printf("%.2f%n",shape.volume());...

  • The following code computes the radius of a circle. Using static methods from the Math class...

    The following code computes the radius of a circle. Using static methods from the Math class complete the method that computes the radius of a circle using the formula  r2=(x-h)2 +(y-k)2 , given the (x,y) coordinates of one point on its circumference and the (h,k) coordinates of its center. public class Circle {    public static void main(String[] C)    {        double x1 =14.25;        double y1 =13.68;        double xCenter = 25.678;        double yCenter...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

  • Create a class named Circle with fields named radius, diameter, and area. Include a constructor that...

    Create a class named Circle with fields named radius, diameter, and area. Include a constructor that sets the radius to 1 and calculates the other two values. Also include methods named setRadius() and getRadius(). The setRadius() method not only sets the radius, but it also calculates the other two values. (The diameter of a circle is twice the radius, and the area of a circle is pi multiplied by the square of the radius. Use the Math class PI constant...

  • Write a java class definition for a circle object. The object should be capable of setting...

    Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { }    //get method (Accessor Methods ) public double getRadius (...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT