Question

In: Computer Science

In this lab you will learn how to use methods from the Math class in order...

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
}

}

Solutions

Expert Solution

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


Related Solutions

Learn by Doing Matched Pairs: In this lab you will learn how to conduct a matched...
Learn by Doing Matched Pairs: In this lab you will learn how to conduct a matched pairs T-test for a population mean using StatCrunch. We will work with a data set that has historical importance in the development of the T-test. Paired T hypothesis test: μD = μ1 - μ2 : Mean of the difference between Regular seed and Kiln-dried seed H0 : μD = 0 HA : μD > 0 Hypothesis test results: Difference Mean Std. Err. DF T-Stat...
In Lab 4, you made a class with static methods. The static methods converted an integer...
In Lab 4, you made a class with static methods. The static methods converted an integer in binary, Hex, and Octal. To do this, you made a class of static methods. The class did not have any fields. The static methods received the integer value as a parameter. For this lab, make all of the static methods, non-static methods. Lab 5 Requirements Create a class named “Lab5_IntConv_Class Have one private field name intValue. Add two constructors: One is a default...
The Problem Statement In this lab section, you are going to learn how to sort an...
The Problem Statement In this lab section, you are going to learn how to sort an array of integers, and to sort an array of objects. We are going to divide the work into two parts. Part 1. Sorting an array of integers using selection sort In this part, you are given the code (see List 1) for sorting an array of integers into ascending order. The sorting method used is the selection sort. You can cut-and-paste the code into...
in math lab how to simulated heat conduction and how to show it in a video...
in math lab how to simulated heat conduction and how to show it in a video sinulation bottom code is a simulation of just the hear conduction without the video clear,pack,clc % material properties k=200 cp=921 rho=2500 alpha=k/(cp*rho); % introduce discretization constants % time delta_t=0.1; %space delta_L=0.01; % this is in meters % how many points N=100; % constants a1, and a2 a1=1-2*(alpha*delta_t)/(delta_L^2); a2=(alpha*delta_t)/(delta_L^2); % introduce initial and boundary conditions % initial condition T0=zeros(N,1); T0(1:floor(N/3))=30; % boundary condition % this...
In this lab assignment, students will demonstrate the abilities to: - Use functions in math module...
In this lab assignment, students will demonstrate the abilities to: - Use functions in math module - Generate random floating numbers - Select a random element from a sequence of elements - Select a random sample from a sequence of elements (Python Programming) NO BREAK STATEMENTS AND IF TRUE STATEMENTS PLEASE Help with the (create) of a program to play Blackjack. In this program, the user plays against the dealer. Please do the following. (a) Give the user two cards....
Need to use math lab for the Isim function for calculating the output and state response....
Need to use math lab for the Isim function for calculating the output and state response. Of the Transfer Function s + 2 / s^2 + 2 s + 2. I been using the following code and it doesn’t work sys = tf([1 2],[1 2 2]) t = [0:0.01:]; u=0*t; [y,T,x] = lsim(sys,u,t) subplot(121), plot(T,x(:,1)) xlabel('Time(s)'),ylabel('x_1') subplot(122),plot(T,x(:,2)) xlabel('Time(s)'),ylabel('x_2')
Discuss something you can learn from Finance class, such as theory, method or view
Discuss something you can learn from Finance class, such as theory, method or view
No Excel Please! I am trying to learn the math for the exam! Thank you!! You...
No Excel Please! I am trying to learn the math for the exam! Thank you!! You are 35 years old today and are considering your retirement needs. You expect to retire at age 65 (in 30 years) and you plan to live to age 99. You want to buy a house costing $300,000 on your 65th birthday and your living expenses will be $30,000 a year after that (starting at the end of year 65 and continuing through the end...
No excel please! I am trying to learn the math for Exam! Thank yoU! You have...
No excel please! I am trying to learn the math for Exam! Thank yoU! You have just joined the investment banking firm of Mckenzie & Co. They have offered you two different salary arrangements. You can have $75,000 per year for the next two years, or you can have $55,000 per year for the next two years, along with a $30,000 signing bonus today. If the interest rate is 12% compounded monthly, which is a better offer? NB: first convert...
4. You later learn that people from the ‘class’ group from Q3 had classmates who had...
4. You later learn that people from the ‘class’ group from Q3 had classmates who had also tried Duolingo. (IE these classmates had started in a ‘class only’ group, and then added ‘duolingo’). The data for these people are provided below. 4a. State the null and alternative hypotheses (1 point) 4b. Calculate the test statistic (3 points); and state your conclusion (use the same alpha from Q3) (1 point) 4c. What are some reasons why your conclusions from Q3 and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT