By GokiSoft.com|
19:34 04/07/2022|
Java Basic
[Source Code] Java Basic- OOP - Interface - Hình chữ nhật trong java - C2108L
Java Basic- OOP - Interface - Hình chữ nhật trong java
#HinhChuNhat.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt995;
/**
*
* @author Diep.Tran
*/
public class HinhChuNhat implements HCNInterface{
double chieudai, chieurong;
@Override
public double dientichHCN() {
return chieudai * chieurong;
}
@Override
public double getChieuDai() {
return chieudai;
}
@Override
public double getChieuRong() {
return chieurong;
}
@Override
public void setDaiRong(double cd, double cr) {
chieudai = cd;
this.chieurong = cr;
}
}
#HCNTest.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt995;
import java.util.Collections;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class HCNTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so HCN = ");
int n = Integer.parseInt(scan.nextLine());
HinhChuNhat[] hcnList = new HinhChuNhat[n];
//Nhap thong tin N HCN
double cd, cr;
for (int i = 0; i < n; i++) {
System.out.println("Nhap chieu dai: ");
cd = Double.parseDouble(scan.nextLine());
System.out.println("Nhap chieu rong: ");
cr = Double.parseDouble(scan.nextLine());
// HinhChuNhat hcn = new HinhChuNhat();
// hcn.setDaiRong(cd, cr);
// hcnList[i] = hcn;
hcnList[i] = new HinhChuNhat();
hcnList[i].setDaiRong(cd, cr);
}
System.out.println("Thong tin N HCN");
for (HinhChuNhat hcn : hcnList) {
System.out.println("Chieu dai: " + hcn.getChieuDai() +
", chieu rong: " + hcn.getChieuRong() +
", dien tich: " + hcn.dientichHCN()
);
}
//Tim HCN co dien max
int maxIndex = 0;//Vi tri phan tu co dien tich max
double maxDientich = hcnList[maxIndex].dientichHCN();
for (int i = 1; i < hcnList.length; i++) {
double dt = hcnList[i].dientichHCN();
if(maxDientich < dt) {
maxIndex = i;
maxDientich = dt;
}
}
System.out.println("Thong tin HCN max: ");
HinhChuNhat hcn = hcnList[maxIndex];
System.out.println("Chieu dai: " + hcn.getChieuDai() +
", chieu rong: " + hcn.getChieuRong() +
", dien tich: " + hcn.dientichHCN()
);
}
}
#HCNInterface.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bt995;
/**
*
* @author Diep.Tran
*/
public interface HCNInterface {
double dientichHCN();
double getChieuDai();
double getChieuRong();
void setDaiRong(double cd, double cr);
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)