Java Bootcamp – Workbooks and Challenges
)}

Workbook 4.5

From Java Bootcamp Resources -> Module 1 -> 4. Functions, open Workbook 4.5.

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² */

Area=side²Area = side² 68747470733a2f2f666972656261736573746f726167652e676f6f676c65617069732e636f6d2f76302f622f6c6561726e746865706172742d37356165642e61707073706f742e636f6d2f6f2f696d6167657325324662313461363061642d623136302d346637612d396430302d63656637636237656264.png

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.

Area=lengthwidthArea = length * width

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:

  1. Check if the length OR width is negative. If so, print "Error: impossible" and terminate the java program.

  2. 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


Area=baseheight÷2Area = base * height \div 2

/** * 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


Area=πradius²Area = π * radius²

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

Good Luck!


Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.