Conversions between inches and centimeters

15 November 2011
Write a class that contains the following two method:


/** Converts from inches to centimeters */

public static double inchToCentimeter(double in)

/** Converts from centimeters to inches */
public static double centimeterToInch(double cm)


The formula for the conversion is:

centimeters = 2.54 x inches

Write a test program that invokes these methods to display the following tables:

Inches     Centimeters     Centimeters     Inches
 
1.0        2.54            5.0             1.96
2.0        5.08            10.0            3.93
...
9.0        22.86           45.0            17.71
10.0       25.4            50.0            19.68

Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package conversionbetweeninchesandcentimeters;

/**
 *
 * @author dekwan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        sop("Inches    Centimeters    Centimeters    Inches\n");

        // SOP Inches to Centimeters
        for (int i = 1; i <= 10; i++)
        {
            sop("" + i);
            sop("         " + inchToCentimeter(i));
            sop("            " + i * 5);
            sop("             " + centimeterToInch(i * 5) + "\n");
        }
    }

    /** Converts from inches to centimeters */
    public static double inchToCentimeter(double in)
    {
        return in * 2.54;
    }

    /** Converts from centimeters to inches */
    public static double centimeterToInch(double cm)
    {
        return cm / 2.54;
    }

    private static void sop(String string)
    {
        System.out.print(string);
    }

}



0 comments:

Catat Ulasan

Terima kasih kerana memberi ulasan...