Cones come in a few varieties, and we will consider the right circular cone. A cone with a circular base is a circular cone. A circular cone whose axis is perpendicular to the base is a right circular cone.
1.Create a new class called
.Include a Javadoc comment at the top of the class. The Javadoc comment should contain:
i.The name of the class and a (very) short description
ii.An @author tag followed by your name
iii.An @version tag followed by the version number.
2.Do not include a main method inside the class definition. As before, the class is not a complete program.
3.Let’s not worry about a Cone’s location in 3D space (yet!). If we ignore the right circular cone’s location, we can represent it with two variables: radius and height. Create instance variables for its radius and height.
4.A Cone has a public constructor which accepts one parameter for each of the instance variables. The body of the constructor should assign each parameter to its respective instance variable.
5.Create an accessor and a mutator for each instance variable.
6.Create a method that returns the volume of the Cone. The formula for the volume V of a cone of radius r and height h =$#휋푟!ℎ.
7.Create a method that returns the slant height of the Cone. The formula for the slant height SH of a Cone of radius r and height h =√푟!+ℎ!.
8.Create a method that returns the surface area of the Cone. The formula for the surface area A of a Cone of radius r and height h=휋푟!+휋푟(√푟!+ℎ!)9.Create a toString() method which returns a String composed of the concatenation of the information in the Cone.
Source code of the program and its working are given below.Comments are also given along with the code for better understanding.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Working of the program
Source code
Cone class
/** Cone class represent right circular cone with radius and height * it contain methods to find volume,surface area,slant height * @author Your name * @version 1.0 */ public class Cone { //instance variable declaration private double radius; private double height; //constructor to initialize variables public Cone(double radius, double height) { this.radius = radius; this.height = height; } //getter methods to get values public double getRadius() { return radius; } public double getHeight() { return height; } //setter methods to set values public void setRadius(double radius) { this.radius = radius; } public void setHeight(double height) { this.height = height; } //method to calculate volume public double getVolume() { double v=3.1416*radius*radius*height/3; return v; } //method to calculate slant height public double getSlantHeight() { double slantHeight=Math.sqrt(height*height+radius*radius); return slantHeight; } //method to calculate surface area public double getSurfaceArea() { double surfaceArea=3.1416*radius*(radius+Math.sqrt(height*height+radius*radius)); return surfaceArea; } //overriding toString() method to print information of cone directly @Override public String toString() { return "\nRadius: "+radius+"\nHeight: "+height; } }
Main class
public class Main { public static void main(String[] args) { //create Cone object Cone c=new Cone(8,12); //print cone information System.out.println(c); //display volume System.out.println("Volume: "+c.getVolume()); //display slant height System.out.println("Slant height: "+c.getSlantHeight()); //display surface area System.out.println("Surface area: "+c.getSurfaceArea()); } }
Screen shot of the code
Screen shot of the output
Get Answers For Free
Most questions answered within 1 hours.