Question

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

Here is the instruction

Writing an Interface

In this problem you will first write an interface and then modify classes to implement the interface. Write an interface, GeometricSolid, which has one method, volume. The volume method takes no arguments and returns a double.

You are provided with three classes: Cylinder, Sphere, and RightCircularCone Modify the classes so that they implement the GeometricSolid interface. Supply the appropriate method for each class. You can use Google search to find the formula for the volume. Make sure that you use Math.PI in your calculations.

Notice in InterfaceRunner that the objects are added to an ArrayList of GeometricSolids.

Here are the source codes provided:

Cylinder.java

/**
* Models a Cylinder
*/
public class Cylinder
{
private double radius;
private double height;

/**
* Constructor for objects of class Cylinder
* @param radius the radius of the Cylinder
* @param height the height of this Cylinder
*/
public Cylinder(double radius, double height)
{
this.radius = radius;
this.height = height;
}
  
/**
* Gets the radius of this Cylinder
* @return the radius of the Cylinder
*/
public double getRadius()
{
return radius;
}
  
/**
* Sets the radius of the Cylinder
* @param newRadius the value of the new radius
*/
public void setRadius(int newRadius)
{
radius = newRadius;
}
  
/**
* Gets the height of this Cylinder
* @return the height of the Cylinder
*/
public double getHeight()
{
return height;
}
  
/**
* Sets the height of the Cylinder
* @param newHeight the value of the new height
*/
public void setHeight(int newHeight)
{
height = newHeight;
}
}

RightCircularCone.java

/**

* Models a Right circular cone

*/
public class RightCircularCone

{
private double radius;
private double height;

/**
* Constructor for objects of class RightCircularCone
* @param radius the radius of the RightCircularCone
* @param height the height of this RightCircularCone
*/
public RightCircularCone(double radius, double height)
{
this.radius = radius;
this.height = height;
}
  
/**
* Gets the radius of this RightCircularCone
* @return the radius of the RightCircularCone
*/
public double getRadius()
{
return radius;
}
  
/**
* Sets the radius of the RightCircularCone
* @param newRadius the value of the new radius
*/
public void setRadius(double newRadius)
{
radius = newRadius;
}
  
/**
* Gets the height of this RightCircularCone
* @return the height of the RightCircularCone
*/
public double getHeight()
{
return height;
}
  
/**
* Sets the height of the RightCircularCone
* @param newHeight the value of the new height
*/
public void setHeight(double newHeight)
{
height = newHeight;
}
}

Sphere.java

/**
* Models a sphere in 3-d space
*/
public class Sphere
{
private double radius;  

/**
* Constructor for objects of class Sphere
* @param radius the radius of this Sphere
*/
public Sphere(double radius)
{
this.radius = radius;
}
  
/**
* Gets the radius of this Sphere
* @return the radius of this Sphere
*/
public double getRadius()
{
return radius;
}
  
/**
* Sets the the radius of this Sphere
* @param newRadius the radius of this Sphere
*/
public void setRadius(double newRadius)
{
radius = newRadius;
}

}

Here is the source code needed to complete:

GeometricSolid.java

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

1. We have 3 classes and 1 interface. In total, 4 java files to be saved in 4 different files with extension .java but in same folder. Because we do not use separate packages.

2. After saving all the files, just compile and run the java file that contains the main method i.e., InterfaceRunner.java file.

GeometricSolid.java

public interface GeometricSolid {
  
/* By default the methods in interface are 'public and abstract',

so needed to be implemented by the class that implement this interface. */

double volume();

}

Cylinder.java

public class Cylinder implements GeometricSolid{
  
   /**
   * Models a Cylinder
   */
   private double radius;
   private double height;

   /**
   * Constructor for objects of class Cylinder
   * @param radius the radius of the Cylinder
   * @param height the height of this Cylinder
   */
   public Cylinder(double radius, double height)
   {
   this.radius = radius;
   this.height = height;
   }
  
   /**
   * Gets the radius of this Cylinder
   * @return the radius of the Cylinder
   */
   public double getRadius()
   {
   return radius;
   }
  
   /**
   * Sets the radius of the Cylinder
   * @param newRadius the value of the new radius
   */
   public void setRadius(int newRadius)
   {
   radius = newRadius;
   }
  
   /**
   * Gets the height of this Cylinder
   * @return the height of the Cylinder
   */
   public double getHeight()
   {
   return height;
   }
  
   /**
   * Sets the height of the Cylinder
   * @param newHeight the value of the new height
   */
   public void setHeight(int newHeight)
   {
   height = newHeight;
   }

   @Override // Interface method implemented
   public double volume() {
                return Math.PI*radius*radius*height;
   }

}

Sphere.java

/**
* Models a sphere in 3-d space
*/
public class Sphere implements GeometricSolid
{
private double radius;

/**
* Constructor for objects of class Sphere
* @param radius the radius of this Sphere
*/
public Sphere(double radius)
{
this.radius = radius;
}

/**
* Gets the radius of this Sphere
* @return the radius of this Sphere
*/
public double getRadius()
{
return radius;
}

/**
* Sets the the radius of this Sphere
* @param newRadius the radius of this Sphere
*/
public void setRadius(double newRadius)
{
radius = newRadius;
}

@Override    // Interface method implemented
public double volume() {
   return (4*Math.PI*Math.pow(radius, 3))/3;
}

}

RightCircularCone.java

/**

* Models a Right circular cone

*/
public class RightCircularCone implements GeometricSolid

{
private double radius;
private double height;

/**
* Constructor for objects of class RightCircularCone
* @param radius the radius of the RightCircularCone
* @param height the height of this RightCircularCone
*/
public RightCircularCone(double radius, double height)
{
this.radius = radius;
this.height = height;
}

/**
* Gets the radius of this RightCircularCone
* @return the radius of the RightCircularCone
*/
public double getRadius()
{
return radius;
}

/**
* Sets the radius of the RightCircularCone
* @param newRadius the value of the new radius
*/
public void setRadius(double newRadius)
{
radius = newRadius;
}

/**
* Gets the height of this RightCircularCone
* @return the height of the RightCircularCone
*/
public double getHeight()
{
return height;
}

/**
* Sets the height of the RightCircularCone
* @param newHeight the value of the new height
*/
public void setHeight(double newHeight)
{
height = newHeight;
}

@Override   // INterface method implemented
public double volume() {
   return Math.PI*radius*radius*(height/3);
}

}

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

OUTPUT

4188.79
4.19
15.71
6283.19
5.24
2094.40

Add a comment
Know the answer?
Add Answer to:
Please help me to solve this source code. Use the following file: InterfaceRunner.java import java.util.ArrayList; public...
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
  • public class CylindricalTank {    private String idTag;           // Unique String identifying the tank...

    public class CylindricalTank {    private String idTag;           // Unique String identifying the tank    private double radius;           // The non-negative radius of the base of the tank in meters    private double height;           // The non-negative height of the tank in meters    private double liquidLevel;       // The current height of the liquid in the tank in meters    /**    * CylindricalTank General Constructor    */    public CylindricalTank(String tag, double...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

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

  • C++ Chapter 10 defined the class circleType to implement the basic properties of a circle. (Add t...

    C++ Chapter 10 defined the class circleType to implement the basic properties of a circle. (Add the function print to this class to output the radius, area, and circumference of a circle.) Now every cylinder has a base and height, where the base is a circle. Design a class cylinderType that can capture the properties of a cylinder and perform the usual operations on the cylinder. Derive this class from the class circleType designed in chapter 10.   Some of the...

  • Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...

    Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g GeometricObject.java GeometricObject.java:92: error: class, interface, or enum expected import java.util.Comparator; ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 20.21 Please code using Java IDE. Please DO NOT use Toolkit. You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit? Please show an...

  • this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea {...

    this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);    Triangle triangle1 = new Triangle(); Triangle triangle2 = new Triangle(); // Read and set base and height for triangle1 (use setBase() and setHeight())    // Read and set base and height for triangle2 (use setBase() and setHeight())    // Determine larger triangle (use getArea())    private int base; private int height; private...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • public class Item implements Comparable { private String name; private double price; /** * Constructor for...

    public class Item implements Comparable { private String name; private double price; /** * Constructor for objects of class Item * @param theName name of item * @param thePrice price of item */ public Item(String theName, double thePrice) { name = theName; price = thePrice; }    /** * gets the name * @return name name of item */ public String getName() { return name; }    /** * gets price of item * @return price price of item */...

  • In C++ Amanda and Tyler opened a business that specializes in shipping liquids, such as milk,...

    In C++ Amanda and Tyler opened a business that specializes in shipping liquids, such as milk, juice, and water, in cylindrical containers. The shipping charges depend on the amount of the liquid in the container. (For simplicity, you may assume that the container is filled to the top.) They also provide the option to paint the outside of the container for a reasonable amount. Write a program that does the following: Prompts the user to input the dimensions (in feet)...

  • I need help with my java code i am having a hard time compiling it //...

    I need help with my java code i am having a hard time compiling it // Cristian Benitez import java.awt.Color; import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JPanel; Face class public class FaceDraw{ int width; int height; int x; int y; String smileStatus; //default constructor public FaceDraw(){ } // Getters and Setters for width,height,x,y public int getWidth(){ return width; } public void setWidth(int width){ this.width=width; } public int getHeight(){ return height; } public void setHeight(int height){ this.height=height; } public int getX(){ return...

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