Đề bài:
HCNInterface.java.
Code:
package HCNPackage; public interface HCNInterface { public abstract int ChuviHCN(); public abstract int DientichHCN(); public abstract int getChieuDai(); public abstract int getChieuRong(); public abstract void setDaiRong(int cd, int cr); }
Code:
import HCNPackage.HCNInterface; import java.util.Scanner; public class Hinhchunhat implements HCNInterface { private int chieudai; private int chieurong; public int ChuviHCN() { return (chieudai + chieurong) /2; } public int DientichHCN() { return chieudai*chieurong; } public int getChieuDai( ) { return chieudai; } public int getChieuRong() { return chieurong; } public void setDaiRong(int cd, int cr) { this.chieudai=cd; this.chieurong=cr; } }
HCNtest.java.
Code:
import java.util.Scanner; public class HCNtest extends Hinhchunhat { /*public int d,r; public HCNtest(int d,int r) { this.d=d; this.r=r; }*/ public static void main(String[] args) { int i,j,cd,cr; System.out.print("Nhap so luong hinh chu nhat:"); Scanner q=new Scanner(System.in); int n = q.nextInt(); //khai bao mang doi tuong khoang 20 phan tu; //HCNtest cn[]=new HCNtest[20]; //khai bao doi tuong hcn de lay ket qua tu Hinhchunhat Hinhchunhat[] hcn = new Hinhchunhat[n]; for(i=1;i<=n;i++) { System.out.println("Nhap thong tin cho HCN thu "+i); System.out.print("Nhap chieu dai:"); cd= q.nextInt(); System.out.print("Nhap chieu rong:"); cr= q.nextInt(); hcn[i].setDaiRong(cd,cr); //cn[i]=new HCNtest(cd,cr); } System.out.println("Stt \t Chieu Dai \t Chieu Rong \t Chu vi \t Dien tich"); for(i =1;i<=n;i++) { //hcn[i].setDaiRong(cn[i].d,cn[i].r); System.out.println(i+" \t\t " +hcn[i].getChieuDai()+" \t\t "+hcn[i].getChieuRong()+" \t\t\t "+hcn[i].ChuviHCN()+" \t\t "+hcn[i].DientichHCN()); } int Max=hcn[1].DientichHCN(); for(i=1;i<=n;i++) { if(hcn[i].DientichHCN()>Max) Max=hcn[i].DientichHCN(); } System.out.println("Hinh chu nhat co dien tich lon nhat la: "+Max); } }
Không có nhận xét nào:
Đăng nhận xét