วันศุกร์ที่ 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

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

  1. public class CallingShape{
    public static void main(String[] args){

    //Shape c2 = new Circle();

    //System.out.println(c1.name);
    //System.out.println(c1.circum);
    //System.out.println(c1.area);

    //1. set name and radius and id for c1
    Circle c1 = new Circle();
    c1.setName("Varaponc1");
    c1.setRadius(1.0);
    c1.setId(1);
    c1.calArea();
    c1.calCircum();
    c1.printInfo();

    //2. set name and radius and id for c2
    Circle c2 = new Circle(2.0);
    c2.setName("Varaponc2");
    c2.setId(2);
    c2.calArea();
    c2.calCircum();
    c2.printInfo();


    //3. set name and radius and id for c3
    Circle c3 = new Circle("Varaponc3",3.0,3); //name ,radius , id
    c3.calArea();
    c3.calCircum();
    c3.printInfo();

    //4. set name and radius and id for c4
    Circle c4 = new Circle("Varaponc4",8.0,4); //name ,radius , id

    c4.calArea();
    c4.calCircum();
    c4.printInfo();

    Sphere c5 = new Sphere("Varaponc5",4.0,4); //name ,radius , id
    c5.calArea();
    c5.calCircum();
    c5.calVolume();
    c5.printInfo();

    Square c6 = new Square("Varaponc6",9.0,3.0,5); //name ,radius , id
    c6.calArea();
    c6.calCircum();
    c6.printInfo();

    Triangle c7 = new Triangle("Varaponc7",8.0,4.0,6); //name ,radius , id
    c7.calArea();
    c7.calCircum();
    c7.printInfo();

    System.out.println("Number of Shape: "+Shape.numberOfShape);




    }
    }

    ตอบลบ
  2. import java.text.*;
    public class Circle extends Shape{
    protected double radius;
    //private int id;


    public Circle(){
    //System.out.println("Circle()");
    }
    public Circle(double radius){
    //System.out.println("Circle(double radius)");
    this.radius = radius;
    }
    public Circle(String name,double radius,int id){
    this.name = name;
    this.radius = radius;
    this.id = id;
    }

    public double getRadius(){
    return radius;
    }
    public void setRadius(double radius){ //double radius => informal parameter
    this.radius = radius;
    }
    public void setId(int id){
    this.id = id;
    }
    public int getId(){
    return id;
    }
    public void printInfo(){
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    System.out.println("ID: "+id +"\tName: "+ name + "\tRadius: "+radius+"\tArea: "+ df.format(area) + "\tCircumference "+df.format(circum));
    }
    public void calArea(){
    area = Math.PI * radius * radius;
    }
    public void calCircum(){
    circum = Math.PI * radius * 2;
    }
    }

    ตอบลบ
  3. public class Shape{

    protected String name;
    protected double area;
    protected double circum;
    protected int id;

    public static int numberOfShape;

    //defualt Contructor
    public Shape(){
    //System.out.println("Shape()");
    numberOfShape++;
    }

    public void setName(String name){
    this.name = name;
    }
    public String getName(){
    return name;
    }

    public Double getArea(){
    return area;
    }
    public Double getCircum(){
    return circum;
    }
    public void setID(int id){
    this.id = id;
    }
    public int getId(){
    return id;
    }

    public void calArea(){

    }
    public void calcircum(){

    }



    }

    ตอบลบ
  4. import java.text.*;
    public class Sphere extends Circle{
    private double volume;
    public Sphere(){

    }
    public Sphere(String name,double radius,int id){
    this.name = name;
    this.radius = radius;
    this.id = id;
    }


    public double getVolume(){
    return volume;
    }



    public void calVolume(){
    volume = (4/3)*Math.PI*Math.pow(radius, 3);
    }
    public void printInfo(){
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    System.out.println("ID: "+id +"\tName: "+ name + "\tRadius: "+radius+"\tArea: "+ df.format(area) + "\tCircumference "+df.format(circum)+"\tVolume: "+df.format(volume));
    }



    }

    ตอบลบ
  5. import java.text.*;
    public class Square extends Shape{
    private double high;
    private double width;


    public Square(){


    }
    public Square(String name,double width,double high,int id){
    this.name = name;
    this.width = width;
    this.high = high;
    this.id = id;
    }
    public void calArea(){
    area = width*high;
    }
    public void calCircum(){
    circum = (width+high)*2;
    }
    public void printInfo(){
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    System.out.println("ID: "+id +"\tName: "+ name +"\tArea: "+ df.format(area) + "\tCircumference "+df.format(circum));
    }

    }

    ตอบลบ
  6. import java.text.*;
    public class Triangle extends Shape{
    private double base;
    private double high;


    public Triangle(){

    }

    public Triangle(String name,double base,double high,int id){
    this.name = name;
    this.base = base;
    this.high = high;
    this.id = id;
    }
    public void calArea(){
    area =(1.0/2.0) * base * high;
    }
    public void calCircum(){
    //a = base *base; => Math.pow(base, 2)
    //b = high *high;
    //c = a+b;
    circum = Math.sqrt(Math.pow(base, 2)+Math.pow(high, 2));
    }
    public void printInfo(){
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    System.out.println("ID: "+id +"\tName: "+ name +"\tArea: "+ df.format(area) + "\tCircumference "+df.format(circum));


    }
    }

    ตอบลบ