Question

This must be done in Java and it must contain a public static void (string []args)...

This must be done in Java and it must contain a public static void (string []args)

10.13 (Project: Shape Hierarchy) Implement the Shape hierarchy shown in Fig. 9.3.  Each TwoDimensionalShape should contain method getArea to calculate the area of the two-dimensional shape. Each ThreeDimensionalShape should have methods getArea and getVolume to calculate the surface area and volume, respectively, of the three-dimensional shape.  Create a program that uses an array of Shape references to objects of each concrete class in the hierarchy.  The program should print a text description of the object to which each array element refers.  Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape.  If it’s a TwoDimensionalShape, display its area.  If it’s a ThreeDimensionalShape, display its area and volume.

page 439

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You

====================================

// Shape.java

public class Shape {
   //Declaring instance variables
   private String name;

   //Parameterized constructor
   public Shape(String name) {
   this.name = name;
   }
   //getters and setters
   public String getName() {
   return name;
   }

   public void setName(String name) {
   this.name = name;
   }

   //toString method is used to display the contents of an object inside it
   @Override
   public String toString() {
   return "Shape is "+name;
   }
  
   }

====================================

// TwoDimensionalShape.java

public abstract class TwoDimensionalShape extends Shape {

public TwoDimensionalShape(String name) {

super(name);

}

public abstract double getArea();

public abstract double getPerimeter();

}

====================================

// Circle.java

public class Circle extends TwoDimensionalShape {

//Declaring instance variables

private double radius;

//Parameterized constructor

public Circle(String name, double radius) {

super(name);

this.radius = radius;

}

//This method will calculate the area of the Circle

@Override

public double getArea() {

return Math.PI*radius*radius;

}

//This method will calculate the perimeter of the Circle

@Override

public double getPerimeter() {

return 2*Math.PI*radius;

}

}

====================================

// ThreeDimensionalShape.java

public abstract class ThreeDimensionalShape extends Shape {

public ThreeDimensionalShape(String name) {

super(name);

}

public abstract double getArea();

public abstract double getVolume();

}

====================================

// Cube.java

public class Cube extends ThreeDimensionalShape {

//Declaring instance variables

private double side;

//Parameterized constructor

public Cube(String name, double side) {

super(name);

this.side = side;

}

//This method will calculate the area of the Cube

@Override

public double getArea() {

return 6*side*side;

}

//This method will calculate the volume of the Cube

@Override

public double getVolume() {

return Math.pow(side,3);

}

}

====================================

// Sphere.java

public class Sphere extends ThreeDimensionalShape {

//Declaring instance variables

private double raidus;

//Parameterized constructor

public Sphere(String name, double raidus) {

super(name);

this.raidus = raidus;

}

//This method will calculate the area of the Sphere

@Override

public double getArea() {

return 4*Math.PI*raidus*raidus;

}

//This method will calculate the volume of the Sphere

@Override

public double getVolume() {

return (4.0/3.0)*Math.PI*Math.pow(raidus,3);

}

}

====================================

// Rectangle.java

public class Rectangle extends TwoDimensionalShape {

   private double width;
   private double height;
  
  
   public Rectangle(String name, double width, double height) {
       super(name);
       this.width = width;
       this.height = height;
   }

   @Override
   public double getArea() {
      
       return width*height;
   }

   @Override
   public double getPerimeter() {
      
       return 2*(width+height);
   }

}

====================================

// Test.java

public class Test {

public static void main(String[] args) {

Shape s[]={
new Circle("Circle",5.5),
new Rectangle("Rectangle",5,7),
new Sphere("Sphere",4.5),
new Cube("Cube",3)
};
for(int i=0;i<s.length;i++)
{
if(s[i] instanceof TwoDimensionalShape)
{
System.out.println("___"+s[i].getName()+"___");
System.out.printf("Area :%.2f\n",((TwoDimensionalShape)s[i]).getArea());
}
else if(s[i] instanceof ThreeDimensionalShape)
{
System.out.println("___"+s[i].getName()+"___");
System.out.printf("Area :%.2f\n",((ThreeDimensionalShape)s[i]).getArea());
System.out.printf("Volume :%.2f\n",((ThreeDimensionalShape)s[i]).getVolume());
}
}

}

}


====================================

Output:

___Circle___
Area :95.03
___Rectangle___
Area :35.00
___Sphere___
Area :254.47
Volume :381.70
___Cube___
Area :54.00
Volume :27.00

=======================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
This must be done in Java and it must contain a public static void (string []args)...
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
  • Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...

    Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...

  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

  • JAVA Language public class ArraySkills { public static void main(String[] args) { // *********************** // For...

    JAVA Language public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // String[] myData; // 1. Instantiate the given array to hold 10 Strings. // 2. Add your name to the Array at index 0 and a friend's name to the Array at index 4 // 3. Move your...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {       ...

    import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);               Mileage mileage = new Mileage();               System.out.println("Enter your miles: ");        mileage.setMiles(input.nextDouble());               System.out.println("Enter your gallons: ");        mileage.setGallons(input.nextDouble());               System.out.printf("MPG : %.2f",mileage.getMPG());           } } public class Mileage {    private double miles;    private double gallons;    public double getMiles()...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • THIS IS IN THE JAVA SCRIPT CODE ALSO PLEASE SEND ME THE CODE TYPED THANK YOU....

    THIS IS IN THE JAVA SCRIPT CODE ALSO PLEASE SEND ME THE CODE TYPED THANK YOU. Program 1 Write an inheritance hierarchy of three-dimensional shapes. Make a top-level shape interface that has methods for getting information such as the volume and the surface area of a three-dimensional shape. Then make classes and subclasses that implement various shapes such as cube, cylinders and spheres. Place common behavior in superclasses whenever possible, and use abstract classes as appropriate. Add methods to the...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • Import java.util.*; public class PairFinder { public static void main(String[] args) { Scanner sc...

    import java.util.*; public class PairFinder { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read in the value of k int k = Integer.parseInt(sc.nextLine());    // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); numbers = new int[numberStrings.length]; for (int i = 0; i < numberStrings.length; i++) { numbers[i] = Integer.parseInt(numberStrings[i]); } }    System.out.println(findPairs(numbers, k)); }    //method that...

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