วันเสาร์ที่ 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

2 ความคิดเห็น :

  1. import java.util.*;
    public class Rectangle {
    //attibute
    private double high;
    private double width;
    private double arearec;
    private double circum;


    public void setHigh(double high){
    this.high = high;
    }
    public Double getHigh(){
    return high;
    }
    public void setWidth(double width){
    this.width = width;
    }
    public Double getWidth(){
    return width;
    }
    public void calAreaRec(){
    arearec = high * width;
    }
    public Double getAreaRec(){
    return arearec;
    }
    public void calRecCircum(){
    circum = (high*2)+(width*2);
    }
    public Double getCircum(){
    return circum;
    }
    }

    //callling
    public class callingrectangle {
    public static void main(String[] args) {
    RectangleOOP r1 = new RectangleOOP();
    r1.setHigh(5.0);
    r1.setWidth(10.0);
    r1.calAreaRec();
    r1.calRecCircum();
    System.out.println("Rectangle1");
    System.out.println("Rectangle1 Hight is : " + r1.getHigh() + " Unit.");
    System.out.println("Rectangle1 Width is : " +r1.getWidth() + " Unit.");
    System.out.println("Rectangle1 Area is : " +r1.getAreaRec()+ " Unit.");
    System.out.println("Rectangle1 Circum is : "+r1.getCircum()+ " Unit.");
    RectangleOOP r2 = new RectangleOOP();
    r2.setHigh(15.0);
    r2.setWidth(10.0);
    r2.calAreaRec();
    r2.calRecCircum();
    System.out.println("Rectangle2");
    System.out.println("Rectangle2 Hight is : " + r2.getHigh() + " Unit.");
    System.out.println("Rectangle2 Width is : " +r2.getWidth() + " Unit.");
    System.out.println("Rectangle2 Area is : " +r2.getAreaRec()+ " Unit.");
    System.out.println("Rectangle2 Circum is : "+r2.getCircum()+ " Unit.");
    RectangleOOP r3 = new RectangleOOP();
    r3.setHigh(7.0);
    r3.setWidth(18.0);
    r3.calAreaRec();
    r3.calRecCircum();
    System.out.println("Rectangle3");
    System.out.println("Rectangle3 Hight is : " + r3.getHigh() + " Unit.");
    System.out.println("Rectangle3 Width is : " +r3.getWidth() + " Unit.");
    System.out.println("Rectangle3 Area is : " +r3.getAreaRec()+ " Unit.");
    System.out.println("Rectangle3 Circum is : "+r3.getCircum()+ " Unit.");

    // triangle calculate
    TriangleOOP t1 = new TriangleOOP();
    t1.setHigh(12);
    t1.setBase(10);
    t1.calAreatri();
    t1.calSide();
    System.out.println("Triandgle1");
    System.out.println("Triangle1 Hight is : " + t1.getHigh() + " Unit.");
    System.out.println("Triangle1 Base is : " + t1.getBase() + " Unit.");
    System.out.println("Triangle1 Area is : " + t1.getAreatri()+ " Unit.");
    System.out.println("Triangle1 Side lenght is : "+ t1.getSidelenght()+ " Unit.");
    TriangleOOP t2 = new TriangleOOP();
    t2.setHigh(8);
    t2.setBase(15);
    t2.calAreatri();
    t2.calSide();
    System.out.println("Triandgle2");
    System.out.println("Triangle2 Hight is : " + t2.getHigh() + " Unit.");
    System.out.println("Triangle2 Base is : " + t2.getBase() + " Unit.");
    System.out.println("Triangle2 Area is : " + t2.getAreatri()+ " Unit.");
    System.out.println("Triangle2 Side lenght is : "+ t2.getSidelenght()+ " Unit.");
    TriangleOOP t3 = new TriangleOOP();
    t3.setHigh(17);
    t3.setBase(25);
    t3.calAreatri();
    t3.calSide();
    System.out.println("Triandgle3");
    System.out.println("Triangle3 Hight is : " + t3.getHigh() + " Unit.");
    System.out.println("Triangle3 Base is : " + t3.getBase() + " Unit.");
    System.out.println("Triangle3 Area is : " + t3.getAreatri()+ " Unit.");
    System.out.println("Triangle3 Side lenght is : "+ t3.getSidelenght()+ " Unit.");



    }
    }

    ตอบลบ
  2. import java.util.*;
    public class Triangle{
    //attribute
    private double base;
    private double highTriangle;
    private double areatriangle;
    private double sidelenght;

    public void setHigh(double highTriangle){
    this.highTriangle = highTriangle;
    }
    public Double getHigh(){
    return highTriangle;
    }
    public void setBase(double base){
    this.base = base;
    }
    public Double getBase(){
    return base;
    }
    public void calAreatri(){
    areatriangle = (highTriangle*base)/2;
    }
    public Double getAreatri(){
    return areatriangle;
    }
    public void calSide(){
    sidelenght = base*3;
    }
    public Double getSidelenght(){
    return sidelenght;
    }
    }

    ตอบลบ