Colorado State University Logo | Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3) Colorado State University Logo | Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)
Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)
Computer Science

Lab 11 - Shape Calculator (Optional)

Introduction

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?

Step 6 - Turning in

Once you have run the program and are satisfied it is working, you may submit for grading. Note that you only get five submission attempts, so make sure it is mostly working before you submit. We will bypass the main method in our tests, running our own tests on each method you wrote.

Computer Science Department

279 Computer Science Building
1100 Centre Avenue
Fort Collins, CO 80523
Phone: (970) 491-5792
Fax: (970) 491-2466

Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)

Survey of computer science, formal logic, and computational thinking. Explores the historical, gender, and cultural perspectives on the role of technology in society. Includes learning a basic programming language. Students will be expected to write small programs, and construct written arguments on ways in which technology influences our modern culture. Previous computer science experience not necessary.