Workbook 4.5
From the Java Bootcamp Resources
, launch the Workbook 4.5
folder.
Area Calculator
In this workbook, you will calculate the area of a square, rectangle, triangle, and circle.
Task 1: area of a square
- Function name:
/** * Name: areaSquare <------ * @param side (double) * @return the area (double) * * Inside the function: * 1. If side is negative * - prints "Impossible" * - shuts the app down * 2. Calculates the area of the square. A = side² */
- Parameters:
/** * Name: areaSquare * @param side (double) <-------- * @return the area (double) * * Inside the function: * 1. If side is negative * - prints "Impossible" * - shuts the app down * 2. Calculates the area of the square. A = side² */
- Return value:
/** * Name: areaSquare * @param side (double) * @return the area (double) <-------- * * Inside the function: * 1. If side is negative * - prints "Impossible" * - shuts the app down * 2. Calculates the area of the square. A = side² */
- Inside the function:
/** * Name: areaSquare - returns the area of a square. * @param side (double) * @return the area (double) * * Inside the function: <--------- * 1. If side is negative * - prints "Impossible" * - shuts the app down * 2. Calculates the area of the square. A = side² */
Use a Math
function to calculate side to the power of 2.
Task 2: area of a rectangle
Write a function that calculates the area of a rectangle.
1. Function name.
/** * Name: areaRectangle <------ * @param length (double). * @param width (double). * @return the area (double) * * Inside the function: * 1. If the length OR width is negative * - print "Error: impossible" and * - terminate the program. * 2. return the area: length * width */
2. Parameters.
/** * Name: areaRectangle * @param length (double). <----- * @param width (double). <----- * @return the area (double) * * Inside the function: * 1. If the length OR width is negative * - print "Error: impossible" and * - terminate the program. * 2. return the area: length * width */
3. Return value.
/** * Name: areaRectangle * @param length (double). * @param width (double). * @return the area (double) <------ * * Inside the function: * 1. If the length OR width is negative * - print "Error: impossible" and * - terminate the program. * 2. return the area: length * width */
Inside the function:
-
Check if the length OR width is negative. If so, print "Error: impossible" and terminate the java program.
-
Then, calculate the area of the rectangle and return it.
/** * Name: areaRectangle * @param length (double). * @param width (double). * @return the area (double) * * Inside the function: <----- * 1. If the length OR width is negative * - print "Error: impossible" and * - terminate the program. * 2. return the area: length * width */
Task 3: area of a triangle
/** * Name: areaTriangle * @param base: (double). * @param height: (double). * @return the area (double) * * Inside the function: * 1. If the base OR height is negative * - print "Error: impossible" * - terminate the program. * 2. return the area: base * height / 2 */
Task 4: area of a circle
How do you get PI? You could set a double
equal to 3.14159... but, please don't do that. You can get PI from the Math
class. I encourage you to look it up.
Then, write a function that calculates the area of a circle.
/** * Name: areaCircle * @param radius (double). * @return area (double) * * Inside the function: * 1. If the radius is negative * - print: Impossible * - terminates the program * 2. returns the area: π * radius2 */
Task 5: Calculate the areas
Using the appropriate function, calculate the area for:
-
a square with a side of 2 cm.
-
a rectangle with a length of 1 cm, and a width of 2 cm.
-
a triangle with a base of 1 cm, and a height of 2 cm.
-
a circle with a radius of 2 cm.
// double square = area of square with a side of 2. // double rectangle = area of rectangle with length: 1, width: 2. // double triangle = area of triangle with base: 1, height: 2. // double circle = area of circle with a radius of 2.
Task 6: Write a function that prints every area
Instead of cluttering main()
with 4 println
statements, write a function that prints every area.
/** * Name: printAreas -- it prints four areas * @param square (double) * @param rectangle (double) * @param triangle (double) * @param circle (double) * * Inside the function: * 1. print: ("Square area: <square area>") * 2. print: ("Rectangle area: <rectangle area>") * 3. print: ("Triangle area: <triangle area>") * 4. print: ("Circle area: <circle area>") * */
Task 7: Call the printAreas
function.
From main()
, call the printAreas function.
Run your code
Thank you for using the area calculator
This calculator lets you get the area of:
1 -- Square
2 -- Rectangle
3 -- Triangle
4 -- Circle
Square area: 4.0
Rectangle area: 2.0
Triangle area: 1.0
Circle area: 12.5663706144
Visualizing the Runtime
After you solve this workbook, I still recommend watching the video solution on Udemy.
It will show you how to visualize the runtime using Visual Studio Code.