[NetBean]
1. Xây dựng Project như sau:
2. Code:
//-------------------------------------------
//abstract class HinhHoc
//-------------------------------------------
package vd_abstract_150114;
public abstract class HinhHoc {
String tenHinh;
// Constructor
public HinhHoc(String tenHinh){
this.tenHinh=tenHinh;
}
// Tinh chu vi
abstract float ChuVi();
// Tinh dien tich
abstract float DienTich();
// In ket qua
void InKQ(){
System.out.print("\n + Ten hinh: "+ tenHinh);
System.out.print("\n - Chu vi: "+ ChuVi());
System.out.print("\n - Dien tich: "+ DienTich());
}
}
// class HinhVuong
//-------------------------------------------
package vd_abstract_150114;
public class HinhVuong extends HinhHoc{
float a;
// constructor
public HinhVuong(String tenHinh,float a){
super(tenHinh);
this.a=a;
}
// Chu vi
float ChuVi(){
return 4*a;
}
float DienTich(){
return a*a;
}
}
// class HinhChuNhat
//-------------------------------------------
package vd_abstract_150114;
public class HinhChuNhat extends HinhVuong {
float b;
// constructor
public HinhChuNhat(String tenHinh,float a,float b){
super(tenHinh,a);
this.b=b;
}
// Chu vi
float ChuVi(){
return 2*(a+b);
}
// Dien tich
float DienTich(){
return a*b;
}
}
//-------------------------------------------
// class HinhTamGiac
//-------------------------------------------
// class HinhTamGiac
//-------------------------------------------
package vd_abstract_150114;
public class HinhTamGiac extends HinhChuNhat {
float c;
// constructor
public HinhTamGiac(String tenHinh,float a,float b,float c){
super(tenHinh,a,b);
this.c=c;
}
// chu vi
float ChuVi(){
return (a+b+c);
}
// dien tich
float DienTich(){
float p=ChuVi()/2;
return (float)Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
}
// class HinhTron
//-------------------------------------------
package vd_abstract_150114;
public class HinhTron extends HinhHoc {
float r;
final float PI=3.14f;
// constructor
public HinhTron(String tenHinh,float r){
super(tenHinh);
this.r=r;
}
// chu vi
float ChuVi(){
return 2*PI*r;
}
// dien tich
float DienTich(){
return PI*r*r;
}
}
// class VD_abstract_150114
/* http://www.laptirnhmaytinh.net */
package vd_abstract_150114;
import java.util.Scanner;
public class Vd_abstract_150114 {
public static void main(String[] args) {
float a,b,c,r;
Scanner inp=new Scanner(System.in);
HinhHoc hv,hcn,tg,ht;
// Hinh vuong
try{
do{
System.out.print("\n + Nhap canh hinh vuong: ");
a=inp.nextFloat();
if(a<=0)
System.out.println("\n >> Nhap lai !");
}while(a<=0);
// khoi tao doi tuong
hv=new HinhVuong("Hinh vuong",a);
hv.InKQ();
}catch(Exception e){
System.out.println("\n * Co loi xay ra ! Ma loi: "+e.toString());
}
// Hinh chu nhat
try{
do{
System.out.print("\n + Nhap canh hinh chu nhat (a,b): ");
a=inp.nextFloat();
b=inp.nextFloat();
if (a<=0||b<=0)
System.out.println("\n >> Nhap lai !");
}while(a<=0||b<=0);
// khoi tao doi tuong
hcn=new HinhChuNhat("Hinh chu nhat",a,b);
hcn.InKQ();
}catch(Exception e){
System.out.println("\n * Co loi xay ra ! Ma loi: "+e.toString());
}
// Hinh tam giac
try{
do{
System.out.print("\n + Nhap canh hinh tam giac (a,b,c): ");
a=inp.nextFloat();
b=inp.nextFloat();
c=inp.nextFloat();
if (a<=0||b<=0||c<=0||a+b<=c||a+c<=b||b+c<=a)
System.out.println("\n >> Nhap lai !");
}while(a<=0||b<=0||c<=0||a+b<=c||a+c<=b||b+c<=a);
// khoi tao doi tuong
tg=new HinhTamGiac("Hinh tam giac",a,b,c);
tg.InKQ();
}catch(Exception e){
System.out.println("\n * Co loi xay ra ! Ma loi: "+e.toString());
}
// Hinh tron
try{
do{
System.out.print("\n + Nhap ban kinh hinh tron: ");
r=inp.nextFloat();
if(r<=0)
System.out.println("\n >> Nhap lai !");
}while(r<=0);
// khoi tao doi tuong
ht=new HinhTron("Hinh tron",r);
ht.InKQ();
}catch(Exception e){
System.out.println("\n * Co loi xay ra ! Ma loi: "+e.toString());
}
}
}
//-------------------------------------------
[Download Project]
0 nhận xét:
Đăng nhận xét