8 20 D Wed Nov 9 113021 AM E Chrome File Edit View History B

8 20% D Wed Nov 9 11:30:21 AM E Chrome File Edit View History Bookmarks People Window Help Lab Assignments Mod 6-Ch6 Programming Principles l Section 16 Fall Semester 2016 CO kennesaw.view.usg.edu /d21/le/content/1 103674 fullscreen/18397105/View Lab Assignments Mod 6-Ch6 Program 3 Design and implement a Java program (name it ComputeAreas) that defines three methods as follows: Method squareArea (double side) determines and returns the area of a square Method rectangleArea (double width, double length) determines and returns the area of a rectangle Method circleArea (double radius) determines and returns the area of a circle Method triangleArea (double base, double height) determines and returns the area of a triangle. Test the methods with different input value read from the user (in the main method) Design the main method of your program such that it allows the user to re-run the program with different inputs (i.e use a loop structure). Document your code, and organize and space the outputs properly. Use escape characters to organize the outputs. Sample output is Square Side 5.1 26.01 Square Area Rectangle Width 4.0 Rectangle Length 5.5 22.0 Rectangle Area Circle Radius 2.5 19.625 Circle Area View a Text

Solution

ComputeAreas.java

import java.text.DecimalFormat;


public class ComputeAreas {
   public static void main(String[] args) {
       DecimalFormat df = new DecimalFormat(\"0.00\");
       java.util.Scanner scan = new java.util.Scanner(System.in);
       for(;;){
       System.out.println(\"Enter the side of square: \");
       double side = scan.nextDouble();
       System.out.println(\"Square area: \"+df.format(squareArea(side)));
       System.out.println(\"Enter the width of rectangle: \");
       double width = scan.nextDouble();
       System.out.println(\"Enter the length of rectangle: \");
       double length = scan.nextDouble();
       System.out.println(\"Rectablge area: \"+df.format(rectangleArea(width, length)));
      
       System.out.println(\"Enter the radius of circle: \");
       double radius = scan.nextDouble();
       System.out.println(\"Circle area: \"+df.format(circleArea(radius)));
       System.out.println(\"Enter the base of triangle: \");
       double base = scan.nextDouble();
       System.out.println(\"Enter the height of triangle: \");
       double height = scan.nextDouble();
       System.out.println(\"Triangle area: \"+df.format(triangleArea(base, height)));
       System.out.println(\"Do you want to continue(y/n): \");
       char ch = scan.next().charAt(0);
       if(ch == \'n\'||ch==\'N\'){
           break;
       }
       }
   }
   public static double squareArea(double side){
       return side * side;
   }
   public static double circleArea(double radius){
       return Math.PI * radius * radius;
   }
   public static double rectangleArea(double width, double length){
       return width * length;
   }
   public static double triangleArea(double base , double height){
       return (height * base)/2;
   }
}

Output:

Enter the side of square:
5.1
Square area: 26.01
Enter the width of rectangle:
4.0
Enter the length of rectangle:
5.5
Rectablge area: 22.00
Enter the radius of circle:
2.5
Circle area: 19.63
Enter the base of triangle:
6.4
Enter the height of triangle:
3.6
Triangle area: 11.52
Do you want to continue(y/n):
n

 8 20% D Wed Nov 9 11:30:21 AM E Chrome File Edit View History Bookmarks People Window Help Lab Assignments Mod 6-Ch6 Programming Principles l Section 16 Fall S
 8 20% D Wed Nov 9 11:30:21 AM E Chrome File Edit View History Bookmarks People Window Help Lab Assignments Mod 6-Ch6 Programming Principles l Section 16 Fall S

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site