Interface > Abstract > Class Extends và Override method tư interface
Shape.Java
Java 2017
package Java2;
public interface Shape {
void getArea();
void getVolume();
}
TwoDimensionalShape.Java
Java Advanced 2017
package Java2; public abstract class TwoDimensionalShape implements Shape { @Override public void getArea() { // TODO Auto-generated method stub } @Override public void getVolume() { // TODO Auto-generated method stub } }
ThreeDimensionalShape.Java
Java Advanced 2017
package Java2;
public abstract class ThreeDimensionalShape implements Shape {
@Override
public void getArea() {
// TODO Auto-generated method stub
}
@Override
public void getVolume() {
// TODO Auto-generated method stub
}
}
Circle.Java
Java Advanced 2017
package Java2;
public class Circle extends TwoDimensionalShape {
private final double PI = 3.14f;
private double r, S;
public Circle(double r) {
super();
this.r = r;
Calculature();
}
public double getPI() {
return PI;
}
public double getR() {
return r;
}
public void Calculature() {
S = Math.round(PI * getR() * PI);
}
@Override
public void getArea() {
// TODO Auto-generated method stub
// S = PI*R*2
System.out.println("Dien tich hinh tron: " + S);
}
}
Square.Java
Java Advanced 2017
package Java2;
public class Square extends TwoDimensionalShape {
private int a;
public int getA() {
return a;
}
public Square(int a) {
super();
this.a = a;
}
@Override
public void getArea() {
// TODO Auto-generated method stub
//S = a²
System.out.println("Dien tich hinh vuong: " + Math.round(getA() * getA()));
}
}
Triangle.Java
Java Advanced 2017
package Java2;
public class Triangle extends TwoDimensionalShape {
private int b; //b chieu dai canh day
private int h; //h: chieu cao
public int getB() {
return b;
}
public int getH() {
return h;
}
public Triangle(int b, int h) {
super();
this.b = b;
this.h = h;
}
@Override
public void getArea() {
// TODO Auto-generated method stub
//S = (b*h)/2
int a = getB() * getH();
System.out.println("Dien tich hinh tam giac: " + a / 2);
}
}
Sphere.Java
Java Advanced 2017
package Java2;
public class Sphere extends ThreeDimensionalShape {
private double r; //Ban kinh
private final double PI = 3.14d;
private double S, V;
public Sphere(double r) {
super();
this.r = r;
Calculature();
}
public double getR() {
return r;
}
public void Calculature() {
//S=4*PI*R^2
double R2PI = PI * getR() * getR();
S = 4 * R2PI;
//v = 3/4 *PI * R^3
V = (3 * R2PI * getR()) / 4;
}
@Override
public void getArea() {
// TODO Auto-generated method stub
System.out.println("Dien tich cua hinh cau: " + Math.round(S));
}
@Override
public void getVolume() {
// TODO Auto-generated method stub
System.out.println("The tich hinh cau: " + Math.round(V));
}
}
Cube.Java
Java Advanced 2017
package Java2;
public class Cube extends ThreeDimensionalShape {
private int a;
public int getA() {
return a;
}
public Cube(int a) {
super();
this.a = a;
}
@Override
public void getArea() {
// TODO Auto-generated method stub
// S = 6a²
System.out.println("Dien tich hinh lap phuong: " + 6 * (getA() * getA()));
}
@Override
public void getVolume() {
// TODO Auto-generated method stub
// V = a x a x a
System.out.println("The tich hinh lap phuong: " + getA() * getA() * getA());
}
}
Tetrahedron.Java
Java Advanced 2017
package Java2;
public class Tetrahedron extends ThreeDimensionalShape {
private int a, b, c, h; // abc canh tam giac, h la chieu cao
private int P, result, S;
private boolean check = false;
public int getA() {
return a;
}
public int getB() {
return b;
}
public int getC() {
return c;
}
public int getH() {
return h;
}
public int getP() {
return P;
}
public int getResult() {
return result;
}
public Tetrahedron(int a, int b, int c, int h) {
super();
this.a = a;
this.b = b;
this.c = c;
this.h = h;
calculature();
}
public int getS() {
return S;
}
public boolean check() {
if (getA() + getB() > getC()) {
if (getA() + getC() > getB()) {
if (getC() + getB() > getA()) {
check = true;
} else {
System.out.println("Nhap sai gia tri B or C");
}
} else {
System.out.println("Nhap sai gia tri A or C");
}
} else {
System.out.println("Nhap sai gia tri A or B");
}
return check;
}
public void calculature() {
P = (getA() + getB() + getC()) / 2;
result = P * (P - getA()) * (P - getB()) * (P - getC());
S = (int) Math.sqrt(result);
}
@Override
public void getArea() {
// TODO Auto-generated method stub
// p = (a + b + c) /2
// S =(SQRT) p[p-a][p-b][p-c]
System.out.println("Dien tich hinh tu dien : " + S);
}
@Override
public void getVolume() {
// TODO Auto-generated method stub
// V = 1/3 * S*h
System.out.println("The tich hinh tu dien: " + (S * h) / 3);
check();
}
}
Main.Java
Java Advanced 2017
package Java2; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<Shape> list = new ArrayList<Shape>(); Shape c = new Circle(5);// r2 Shape s = new Square(5);// a Shape t = new Triangle(4, 6);// a canh, h chieu cao Sphere s1 = new Sphere(6); // r Cube c1 = new Cube(9);// a Tetrahedron t1 = new Tetrahedron(9, 8, 12, 2); // a,b,c,h list.add((Shape) c); list.add((Shape) s); list.add((Shape) t); list.add((Shape) s1); list.add((Shape) c1); list.add((Shape) t1); // Output 1 System.out.println("\nDIEN TICH CUA CAC HINH\n"); for (Shape item : list) { item.getArea(); } System.out.println("\nTHE TICH CUA CAC HINH\n"); for (Shape item : list) { item.getVolume(); } } }
0 nhận xét:
Post a Comment