Back

import java.util.*;
public class example2 {

   public static void main(String[] args)
   {
      //Variable declarations
      double area_of_square, length_of_side, radius, area_of_circle;

      //Input
      System.out.println("Example 2:");

      System.out.println("Enter the area of the square: ");
      //readDouble will be a different method that we will create to read in from the keyboard 
      area_of_square = readDouble();

      System.out.println("Enter the radius of the circle: ");
      radius = readDouble();

      //Calculations
      //Area of a square is A=L^2 therefore L= SQRT(A)
      length_of_side = Math.sqrt(area_of_square);
      //Area of a circle is A=PI*r^2
      area_of_circle = Math.PI * Math.pow(radius,2);

      //Output
      System.out.println("A square with the area of " + area_of_square +
            " has the length of each side equal to " + length_of_side);
      System.out.println("A circle with the radius " + radius + " has an area of " + area_of_circle);
   }

   public static double readDouble()
   {
      Scanner sc = new Scanner(System.in);
      double x = sc.nextDouble();
      return x;
   }
}

Top