By GokiSoft.com|
14:06 21/09/2022|
Java Basic
[Source Code] Java Basic- OOP - Interface - Hình chữ nhật trong java - C2109I
Java Basic- OOP - Interface - Hình chữ nhật trong java
#HCNInterface.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson06.bt995;
/**
*
* @author diepvan
*/
public interface HCNInterface {
double dientichHCN();
double getChieuDai();
double getChieuRong();
void setDaiRong(double cd, double cr);
}
#HinhChuNhat.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson06.bt995;
/**
*
* @author diepvan
*/
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) {
this.chieudai = cd;
this.chieurong = cr;
}
}
#Main.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson06.bt995;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so HCN can them: ");
int N = Integer.parseInt(scan.nextLine());
HinhChuNhat[] hcnList = new HinhChuNhat[N];
for (int i = 0; i < N; i++) {
double cd, cr;
System.out.println("Nhap chieu dai: ");
cd = Double.parseDouble(scan.nextLine());
System.out.println("Nhap chieu rong: ");
cr = Double.parseDouble(scan.nextLine());
hcnList[i] = new HinhChuNhat();
hcnList[i].setDaiRong(cd, cr);
}
System.out.println("Thong tin HCN: ");
for (HinhChuNhat hcn : hcnList) {
System.out.println("Chieu dai: " + hcn.getChieuDai() + ", chieu rong: " +
hcn.getChieuRong() + "Dien tich: " + hcn.dientichHCN());
}
double max = hcnList[0].dientichHCN();
int index = 0;
for (int i = 1; i < N; i++) {
if(hcnList[i].dientichHCN() > max) {
max = hcnList[i].dientichHCN();
index = i;
}
}
HinhChuNhat hcn = hcnList[index];
System.out.println("Thong tin HCN co dien tich MAX");
System.out.println("Chieu dai: " + hcn.getChieuDai() + ", chieu rong: " +
hcn.getChieuRong() + "Dien tich: " + hcn.dientichHCN());
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)