วันศุกร์ที่ 31 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรมร้านเช่ารถมอเตอร์ไซต์

ตัวอย่างโค้ดโปรแกรมร้านเช่ารถมอเตอร์ไซต์

ตัวอย่างโค้ดโปรแกรมนี้ เป็นตัวอย่างโค้ดโปรแกรมร้านเช่ามอเตอร์ไซต์ โดยกรอกจำนวนมอเตอร์ไซต์ที่ต้องการเช่า จากนั้นโปรแกรมจะวนลูปแสดงมอเตอร์ไซต์ และราคา ซึ่งในที่นี้จะฟิกไว้ที่ ชื่อ Car และราคา 2000 บาททุกคัน จากนั้นจะแสดงผลรวมทั้งหมด

ตัวอย่างโค้ดโปรแกรม

import java.math.BigInteger;
import java.util.Scanner;

public class JavaCodeExam {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input Number Of Car : ");
int carNumber = in.nextInt();
BigInteger sumPrice = new BigInteger("0");
System.out.println("No\t\tName\t\tPrice");
for (int i = 1; i <= carNumber; i++) {
System.out.println(i + "\t\tCar\t\t2000");
sumPrice = sumPrice.add(BigInteger.valueOf(2000));
}
System.out.println("Sumprice = " + sumPrice);
}
}

yengo หรือ buzzcity

วันพฤหัสบดีที่ 30 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรมการแปลงเลขฐาน 10 เป็นเลขฐาน 2

ตัวอย่างโค้ดโปรแกรมการแปลงเลขฐาน 10 เป็นเลขฐาน 2

ตัวอย่างโค้ดโปรแกรมการแปลงเลขฐาน โดยการแปลงเลข 10 เป็นเลขฐาน 2

ตัวอย่างโค้ด

import java.util.ArrayList;
import java.util.List;

public class JavaCodeExam {
public static void main(String[] args) {
int a10 = 84;
List<Integer> ans = new ArrayList<Integer>();
do{
int bit = a10 % 2;
a10 = a10 / 2;
ans.add(bit);
}while(a10 > 0);

for (int i = ans.size(); i > 0; i--) {
System.err.print(ans.get(i - 1));
}
}
}

yengo หรือ buzzcity

วันพุธที่ 29 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรมแปลงเลขฐาน 2 เป็นเลขฐาน 10

ตัวอย่างโค้ดโปรแกรมแปลงเลขฐาน 2 เป็นเลขฐาน 10

ตัวอย่างโค้ดโปรแกรมนี้ เป็นตัวอย่างโปรแกรมที่แปลงโปรแกรมจากเลขฐาน 2 เป็นเลขฐาน 10

ตัวอย่างโค้ด

public class CodeFromDoesystem {
public static void main(String[] args) throws Exception {
int a2 = 1010111;
String a2Str = a2 + "";
int sum = 0;
for (int i = 0; i < a2Str.length(); i++) {
int base = a2 % 10;
a2 = a2 / 10;
int pow = (int) Math.pow(2, i);
System.out.println(base + " x " + pow + " = " + (base * pow));
sum += base * pow;
}
System.out.println("Answer is " + sum);
}
}

ผลลัพธ์ที่ได้

1 x 1 = 1
1 x 2 = 2
1 x 4 = 4
0 x 8 = 0
1 x 16 = 16
0 x 32 = 0
1 x 64 = 64
Answer is 87

yengo หรือ buzzcity

วันอังคารที่ 28 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรมบวก Matrix 2 มิติ

ตัวอย่างโค้ดโปรแกรมบวก Matrix 2 มิติ

มาดูตัวอย่างโค้ดสำหรับการบวก Matrix 2 มิติกันครับ ซึ่งการบวกนั้น ก็นำแถวเดียวกัน คอลัมน์เดียวกัน มาบวกกัน

ตัวอย่างโค้ด

public class CodeFromDoesystem {
public static void main(String[] args) throws Exception {
int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
int[][] b = { { 2, 3, 4 }, { 5, 6, 7 }, { 8, 9, 10 }, { 11, 12, 13 } };
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.println("row " + (i + 1) + " col " + (j + 1) + " : " + (a[i][j] + b[i][j]));
}
}
}
}

ผลลัพธ์ที่ได้

row 1 col 1 : 3
row 1 col 2 : 5
row 1 col 3 : 7
row 2 col 1 : 9
row 2 col 2 : 11
row 2 col 3 : 13
row 3 col 1 : 15
row 3 col 2 : 17
row 3 col 3 : 19
row 4 col 1 : 21
row 4 col 2 : 23
row 4 col 3 : 25

yengo หรือ buzzcity

วันจันทร์ที่ 27 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรม แสดงค่าจาก Array 2 มิติ

ตัวอย่างโค้ดโปรแกรม แสดงค่าจาก Array 2 มิติ

มาดูตัวอย่างโค้ดโปรแกรมที่แสดงค่าจาก Array 2 มิติกันครับ ซึ่งจะเป็นการแสดงแถว และคอลัมน์ ของ Array ออกมาทั้งหมด

ตัวอย่างโค้ด

public class CodeFromDoesystem {
public static void main(String[] args) throws Exception {
int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.println("row " + (i + 1) + " col " + (j + 1) + " : " + a[i][j]);
}
}
}
}

ผลลัพธ์ที่ได้

row 1 col 1 : 1
row 1 col 2 : 2
row 1 col 3 : 3
row 2 col 1 : 4
row 2 col 2 : 5
row 2 col 3 : 6
row 3 col 1 : 7
row 3 col 2 : 8
row 3 col 3 : 9
row 4 col 1 : 10
row 4 col 2 : 11
row 4 col 3 : 12

yengo หรือ buzzcity

วันอาทิตย์ที่ 26 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรม โปรแกรมคำนวณพื้นที่วงกลม

ตัวอย่างโค้ดโปรแกรม โปรแกรมคำนวณพื้นที่วงกลม

เรามาดูตัวอย่างโค้ดโปรแกรมคำนวณพื้นที่วงกลมกันครับ ซึ่งการหาพื้นที่วงกลมนั้น มีสูตรว่า p

ตัวอย่างโค้ด


import java.util.Scanner;

public class CodeFromDoesystem {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
System.out.print("Input Circle Radius : ");
double radius = in.nextDouble();
double result = Math.PI * radius * radius;
System.out.println("The Circle Area = " + result);
}
}

yengo หรือ buzzcity

วันเสาร์ที่ 25 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรม โปรแกรมหาเส้นรอบวงสี่เหลี่ยม

ตัวอย่างโค้ดโปรแกรม โปรแกรมหาเส้นรอบวงสี่เหลี่ยม

การหาเส้นรอบวงสี่เหลี่ยมนั้น มีสูตรว่า 2 * (ความกว้าง + ความยาว) ดังนั้น โปรแกรมจึงรับค่าความกว้างกับความยาวสองค่า

ตัวอย่างโค้ด

import java.util.Scanner;

public class CodeFromDoesystem {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
System.out.print("Input width : ");
double width = in.nextDouble();
System.out.print("Input length : ");
double length = in.nextDouble();
System.out.println("-----------------------");
System.out.println("The Square Perimeter : " + 2 * (width + length));
}
}

yengo หรือ buzzcity

วันศุกร์ที่ 24 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรม แม่สูตรคูณ

ตัวอย่างโค้ดโปรแกรม แม่สูตรคูณ

มาดูตัวอย่างโปรแกรม แสดงแม่สูตรคูณ โดยกรอกตัวเลขแม่สูตรคูณเข้าไป แล้วโปรแกรมจะแสดง แม่สูตรคูณ

ตัวอย่างโค้ด


import java.util.Scanner;

public class CodeFromDoesystem {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
System.out.print("input multiplication : ");
Long multiplication = in.nextLong();
for (int i = 1; i <= 12; i++) {
System.out.println(multiplication + " x " + i + " = " + multiplication*i);
}
}
}

yengo หรือ buzzcity

วันพฤหัสบดีที่ 23 มกราคม พ.ศ. 2557

ตัวอย่างโปรแกรมจำลองตู้ ATM การถอนเงิน ฝากเงิน โดยใช้ Java

ตัวอย่างโปรแกรมจำลองตู้ ATM การถอนเงิน ฝากเงิน โดยใช้ Java

เรามาดูตัวอย่างโค้ดโปรแกรมจำลองตู้ ATM โดยจะมีการฝากเงิน การถอนเงิน และการเช็คเงิน 

ตัวอย่างโค้ดโปรแกรม


public class ATM {
    private double money;
    public ATM(double money) { //สร้าง Construct โดยกำหนดเงินเริ่มต้น
        this.money = money;
    }
    public static void main(String[] args) {
        ATM accou = new ATM(3000); //สร้างออบเจ็ค ATM โดยให้เงินเริ่มต้นที่ 3000
        System.out.println(accou.checkmoney()); //เช็คเงินว่ามีเท่าไหร่ ซึ่งค่าที่ได้ก็คือ 3000
        accou.Deposit(1000); //ฝากเงินเป็นจำนวน 1000
        System.out.println(accou.checkmoney()); //เงินที่มีเท่ากับ 4000
        accou.drawout(1500); //ถอนเงิน 1500
        System.out.println(accou.checkmoney()); //เงินที่มีเท่ากับ 2500
    }
    public void Deposit(double inmoney){ //ฟังก์ชันสำหรับการฝากเงิน
        this.money += inmoney;
    }
    public void drawout(double demoney){ //ฟังก์ชันสำหรับการถอนเงิน
        if(this.money < demoney){ //เช็คว่ายอดเงินที่มี น้อยกว่ายอดเงินที่ต้องการถอนหรือไม่
            System.out.println("กรุณากรอกจำนวนเงินที่ต้องการถอนให้ถูกต้อง");
        }
        else{
            this.money -= demoney;
        }
    }
    public double checkmoney(){ //ฟังก์ชันสำหรับการเช็คยอดเงิน
        return this.money;
    }
}

yengo หรือ buzzcity

วันพุธที่ 22 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรมคำนวณค่าจอดรถ

ตัวอย่างโค้ดโปรแกรมคำนวณค่าจอดรถ

ต้องการเขียนโปรแกรมโดยมีเงื่อนไขดังนี้

- มีการส่งพารามิเตอร์ดังนี้ double xxx(Date TimeIn , Date TimeOut)
- 3 ชั่วโมงแรกคิดค่าจอด 20 บาท ต่อไปชั่วโมงละ 10 บาท ถ้าเกิน 30 นาทีปัดขึ้น น้อยกว่าปัดลง
- ถ้าเกิน 24 ชั่วโมง คิดค่าจอดเป็นวัน วันละ 500 บาท เศษที่เกินคิดตาม Concept ข้างต้น

import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class parking {
    public static void main(String[] args) throws ParseException {
        Scanner in = new Scanner(System.in);
        DateFormat formatT = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
     
        System.out.print("TimeIn(yyyy-MM-dd-HH-mm) : ");
        String fromin = in.nextLine();
        Date din = formatT.parse(fromin);
     
        System.out.print("TimeOut(yyyy-MM-dd-HH-mm) : ");
        String fromout = in.nextLine();
        Date dout = formatT.parse(fromout);

        System.out.println("You will have to pay : " + kidTang(din, dout) + " Baht");
    }

    public static double kidTang(Date datein, Date dateout){
        double sum = 0;
        double milliTime = dateout.getTime() - datein.getTime();

        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(0);
        nf.setRoundingMode(RoundingMode.HALF_UP);
        int hour = Integer.parseInt(nf.format(milliTime/(1000*60*60)));

        sum = sum + (hour/24)*500;
        hour = hour%24;

        if(hour <= 3) sum = sum + hour*20;
        else sum = sum + 3*20 + (hour-3)*10;

        return sum;
    }
}

yengo หรือ buzzcity

วันอังคารที่ 21 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรมที่หาว่ามีตัวเลขคู่ใน Array กี่ตัว

ตัวอย่างโค้ดโปรแกรมที่หาว่ามีตัวเลขคู่ใน Array กี่ตัว

ตัวอย่างโปรแกรมหาเลขคู่ใน Array ตัวอย่างโค้ดด้านล่าง

ตัวอย่างโค้ด

int[] exAr = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
int ans = 0;
for (int i = 0; i < exAr.length; i++) {
if(exAr[i]%2 == 0) {
ans += 1;
}
}
System.out.println("Even number has " + ans);

ตัวอย่างนี้จะดึงข้อมูลจาก Array ออกมา แล้วดูว่าหารสองลงตัวหรือเปล่า ถ้าหารสองลงตัวก็แสดงว่าเป็นเลขคู่

yengo หรือ buzzcity

วันจันทร์ที่ 20 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรม Reverse ข้อความ

ตัวอย่างโค้ดโปรแกรม Reverse ข้อความ

Reverse ก็คือการสลับตัวอักษร จากหน้าไปหลัง จากหลังไปหน้า

ตัวอย่างโค้ด

import java.util.Scanner;

public class MessageReverse {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String origin = in.nextLine();
        System.out.println(useStrBuilder(origin));
    }
    public static String useStrBuilder(String origin){
        if(origin.equals("")) return "";
        StringBuilder toBuilder = new StringBuilder(origin);
        return toBuilder.reverse().toString();
    }
}

ตัวอย่างโค้ดอีกแบบ

import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class MessageReverse {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String origin = in.nextLine();
        System.out.println(useCollection(origin));
    }
    public static String useCollection(String origin){
        if(origin.equals("")) return "";
        String[] toReverse = origin.split("");
        Collections.reverse(Arrays.asList(toReverse));
        return Arrays.toString(toReverse).replace(", ", "").replace("[", "").replace("]", "");
    }
}

yengo หรือ buzzcity

วันอาทิตย์ที่ 19 มกราคม พ.ศ. 2557

เขียนโปรแกรม หาตัวเลขฟีโบนัชชี (fibonacci)

เขียนโปรแกรม หาตัวเลขฟีโบนัชชี (fibonacci)

เรามาเขียนโปรแกรมหาตัวเลขฟีโบนัชชีกันครับ 

จำนวนฟีโบนัชชีคืออะไร

จำนวนฟีโบนัชชี หรือ เลขฟีโบนัชชี (Fibonacci number) คือลำดับของจำนวนเต็ม โดยมีนิยามของความสัมพันธ์ว่า จำนวนถัดไปเท่ากับผลบวกของจำนวนสองจำนวนก่อนหน้า และสองจำนวนแรกก็คือ 0 และ 1 ตามลำดับ

ตัวอย่างโค้ดโปรแกรม

import java.util.Scanner;

public class Fibonacci {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Input First Fibonacci : ");
        long first = in.nextLong();
        System.out.print("Input Second Fibonacci : ");
        long second = in.nextLong();
        System.out.println("Third Fibonacci is " + (first + second));
    }
}

ตัวอย่างนี้เป็นตัวอย่างการหาตัวเลขฟีโบนัชชีตัวถัดไป

ตัวอย่างโค้ดโปรแกรม

import java.util.Scanner;

public class Fibonacci {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Input Number For Max Fibonacci : ");
        long max = in.nextLong();

        int first = 0;
        int second = 1;
        int fibonac;
        System.out.print("0 1");
        while(true){
            fibonac = first + second;
            first = second;
            second = fibonac;
            if(fibonac > max) {
                System.out.println();
                break;
            }
            System.out.print(" " + fibonac);
        }
    }
}

ตัวอย่างนี้เป็นตัวอย่างที่แสดงตัวเลขฟีโบนัชชีตามที่กำหนด

yengo หรือ buzzcity

วันเสาร์ที่ 18 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรม การเพิ่มวัน

ตัวอย่างโค้ดโปรแกรม การเพิ่มวัน

ตัวอย่างนี้เป็นตัวอย่างการเพิ่มวันขึ้นจากเดิม

ตัวอย่างโค้ดโปรแกรม

import java.util.Date;

public class DateIndex {
    public static void main(String[] args){
        Date date = new Date(2012-1900, 8, 1);
        System.out.println(date); // output Sat Sep 01 00:00:00 ICT 2012
        long msec = date.getTime();
        msec += 3 * 24 * 60 * 60 * 1000L;
        date.setTime(msec);
        System.out.println(date); // output Tue Sep 04 00:00:00 ICT 2012
    }
}

จากตัวอย่าง เป็นการเพิ่มวันขึ้นอีก 3 วัน

yengo หรือ buzzcity

วันศุกร์ที่ 17 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรมที่เช็คว่าวันแรกกับวันที่สอง ต่างกันกี่วัน

ตัวอย่างโค้ดโปรแกรมที่เช็คว่าวันแรกกับวันที่สอง ต่างกันกี่วัน

ตัวอย่างนี้เป็นการเช็คความต่างระหว่างวัน โดยเช็คว่า วันแรกกับวันที่สอง ต่างกันกี่วัน

import java.util.Date;

public class DateIndex {
    public static void main(String[] args){
        Date epoch = new Date(2012-1900, 8, 1);
        Date today = new Date(2012-1900, 8, 4);
        
        long diff = today.getTime() - epoch.getTime();
System.out.println("ทั้งสองวันแตกต่างกัน " + (diff / (1000*60*60*24)) + " วัน"); 
// output ทั้งสองวันแตกต่างกัน 3 วัน
    }
}

yengo หรือ buzzcity

วันพฤหัสบดีที่ 16 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดเปรียบเทียบวันสองวัน ว่า วันแรก เท่ากับ ก่อน หรือหลัง วันที่สอง

ตัวอย่างโค้ดเปรียบเทียบวันสองวัน ว่า วันแรก เท่ากับ ก่อน หรือหลัง วันที่สอง

ตัวอย่างนี้เป็นการเปรียบเทียบ โดยมีวันสองวัน แล้วนำมาเปรียบเทียบว่า วันแรก เป็นวันเดียวกัน เป็นวันก่อนหน้า หรือว่าเป็นวันหลังวันที่สอง

ตัวอย่างโค้ดโปรแกรม

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateIndex {
    public static void main(String[] args) throws ParseException{
        DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
        Date d1 = df.parse("2555-01-01");
        Date d2 = df.parse("2555-01-02");
        String relation = null;
        if (d1.equals(d2)) relation = "the same date as";
        else if (d1.before(d2)) relation = "before";
        else if (d1.after(d2)) relation = "after";
        System.out.println(d1 + " is " + relation + " " + d2);
    }
}

yengo หรือ buzzcity

วันพุธที่ 15 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรมการปัดเศษ และจำนวนของทศนิยม

ตัวอย่างโค้ดโปรแกรมการปัดเศษ และจำนวนของทศนิยม

ตัวอย่างโค้ดต่อไปนี้เป็นตัวอย่างการปัดเศษของทศนิยม และการกำหนดจำนวนของทศนิยม โดยการใช้ BigDecimal กับ RoundingMode

ตัวอย่างโปรแกรม

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalIndex {
    public static void main(String[] args) {
        BigDecimal bd = new BigDecimal("123456789.0163456890");
        System.out.println(bd.setScale(3, RoundingMode.UP));
        // output = 123456789.017
        
        System.out.println(bd.setScale(3, RoundingMode.DOWN));
        // output = 123456789.016
        
        BigDecimal bd2 = new BigDecimal("6.5");
        System.out.println(bd2.setScale(0, RoundingMode.HALF_DOWN));
        // output = 6
        System.out.println(bd2.setScale(0, RoundingMode.HALF_UP));
        // output = 7
        
        BigDecimal bd3 = new BigDecimal("6.5");
        System.out.println(bd3.setScale(0, RoundingMode.HALF_EVEN));
        // output = 6
        
        BigDecimal bd4 = new BigDecimal("3.5");
        System.out.println(bd4.setScale(0, RoundingMode.HALF_EVEN));
        // output = 4
    }
}

yengo หรือ buzzcity

วันอังคารที่ 14 มกราคม พ.ศ. 2557

ตัวอย่างโปรแกรมหาค่า ห.ร.ม.

ตัวอย่างโปรแกรมหาค่า ห.ร.ม.

ตัวอย่างโค้ดต่อไปนี้เป็นตัวอย่างโปรแกรมหาค่า ห.ร.ม. ซึ่งก็คือโปรแกรมหาค่าหารร่วมมาก

ตัวอย่างโปรแกรม ตัวอย่างแรก

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input First for GCD : ");
int first = in.nextInt();
System.out.print("Input Second for GCD : ");
int second = in.nextInt();
System.out.println(gcd(first, second));
}
public static int gcd(int first, int second){
List<Integer> compoundFirst = new ArrayList<Integer>(); // แยกตัวประกอบตัวแรก
int num = 2;
while(num < first){
if(first%num == 0){
compoundFirst.add(num);
first = first/num;
num = 1;
}
num++;
}
compoundFirst.add(first);
List<Integer> compoundSecond = new ArrayList<Integer>(); // แยกตัวประกอบตัวที่สอง
num = 2;
while(num < second){
if(second%num == 0){
compoundSecond.add(num);
second = second/num;
num = 1;
}
num++;
}
compoundSecond.add(second);
int result = 1;
for (int i = 0; i < compoundFirst.size(); i++) {
for (int j = 0; j < compoundSecond.size(); j++) {
if(compoundFirst.get(i) == compoundSecond.get(j)){ // เช็คว่าเหมือนกันหรือเปล่า
result *= compoundSecond.get(j);
compoundSecond.remove(j);
break;
}
}
}
return result;
}

ตัวอย่างโปรแกรม ตัวอย่างที่สอง

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input First for GCD : ");
int first = in.nextInt();
System.out.print("Input Second for GCD : ");
int second = in.nextInt();
System.out.println(gcd(first, second));
}
public static int gcd(int first, int second){
if(first%second == 0 || second%first == 0) {
return Math.min(first, second);
}
int result = 1;
int num = 2;
int min = Math.min(first, second);
while(num < min){
if(first%num == 0 && second%num == 0){ // เช็คว่าหารทั้งสองตัวลงตัว
result = num*result;
first = first/num;
second = second/num;
num = 1;
}
num++;
}
return result;
}

ตัวอย่างโปรแกรม ตัวอย่างที่สาม

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input First for GCD : ");
int first = in.nextInt();
System.out.print("Input Second for GCD : ");
int second = in.nextInt();
System.out.println(gcd(first, second));
}
public static int gcd(int first, int second){
if(first%second == 0) {
return Math.min(first, second);
}
int result = 1;
while(first%second != 0){
result = first%second;
first = second;
second = result;
}
return result;
}

ตัวอย่างโปรแกรม ตัวอย่างที่สี่

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input First for GCD : ");
int first = in.nextInt();
System.out.print("Input Second for GCD : ");
int second = in.nextInt();
System.out.println(gcd(first, second));
}
public static int gcd(int a, int b){
if(a%b == 0) {
return b;
}
//System.out.println(a + " = " + b + "*" + a/b + " + " + a%b);
return gcd(b, a%b);
}

yengo หรือ buzzcity

วันจันทร์ที่ 13 มกราคม พ.ศ. 2557

ตัวอย่างโปรแกรมเลขประจำตัวประชาชน โดยใช้ภาษา Java

ตัวอย่างโปรแกรมเลขประจำตัวประชาชน โดยใช้ภาษา Java

ตัวอย่างนี้เป็นตัวอย่างหาค่า checkbit ตัวสุดท้ายของเลขประจำตัวประชาชน

ตัวอย่างโค้ด

import java.util.Scanner; //import util.scanner เพื่อใช้สำหรับรับค่า

public class IDPerson {
    public static void main(String[] args) {
        long id = 0;
        while(true){ //วนจนกว่าจะป้อนค่าที่ถูกต้อง
            try {
                Scanner in = new Scanner(System.in); //สร้างตัวแปร scanner เพื่อรับค่าทางแป้นพิมพ์
                System.out.print("กรุณากรอกรหัสประจำตัว 12 หลัก "); //แสดงให้ผู้กรอกรู้ว่าต้องกรอกตัวเลข
                id = in.nextLong(); //รับค่าจากแป้นพิมพ์เป็นชนิด long
                String checkbit = id + ""; //สร้างตัวแปร checkbit เป็นชนิดสตริงเพื่อตรวจสอบความยาวของตัวเลข
                if(checkbit.length() == 12) break; //check ว่าค่าที่รับเข้ามามีขนาดกี่หลัก ถ้ามีสิบสองหลักให้ break ออกจาก loop
            }catch (Exception e) {
            }
        }
        long base = 100000000000l; //สร้างตัวแปร เพื่อสำหรับให้หารเพื่อเอาหลักที่ต้องการ
        int basenow; //สร้างตัวแปรเพื่อเก็บค่าประจำหลัก
        int sum = 0; //สร้างตัวแปรเริ่มตัวผลบวกให้เท่ากับ 0
        for(int i = 13; i > 1; i--) { //วนรอบตั้งแต่ 13 ลงมาจนถึง 2
            basenow = (int)Math.floor(id/base); //หาค่าประจำตำแหน่งนั้น ๆ
            id = id - basenow*base; //ลดค่า id ลงทีละหลัก
            System.out.println(basenow + "x" + i + " = " + (basenow*i)); //แสดงค่าเมื่อคูณแล้วของแต่ละหลัก
            sum += basenow*i; //บวกค่า sum ไปเรื่อย ๆ ทีละหลัก
            base = base/10; //ตัดค่าที่ใช้สำหรับการหาเลขแต่ละหลัก
        }
        System.out.println("Sum is " + sum); //แสดงค่า sum
        int checkbit = (11 - (sum%11))%10; //คำนวณค่า checkbit
        System.out.println("Check bit is " + checkbit); //แสดงค่า checkbit ที่ได้
    }
}

yengo หรือ buzzcity

วันอาทิตย์ที่ 12 มกราคม พ.ศ. 2557

ตัวอย่างโปรแกรมเช็คว่าวันสองวันเป็นวันเดียวกันหรือเปล่า โดยใช้ภาษา Java

ตัวอย่างโปรแกรมเช็คว่าวันสองวันเป็นวันเดียวกันหรือเปล่า โดยใช้ภาษา Java

ตัวอย่างโปรแกรมนี้ เป็นโปรแกรมที่เช็คว่า วันที่ของวันสองวันเป็นวันเดียวกันหรือเปล่า

ตัวอย่างโค้ดโปรแกรม

import java.util.Date; 
public class check2Date{
  public static void main(String[] args){
    // เปรียบเทียบวันที่ 25 พฤษภาคม 2555 กับวันที่ 1 มิถุนายน 2555 
  
    // สร้างตัวแปร ชื่อว่า preDate เก็บค่า 25 พฤษภาคม 2555 
    Date preDate = new Date(2012 - 1900, 4, 25);

    // สร้างตัวแปร ชื่อว่า preDate เก็บค่า 1 มิถุนายน 2555 
    Date afterDate = new Date(2012 - 1900, 5, 1);
  
    if(preDate.getDay() == afterDate.getDay()){ // เช็คว่าเป็นวันเดียวกันหรือเปล่า 
      System.out.println("It Same day."); // ถ้าเป็นวันเดียวกัน ให้แสดงคำนี้ 
    }
    else System.out.println("Is not the same day."); // ถ้าไม่เป็นวันเดียวกัน ให้แสดงคำนี้
  }
}

ตัวอย่างนี้ สร้างวันไว้สองวัน โดยใช้ Date แล้วก็นำมาเปรียบเทียบกันโดยใช้ getDay()

yengo หรือ buzzcity

วันเสาร์ที่ 11 มกราคม พ.ศ. 2557

ตัวอย่างโค้ด โปรแกรมคำนวณเกรด โดยภาษา Java

ตัวอย่างโค้ด โปรแกรมคำนวณเกรด โดยภาษา Java

ตัวอย่างโค้ดโปรแกรม ตัวอย่างนี้เป็นตัวอย่างของโปรแกรมคำนวณเกรด โดยมี input เป็นคะแนนเต็ม 100 แล้วคำนวณออกมาว่า เกรดที่ได้นั้นเป็นเกรดอะไร

ตัวอย่างโค้ด ตัวอย่างแรก

import java.util.Scanner;
public class Grade2 {
    public static void main(String args[]){
        System.out.print("Input Score : ");
        Scanner in = new Scanner(System.in);
        int score = in.nextInt();
        if(score < 50) System.out.println("Your Score : F");
        if(score >= 50 && score < 60) System.out.println("Your Score : D");
        if(score >= 60 && score < 70) System.out.println("Your Score : C");
        if(score >= 70 && score < 80) System.out.println("Your Score : B");
        if(score >= 80) System.out.println("Your Score : A");
    }
}

ตัวอย่างนี้เป็นตัวอย่างโค้ดที่ใช้แต่ if อย่างเดียว

ตัวอย่างโค้ด ตัวอย่างที่สอง

import java.util.Scanner;
public class Grade3 {
    public static void main(String args[]){
        System.out.print("Input Score : ");
        Scanner in = new Scanner(System.in);
        int score = in.nextInt();
        if(score < 50) System.out.println("Your score : F");
        else{
            if(score < 60) System.out.println("Your score : D");
            else{
                if(score < 70) System.out.println("Your score : C");
                else{
                    if(score < 80) System.out.println("Your score : B");
                    else System.out.println("Your score : A");
                }
            }
        }
    }
}

ตัวอย่างที่สองนี้เป็นตัวอย่างโค้ดที่ใช้แต่ if else

ตัวอย่างโค้ด ตัวอย่างที่สาม

import java.util.Scanner;
public class Grade {
    public static void main(String args[]){
        System.out.print("Input Score : ");
        Scanner in = new Scanner(System.in);
        int score = in.nextInt();
        if(score < 50) System.out.println("Your Grade : F");
        else if(score < 60) System.out.println("Your Grade : D");
        else if(score < 70) System.out.println("Your Grade : C");
        else if(score < 80) System.out.println("Your Grade : B");
        else System.out.println("Your Grade : A");
    }
}

ตัวอย่างที่สามนี้เป็นตัวอย่างโค้ดที่ใช้ if else if

yengo หรือ buzzcity

วันศุกร์ที่ 10 มกราคม พ.ศ. 2557

ตัวอย่างโค้ด โปรแกรมหาพื้นที่สามเหลี่ยม

มาดูตัวอย่างโค้ดโปรแกรมหาพื้นที่สามเหลี่ยมกันครับ

ตัวอย่างโค้ด

import java.util.Scanner;
public class Main {
    public static void main(String[] args ) {
        Scanner in = new Scanner(System.in);
        System.out.print("Input width : ");
        double width = in.nextDouble();
        System.out.print("Input height : ");
        double height = in.nextDouble();
        System.out.println("-----------------------");
        System.out.println("The triangular area : " + 0.5 * width * height);
    }
}

ตัวอย่างนี้รับค่าจากแป้นพิมพ์ ด้วย Scanner จากนั้นรับค่ามาสองค่านั่นคือ ความกว้าง กับความสูง เนื่องจากสูตรของพื้นที่สามเหลี่ยมคือ (0.5 * ฐาน * สูง) จากนั้นก็แสดงออกมาทางหน้าจอ

yengo หรือ buzzcity

วันพุธที่ 8 มกราคม พ.ศ. 2557

ตัวอย่างโค้ด โปรแกรมหาพื้นที่สี่เหลี่ยม

ตัวอย่างโค้ด โปรแกรมหาพื้นที่สี่เหลี่ยม

เรามาดูตัวอย่างโปรแกรมโค้ดภาษา Java โดยโปรแกรมที่จะเขียนนี้เป็นตัวอย่างของโปรแกรมหาพื้นที่สี่เหลี่ยม

ตัวอย่างโค้ด

import java.util.Scanner;
public class Main {
    public static void main(String[] args ) {
        Scanner in = new Scanner(System.in);
        System.out.print("Input width : ");
        double width = in.nextDouble();
        System.out.print("Input length : ");
        double length = in.nextDouble();
        System.out.println("-----------------------");
        System.out.println("square area : " + width * length);
    }
}

ตัวอย่างนี้เป็นการใช้ Scanner เพื่อรับค่า จากนั้นก็รับค่าความกว้าง กับความยาว จากนั้น ก็นำมาคูณกัน

ลองมาดูตัวอย่างโปรแกรมโปรแกรมหาพื้นที่สี่เหลี่ยมจตุรัสกันครับ

ตัวอย่างโค้ด


import java.util.Scanner;
public class Main {
    public static void main(String[] args ) {
        Scanner in = new Scanner(System.in);
        System.out.print("Input length : ");
        double length = in.nextDouble();
        System.out.println("-----------------------");
        System.out.println("square area : " + 2 * length);
    }
}

รู้กันดีว่าสี่เหลี่ยมจตุรัสเป็นสี่เหลี่ยมที่มีด้านเท่ากันทุกด้าน ดังนั้นตัวอย่างนี้จึงรับค่าความยาวมาแค่ค่าเดียว จากนั้นก็นำมาคูณ 2

yengo หรือ buzzcity

วันอังคารที่ 7 มกราคม พ.ศ. 2557

เขียนโปรแกรมแปลงตัวอักษรเป็นรหัส ACSII โดย Java

เขียนโปรแกรมแปลงตัวอักษรเป็นรหัส ACSII โดย Java

เรามาดูตัวอย่างโปรแกรมที่รับค่าตัวอักษรมา แล้วนำมาแปลงเป็นรหัส ACSII กันครับ

ตัวอย่างโค้ด


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CodeFromDoesystem {
public static void main(String[] args) throws Exception {
BufferedReader str = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Insert Number : ");
int s = str.read();
System.out.println("Convert to ACSII : " + s);
}
}

yengo หรือ buzzcity

วิธีใช้ และตัวอย่างโค้ด java.math.BigInteger.clearBit(int n)

วิธีใช้ และตัวอย่างโค้ด java.math.BigInteger.clearBit(int n)

เรามาดูโค้ดตังอย่างวิธีใช้ของโค้ด java.math.BigInteger.clearBit(int n)

การ clearBit นั้น ค่าที่ออกมาจะได้ (this & ~(1<<n))

ตัวอย่างโค้ด


import java.math.BigInteger;

public class BigIntegerExam {
	public static void main(String[] args) {
		BigInteger bi1 = new BigInteger("7"); 
		System.out.println(bi1.clearBit(2)); // 3
		// 7 = 111, 111 << 2 = 11100, ~11100 = 00011, 111 & 00011 = 00011 = 3
		
		int testbit = 7; // 111 , 7 << 1 = 14 = 1110, 7 << 2 = 28 = 11100
		System.out.println(testbit << 2); // 28
	}
}

ตัวอย่างนี้ เรามี BigInteger มีค่าเป็น 7 จากนั้นำมา clearBit(2) ซึ่งจะได้ 3

สังเกตุจากตัวอย่างการ testbit จะได้ว่า 7 <<< 2 = 11100 เมื่อนำมา Not จะได้ 00011 จากนั้นนำ 111 มา And กับ 00011 ก็จะได้ 00011 ซึ่งก็เท่ากับ 3

yengo หรือ buzzcity

วันจันทร์ที่ 6 มกราคม พ.ศ. 2557

ตัวอย่างโค้ดโปรแกรมรับค่า ชื่อ แล้วแสดงออกทางหน้าจอ

ตัวอย่างโค้ดตัวอย่างแรก

package com.doesystem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CodeFromDoesystem {
public static void main(String[] args) throws IOException {
BufferedReader str = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Insert Name : ");
        String s = str.readLine();
        System.out.println("Yourname " + s );
}
}

ตัวอย่างนี้เป็นการรับค่าจากแป้นพิมพ์ด้วย InputStreamReader เมื่อรับมาแล้วก็ใช้ readLine เพื่ออ่านทั้งบรรทัด มาเก็บไว้ในตัวแปร จากนั้นก็ println ชื่อที่รับออกมา

ตัวอย่างโค้ดตัวอย่างที่ 2

package com.doesystem;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CodeFromDoesystem {
public static void main(String[] args) {
BufferedReader str = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Insert Name : ");
        String s = "";
        try{
            s = str.readLine();
        }
        catch(Exception e){
            System.out.print(e);
        }
        System.out.println("Yourname " + s );
}
}

ตัวอย่างนี้คล้าย ๆ กับตัวอย่างข้างบนแต่ต่างกันที่ตัวอย่างข้างบน ใช้ throws Exception ออกไป แต่ตัวอย่างนี้จะใช้ try catch เพื่อดับจับข้อผิดพลาดของการรับข้อมูล

yengo หรือ buzzcity

ตัวอย่างโค้ด และการใช้งาน java.math.BigInteger.andNot(BigInteger val)

java.math.BigInteger.andNot(BigInteger val) จะ returns ค่า BigInteger ที่มีค่ามาแอนกัน

ซึ่งค่าที่นำมาแอน จำนำมา Not ก่อน

ตัวอย่างโค้ด

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("6"); // 110
  BigInteger bi2 = new BigInteger("3"); // 011 ~010=101
  System.out.println(bi1.andNot(bi2)); // 100
  
  bi1 = new BigInteger("5"); // 0101
  bi2 = new BigInteger("8"); // 1000 ~1000=0111
  System.out.println(bi1.andNot(bi2)); // 0101
 }
}

ตัวอย่างนี้ มี BigInteger สองตัว จากนั้นนำมา andNot กัน ซึ่งตัวที่ทำมา And นั้น ต้อง Not ก่อน ตัวอย่างเช่น 3 ซึ่งมีบิตเป็น 011 เมื่อนำมา Not แล้วจะได้ 101 เมื่อนำมา And กับ ุ6 ซึ่งก็คือ 110 จะได้ 100

yengo หรือ buzzcity

วันอาทิตย์ที่ 5 มกราคม พ.ศ. 2557

ตัวอย่างโปรแกรมหาผลบวกของสองจำนวน โดย Java

เรามาดูตัวอย่างโค้ดที่หาผลรวมของจำนวนสองจำนวนกันครับ

ซึ่งจำนวนสองจำนวนนั้นเป็นการรับค่ามาจาก keyboard

ตัวอย่างโค้ด


import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner Sc = new Scanner(System.in);
        System.out.print("Insert Number1 : ");
        int First = Sc.nextInt();
        System.out.print("Insert Number2 : ");
        int Second = Sc.nextInt();
        System.out.println("Sum : " + (First + Second) );
    }
}

จากโค้ดตัวอย่างมีการรับค่ามาจาก keyboard โดยรับเป็นจำนวนเต็ม Int สองจำนวน แล้วนำสองจำนวนนั้นมาบวกกัน แสดงออกทางหน้าจอ

เราใช้ Scanner ในการบอกว่าจะรับค่า และ System.in บอกว่าจะรับค่ามาจาก keyboard เมื่อได้ตัวแปรที่เป็น Scanner แล้ว เราก็ใช้ nextInt เพื่อรับค่ามาแบบจำนวนเต็ม Int

yengo หรือ buzzcity

ตัวอย่างโค้ด และการใช้งาน java.math.BigInteger.and(BigInteger val)

ตัวอย่างโค้ด และการใช้งาน java.math.BigInteger.and(BigInteger val)

java.math.BigInteger.and(BigInteger val) จะ returns ค่า BigInteger ที่มีค่ามาแอนกัน

ตัวอย่างโค้ด


import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("6"); // 110
  BigInteger bi2 = new BigInteger("3"); // 011
  System.out.println(bi1.and(bi2)); // 010
  
  bi1 = new BigInteger("5"); // 0101
  bi2 = new BigInteger("8"); // 1000
  System.out.println(bi1.and(bi2)); // 0000
 }
}

จากโค้ดด้านบนเรามี BigInteger ที่มีค่า 6 กับ 3 ซึ่งเราสามารถแยกบิตได้ 6 = 110 และ 3 = 011 เมื่อนำมา and กัน จะได้ 010 ซึ่งถ้าบิตเป็นหนึ่งทั้งคู่ จะเป็น 1 นอกนั้นเป็น 0

yengo หรือ buzzcity

วันเสาร์ที่ 4 มกราคม พ.ศ. 2557

ตัวอย่างโค้ด และการใช้งาน java.math.BigInteger.abs()

ตัวอย่างโค้ด และการใช้งาน java.math.BigInteger.abs()

java.math.BigInteger.abs() จะ returns ค่าเป็น BigInteger ที่มีค่าเป็น absolute

ตัวอย่างโค้ด

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("456");
  BigInteger bi2 = new BigInteger("-456");
  System.out.println("Absolute value of " + bi1 + " is " + bi1.abs()); // Absolute value of 456 is 456
  System.out.println("Absolute value of " + bi2 + " is " + bi2.abs()); // Absolute value of -456 is 456
 }
}

จากตัวอย่างข้างต้น เรามี BigInteger ซึ่งตัวแรกมีค่า 456 อีกตัวมีค่า -456 เมื่อนำไปหาค่าโดยเรียกใช้ method abs ซึ่งค่าที่ return มานั้น ตัวแรกก็ให้ค่า 456 เหมือนเดิม ส่วนอีกตัว ก็จะได้ค่า 456 ซึ่งเป็นค่า Absolute ของ -456 

yengo หรือ buzzcity

ตัวอย่างโค้ด และการใช้งาน java.math.BigInteger.add(BigInteger val)

ตัวอย่างโค้ด และการใช้งาน java.math.BigInteger.add(BigInteger val)

java.math.BigInteger.add(BigInteger val) จะ return ค่า BigInteger object ที่มีค่าเป็น ค่าเดิม บวกกับค่า val

import java.math.BigInteger;

public class BigIntegerExam {
 public static void main(String[] args) {
  BigInteger bi1 = new BigInteger("456");
  BigInteger bi2 = new BigInteger("789");
  System.out.println(bi1.add(bi2)); // output : 1245 (456 + 789)
  System.out.println(bi1); // output : 456
  bi1 = bi1.add(bi2);
  System.out.println(bi1); // output : 1245
  System.out.println(bi1.add(BigInteger.valueOf(1))); // output : 1246
 }
}

จากตัวอย่างโค้ดข้างบน เราสร้าง BigInteger ขึ้นมาสองตัว โดยตัวแรกมีค่า 456 อีกตัวมีค่า 789 เราเอาตัวแรกมาเรียกใช้ method add แล้วก็ส่ง BigInteger ตัวที่สองไป ผลลัพธ์ที่ได้ ก็คือ 1245 ซึ่งก็คือค่า 456 + 789 นั่นเอง

จากนั้นเราก็สั่งให้แสดงค่า BigInteger ตัวแรก ซึ่งก็จะได้ค่าของ 456 นั่นคือตัวเดิมนั่นเอง


yengo หรือ buzzcity