Java final keyword calculate circle area with constant

Question

We would like to calculate circle area with constant

Code structure you can use:

import java.util.Scanner; // Scanner is in the java.util package

public class Main {
  public static void main(String[] args) {
    // Create a Scanner object
    Scanner input = new Scanner(System.in);
    /*from   w ww .ja  v a2s.  c  o  m*/
    // Prompt the user to enter a radius
    System.out.print("Enter a number for radius: ");
    double radius = input.nextDouble();

    // Compute area
    double area = radius * radius * PI;

    // Display result
    System.out.println("The area for the circle of radius " +
      radius + " is " + area);
  } 
}


import java.util.Scanner; // Scanner is in the java.util package

public class Main {
  public static void main(String[] args) {
    final double PI = 3.14159; // Declare a constant
    
    // Create a Scanner object
    Scanner input = new Scanner(System.in);
    
    // Prompt the user to enter a radius
    System.out.print("Enter a number for radius: ");
    double radius = input.nextDouble();

    // Compute area
    double area = radius * radius * PI;

    // Display result
    System.out.println("The area for the circle of radius " +
      radius + " is " + area);
  } 
}

Note

You can use final to mark a variable as constant in Java.




PreviousNext

Related