Java Arithmetic Operator convert feet into meters

Question

We would like to write a program that reads a number in feet.

Convert it to meters, and displays the result.

One foot is 0.305 meter. Here is a sample run:

import java.util.Scanner;

public class Main {

  public static void main(String[] Strings) {

    Scanner input = new Scanner(System.in);

    //your code here

  }/*from ww  w  .  ja  v a 2  s.c o m*/
}


import java.util.Scanner;

public class Main {

  public static void main(String[] Strings) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter a value for feet: ");
    double feet = input.nextDouble();
    double meters = feet * 0.305;
    System.out.println(feet + " feet is " + meters + " meters");

  }
}

Note

To define a method

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a value for feet: ");
    double feet = input.nextDouble();

    double meters = feetToMeters(feet);

    System.out.println(feet + " feet is " + meters + " meters");
  }//from www .j  av  a  2s . c  om

  private static double feetToMeters(double feet) {
    return feet * 0.305;
  }
}

Define a constant for converting.

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    // Create a Scanner object
    Scanner input = new Scanner(System.in);

    // Create constant value
    final double METERS_PER_FOOT = 0.305;

    // Prompt user to enter a number in feet
    System.out.print("Enter a value for feet: ");
    double feet = input.nextDouble();

    // Convert feet into meters
    double meters = feet * METERS_PER_FOOT;

    // Display results
    System.out.println(feet + " feet is " + meters + " meters");
  }// w  ww  .  j  a v  a2s.c om
}



PreviousNext

Related