ตัวอย่างโค้ดโปรแกรม โปรแกรมคำนวณพื้นที่วงกลม
เรามาดูตัวอย่างโค้ดโปรแกรมคำนวณพื้นที่วงกลมกันครับ ซึ่งการหาพื้นที่วงกลมนั้น มีสูตรว่า pr²
ตัวอย่างโค้ด
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);
}
}
import java.util.*;
ตอบลบclass Circle{
public static void main(String[] args) {
double area;
double circum;
//input
Scanner sc = new Scanner(System.in);
System.out.println("Enter a radius: ");
double radius = sc.nextDouble();
//process
area = Math.PI * radius * radius;
circum = 2 * Math.PI * radius;
//output => UI
System.out.println("Area: " + area);
System.out.println("Circumference :" + circum);
}
}
/////////////////////callcircle
public class CallingCircle{
public static void main(String[] args) {
circleOOP c1 = new circleOOP();
System.out.println(c1);
System.out.println(c1.getRadius());
c1.setName("POM");
c1.setRadius(1.0);
System.out.println(c1.getName());
System.out.println(c1.getRadius());
circleOOP c2 = new circleOOP();
c2.setName("TU");
c2.setRadius(5.0);
System.out.println(c2.getName());
System.out.println(c2.getRadius());
circleOOP c3 = new circleOOP();
c3.setName("POCK");
c3.setRadius(2.0);
System.out.println(c3.getName());
System.out.println(c3.getRadius());
//set c1 radius = 7.0
c1.setRadius(7.0);
System.out.println(c1.getName()+" "+ "has a radius = "+c1.getRadius() +" Unit.");
//increase another 5.0 radius of exit Pom.
double newRadius = c1.getRadius()+5.5;
c1.setRadius(newRadius);
System.out.println(c1.getName()+" "+ "has a radius = "+c1.getRadius() +" Unit.");
circleOOP c4 = new circleOOP();
c4.setName("NOO");
c4.setRadius(13.0);
System.out.println(c4.getName()+" "+ "has a radius = "+c4.getRadius() +" Unit.");
circleOOP c5 = new circleOOP();
c5.setName("PALM");
c5.setRadius(19.0);
System.out.println(c5.getName()+" "+ "has a radius = "+c5.getRadius() +" Unit.");
circleOOP c6 = new circleOOP();
c6.setName("FEW");
c6.setRadius(21.0);
System.out.println(c6.getName()+" "+ "has a radius = "+c6.getRadius() +" Unit.");
c1.calCircum();
System.out.println(c1.getName()+" "+"Has circum = "+ c1.getCircum() + " Unit");
c1.calArea();
System.out.println(c1.getName()+" "+"Has Area = "+ c1.getArea()+ " Unit");
c2.calCircum();
System.out.println(c2.getName()+" "+"Has circum = "+ c2.getCircum() + " Unit");
c2.calArea();
System.out.println(c2.getName()+" "+"Has Area = "+ c2.getArea()+ " Unit");
c3.calCircum();
System.out.println(c3.getName()+" "+"Has circum = "+ c3.getCircum() + " Unit");
c3.calArea();
System.out.println(c3.getName()+" "+"Has Area = "+ c3.getArea()+ " Unit");
c4.calCircum();
System.out.println(c4.getName()+" "+"Has circum = "+ c4.getCircum() + " Unit");
c4.calArea();
System.out.println(c4.getName()+" "+"Has Area = "+ c4.getArea()+ " Unit");
c5.calCircum();
System.out.println(c5.getName()+" "+"Has circum = "+ c5.getCircum() + " Unit");
c5.calArea();
System.out.println(c5.getName()+" "+"Has Area = "+ c5.getArea()+ " Unit");
c6.calCircum();
System.out.println(c6.getName()+" "+"Has circum = "+ c6.getCircum() + " Unit");
c6.calArea();
System.out.println(c6.getName()+" "+"Has Area = "+ c6.getArea()+ " Unit");
}
}
import java.util.*;
ตอบลบpublic class circleOOP{
//attibute
private double radius;
private double area;
private double circum;
private String name;
//methods
public double getRadius(){
return radius;
}
public void setRadius(double r){
radius = r;
}
public void setName (String name){
this.name = name;
}
public String getName(){
return name;
}
public void calCircum(){
circum = 2 * Math.PI * radius;
}
public Double getCircum(){
return circum;
}
public void calArea() {
area = Math.PI * radius * radius ;
}
public Double getArea(){
return area;
}
}