Memaparkan catatan dengan label Java. Papar semua catatan
Memaparkan catatan dengan label Java. Papar semua catatan

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);
    }

}



Displaying Patterns

05 November 2011
Write a method to display a patter as follow:
1
            2 1
          3 2 1
...
n n-1 ... 3 2 1

The method header is

public static void displayPattern(int n)


Input:
9

Output:
1
       21
      321
     4321
    54321
   654321
  7654321
 87654321
987654321

Main.java

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

package displayingpatterns;

import java.util.Scanner;

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);

        displayPattern(scan.nextInt());
    }

    public static void displayPattern(int n)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = n; j > i; j--)
            {
                sop(" ");
            }

            for (int j = i; j > 0; j--)
            {
                sop("" + j);
            }

            sop("\n");
        }
    }

    public static void sop(String str)
    {
        System.out.print(str);
    }

}


Sorting Three Numbers

04 November 2011
Write the following method to display three numbers in decreasing order:

public static void displaySortedNumbers(double num1, double num2, double num3)


Input:
5678 1234 6789

Output:
6789.0
5678.0
1234.0

Main.java

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

package sortingthreenumbers;

import java.util.Scanner;

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);
        double num1 = scan.nextDouble();
        double num2 = scan.nextDouble();
        double num3 = scan.nextDouble();

        displaySortedNumbers(num1, num2, num3);
    }

    public static void displaySortedNumbers(double num1, double num2, double num3)
    {
        double temp;

        while (num1 < num2 || num2 < num3)
        {
            temp = num1;
            num1 = num2;
            num2 = temp;

            while (num2 < num3)
            {
                temp = num2;
                num2 = num3;
                num3 = temp;
            }
        }

        sopl("");
        sopl("" + num1);
        sopl("" + num2);
        sopl("" + num3);
    }

    public static void sopl(String str)
    {
        System.out.println(str);
    }

}



Summing int digits in an interger

Write a method that computes the sum of the digits in an interger. Use the following method header:

public static int sumDigits(long n)


For example, sumDigits(234) return 9 (2 + 3 + 4).

Input:
234

Output:
9

Main.java

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

package summingthedigitsinaninterger;

import java.util.Scanner;

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);
       
        long number = scan.nextInt();
        sopl(sumDigits(number) + "\n");
    }

    public static void sopl(String str)
    {
        System.out.println(str);
    }

    public static int sumDigits(long digit)
    {
        int num;
        int sum = 0;
       
        while (digit != 0)
        {
            num = (int) digit % 10;
            digit = digit / 10;
            sum = sum + num;
        }

        return sum;
    }

}


Math: Triangular Numbers

03 November 2011
A triangulat number is define as 1 + 2 + 3 + ... + n for n = 1, 2, ..., and so on. So, the first few numbers are 1, 3, 6, 10, ... Write the following method that return a triangular number:


public static int getTriangularNumber(int n)


Write test program that displays the first 100 triangular number with 10 numbers on each line.

Input:
100

Ouput:
1 3 6 10 15 21 28 36 45 55
66 78 91 105 120 136 153 171 190 210
231 253 276 300 325 351 378 406 435 465
496 528 561 595 630 666 703 741 780 820
861 903 946 990 1035 1081 1128 1176 1225 1275
1326 1378 1431 1485 1540 1596 1653 1711 1770 1830
1891 1953 2016 2080 2145 2211 2278 2346 2415 2485
2556 2628 2701 2775 2850 2926 3003 3081 3160 3240
3321 3403 3486 3570 3655 3741 3828 3916 4005 4095
4186 4278 4371 4465 4560 4656 4753 4851 4950 5050

Main.java

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

package triangularnumbers;

import java.util.Scanner;

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);

        int number = scan.nextInt();

        sop("\n");

        for (int i = 0; i < number; i += 10)
        {
            for (int j = 1; j <= 10; j++)
            {
                sop(getTriangularNumber(i + j) + " ");
            }
            sop("\n");
        }
    }

    public static int getTriangularNumber(int n)
    {
        return (n * (n + 1)) / 2;
    }

    public static void sop(String str)
    {
        System.out.print(str);
    }
}