In: Computer Science
In this lab you will learn how to use methods from the Math class in order to calculate the area or the volume of several different shapes. If you are confused about the Methods you can access from the Math class and would like to see some examples click here.
Hint: Most of these methods can be done in one line.
Step 1 - circleArea
In this method you will calculate the area of a circle.
Before you can calculate the area you must first write the method signature. This method is a public static method that is called circleArea. This method returns a double and takes in one parameter of type double that represents the radius.
In the method create a variable that will represent the area. Next you will want to calculate the area of a circle the equation is PI * radius^2. In order to calculate this you can use the constant from the Math class Math.PI and the method Math.pow(double, double).
After you calculate the area return it at the end of the method.
Testing circleArea
Once you have finished writing the method you should go down to the main method and uncomment the lines of code associated with the circleArea method. Does your code print what it is supposed to?
Step 2 - sphereVolume
In this method you will calculate the volume of a sphere using methods from the Math class.
This method is a public static method that is called sphereVolume. It returns a double and takes in one parameter of type double that represents the radius.
Inside of the method declare a variable of type double that will contain the value to return at the end of your method. After that set that variable equal to the equation: (4/3) * PI * radius ^ 3. Finally, return the variable.
Testing sphereVolume
Once you have finished writing the method you should go down to the main method and uncomment the lines of code associated with the sphereVolume method. Does your code print what it is supposed to?
Step 3 - triangleArea
In this method you will calculate the area of a triangle.
This method is a public static method that returns a double and is called triangleArea. It has two parameters that are both doubles and represent the base and the height.
In this method we are going to try to simplify it and do everything in one line. All you need to do is simply return the equation (base * height) / 2.
Setting the equation equal to a variable of type double and simply returning it accomplishes the same exact thing but returning the equation directly reduces the lines of code required.
Testing triangleArea
Once you have finished writing the method you should go down to the main method and uncomment the lines of code associated with the triangleArea method. Does your code print what it is supposed to?
Step 4 - pyramidVolume
In this method you will calculate the volume of a pyramid.
This method is a public static method that returns a double. It is called pyramidVolume and has three parameters that are all doubles. The parameters represent the length, the width and the height.
In the method you can simply return the equation: (length * width * height) / 3.
Testing pyramidVolume
Once you have finished writing the method you should go down to the main method and uncomment the lines of code associated with the pyramidVolume method. Does your code print what it is supposed to?
Step 5 - round
In this method you will practice rounding numbers up or down.
This method is a public static method that returns an int. It is called round and has one parameter that is a double and represents the number you want to round up or down.
In the method you can simply return the equation: Math.floor(the number + .5). However, Math.floor returns a double and our method returns an int so you must use type casting.
Testing round
Once you have finished writing the method you should go down to the main method and uncomment the lines of code associated with the round method. Does your code print what it is supposed to?
public class Shapes {
//TODO: circleArea
//TODO: sphereVolume
//TODO: triangleArea
//TODO: pyramidVolume
//TODO: round
public static void main(String[] args) {
//System.out.println("Testing circleArea()");
//System.out.println(circleArea(2.3)); //should print
16.619025137490002
//System.out.println(circleArea(5)); // should print
78.53981633974483
//System.out.println(circleArea(7.8)); // should print
191.134497044403
//System.out.println("Testing sphereVolume()");
//System.out.println(sphereVolume(2.3)); //should print
50.965010421636
//System.out.println(sphereVolume(5)); // should print
523.5987755982989
//System.out.println(sphereVolume(7.8)); // should print
1987.798769261791
//System.out.println("Testing triangleArea()");
//System.out.println(triangleArea(2, 3)); //should print 3.0
//System.out.println(triangleArea(3.4, 8.7)); // should print
14.79
//System.out.println(triangleArea(3.0, 4.4)); // should print
6.6000000000000005
//System.out.println("Testing pyramidVolume()");
//System.out.println(pyramidVolume(2, 3, 4)); //should print
8.0
//System.out.println(pyramidVolume(3.4, 8.7, 2.1)); // should print
20.706
//System.out.println(pyramidVolume(3.0, 4.4, 2)); // should print
8.8
//System.out.println("Testing round()");
//System.out.println(round(2.3)); //should print 2
//System.out.println(round(7.8)); // should print 8
//System.out.println(round(6.0)); // should print 6
}
}
Code for your program is provided below. All the functions defined are explained using comments. you can also refer to the screenshot of properly indented code that i have provided . Output screenshot is provided as well.
If you need any further clarification please feel free to ask in commentrs. I would really appreciate if you would let me know if the explanation given is to your satisfaction.
#######################################################################
CODE
public class Shapes {
//method to return area of circle
public static double circleArea(double radius)
{
return (Math.PI * Math.pow(radius, 2)); //using formula (Pi*r^2), using Math.PI for pi and Math.pow() to find square
}
//method to find volume of sphere
public static double sphereVolume(double radius)
{
return ((4.0/3.0) * Math.PI * Math.pow(radius, 3)); //using formula ((4/3)Pi*r^3), using Math.PI for pi and Math.pow() to find cube
}
//method to find area of traingle
public static double triangleArea(double base,double height)
{
return ((base*height)/2.0); //using formula b*h/2 for area of traingle
}
//method to find volume of pyramid
public static double pyramidVolume(double length,double width, double height)
{
return ((length*width*height)/3.0); //using fromula l*w*h/3
}
//method to find round
public static int round(double number)
{
/*using Math.floor to find the round of the number,
* we are adding 0.5 int the floor to round the number to closest integer
* because floor gives only the floor to make it work like floor as well as ceil ,
* we have to add 0.5 in it.
*/
return (int)Math.floor(number+ .5);
}
public static void main(String[] args) {
System.out.println("Testing circleArea()");
System.out.println(circleArea(2.3)); //should print 16.619025137490002
System.out.println(circleArea(5)); //should print 78.53981633974483
System.out.println(circleArea(7.8)); //should print 191.134497044403
System.out.println("Testing sphereVolume()");
System.out.println(sphereVolume(2.3)); //should print 50.965010421636
System.out.println(sphereVolume(5)); //should print 523.5987755982989
System.out.println(sphereVolume(7.8)); //should print 1987.798769261791
System.out.println("Testing triangleArea()");
System.out.println(triangleArea(2, 3)); //should print 3.0
System.out.println(triangleArea(3.4, 8.7)); //should print 14.79
System.out.println(triangleArea(3.0, 4.4)); //should print 6.6000000000000005
System.out.println("Testing pyramidVolume()");
System.out.println(pyramidVolume(2, 3, 4)); //should print 8.0
System.out.println(pyramidVolume(3.4, 8.7, 2.1)); //should print 20.706
System.out.println(pyramidVolume(3.0, 4.4, 2)); //should print 8.8
System.out.println("Testing round()");
System.out.println(round(2.3)); //should print 2
System.out.println(round(7.8)); //should print 8
System.out.println(round(6.0)); //should print 6
}
}
######################################################################
OUTPUT
#######################################################################
SCREENSHOT OF CODE