Question

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 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.

----- Circle.java -----

//subclass class Circle

public class Circle extends CircleShape2{
   protected double area;
   protected double volume;

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

   public void computeArea() {
       area = Math.PI * radius * radius;
   }

   public double getArea() {
       return area;
   }

   public void computeVolume() {}                    //must implement
  
   public double getVolume() {
       return volume;
   }
   public String toString() {                   //override the toString() method
       return super.toString() + "Area: " + area + "\n";
    }

}

----- 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();
}

----- CircleShape2 -----

//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

//the driver class for circle shapes

//I have made the following changes to the code

//Output Screen attached

--------CircleShapeApp--------

public class CircleShapeApp{

public static void main(String[] args) {
Circle circle = new Circle(12.98);
Sphere sphere = new Sphere(25.55);
Cone cone=new Cone(12.5,10.48); //Creating Cone object as given in question
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());

shape=cone; //following same format
shape.computeArea();
System.out.println("Cone area: " + shape.getArea());
shape.computeVolume();
System.out.println("Cone volume: " + shape.getVolume());
  
}

}

----------Cone.java--------


public class Cone extends Circle{
   double l,diameter,height;
  
   public Cone(double diameter,double height){
       this.diameter=diameter;
       this.height=height;
      
      
   }
   public void computeArea() {
       radius=diameter/2;
       l=Math.sqrt(height*height+radius*radius); //l is slant height which is required for surface area.
   area = Math.PI * radius * radius+Math.PI*radius*l; // calculation reusability will increase complexity by creation of //extra circle object.
   }
   public void computeVolume(){
       volume=Math.PI*radius*radius*1/3;
   }
     
}
----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-----

//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-------

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;
}
}

-----Circle.java------

public class Circle extends CircleShape2{
protected double area;
protected double volume;

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

public void computeArea() {
area = Math.PI * radius * radius;
}

public double getArea() {
return area;
}

public void computeVolume() {} //must implement
  
public double getVolume() {
return volume;
}
public String toString() { //override the toString() method
return super.toString() + "Area: " + area + "\n";
}

}

-------output Screen-------

Problems @ JavadocDeclaration E Console Servers cterminated> CircleShapeApp [lava Application] CAProgram FilesJavaljdk1.8.0_1

As Circle has no volume it is 2D Structure.

//Thanks

Add a comment
Know the answer?
Add Answer to:
PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is to...
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
  • 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...

  • 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...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • 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...

  • Please answer this question clearly and correctly! the code should be handwritten if all possible!! ALSO MAKE SURE IT IS WRITTEN IN C++! thank you so much in advance and I will upvote when done right...

    Please answer this question clearly and correctly! the code should be handwritten if all possible!! ALSO MAKE SURE IT IS WRITTEN IN C++! thank you so much in advance and I will upvote when done right! (26 points total) Write an inheritance hierarchy for classes of simple shapes. 1. Create a class Shape. Derive the class ThreeDimensionalShape from the Shape class. Derive the class Sphere from the ThreeDimensionalShape class Use abstract classes to define Shape and ThreeDimensionalShape classes, and then...

  • A general shape class is shown below. Shape has a dimension. It also defines constructors, getters,...

    A general shape class is shown below. Shape has a dimension. It also defines constructors, getters, setters and a toString method. class Shape{ int dimension; public Shape(){} public Shape(int newDimension){ dimension = newDimension; } public int getDimension(){ return dimension; } public void setDimension(int newDimension){ dimension = newDimension; } public String toString(){ return "Shape has dimension "+dimension; } } a. Define classes Circle and Square, which inherit from Shape class. b. Both classes must have two constructors similar to Shape class....

  • This is a question about Java abstract classes. The correct answer is E and I do...

    This is a question about Java abstract classes. The correct answer is E and I do not understand why. Please be specific! I am a beginner in Java abstract classes and methods... 13.6 What is the output of running class Test? public class Test {   public static void main(String[] args) {     new Circle9();   } } public abstract class GeometricObject {   protected GeometricObject() {     System.out.print("A");   }   protected GeometricObject(String color, boolean filled) {     System.out.print("B");   } } public class Circle9 extends GeometricObject {...

  • 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...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • 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());...

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