By GokiSoft.com| 19:13 01/07/2022|
Java Basic

[Source Code] Bài tập kế thừa hình tròn + hình chữ nhật - lập trình Java căn bản - Đề 2 - C2108L BT2995

Bài tập kế thừa hình tròn + hình chữ nhật - lập trình Java căn bản - Đề 2



#Shape.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 bt1135;

/**
 *
 * @author Diep.Tran
 */
public abstract class Shape {
    public abstract double tinhChuVi();
    public abstract double tinhDienTich();
}


#Main.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 bt1135;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        HinhTron ht = new HinhTron(2.6);
        HinhChuNhat hcn = new HinhChuNhat(5.6, 9.2);
        
        Shape[] list = new Shape[2];
//        Shape list[] = new Shape[2];
        list[0] = ht;
        list[1] = hcn;
        
        double sum = tinhTongDienTich(list);
        
        System.out.println("Tong dien tich: " + sum);
    }
    
    public static double tinhTongDienTich(Shape[] list) {
        double sum = 0;
        
        for (Shape shape : list) {
            sum += shape.tinhDienTich();
        }
        
        return sum;
    }
}


#HinhTron.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 bt1135;

/**
 *
 * @author Diep.Tran
 */
public class HinhTron extends Shape{
    double radius;

    public HinhTron() {
    }

    public HinhTron(double radius) {
        this.radius = radius;
    }

    @Override
    public double tinhChuVi() {
        return Math.PI * 2 * radius;
    }

    @Override
    public double tinhDienTich() {
        return Math.PI * radius * radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }
}


#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 bt1135;

/**
 *
 * @author Diep.Tran
 */
public class HinhChuNhat extends Shape{
    double width, height;

    public HinhChuNhat() {
    }

    public HinhChuNhat(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double tinhChuVi() {
        return (width + height) * 2;
    }

    @Override
    public double tinhDienTich() {
        return width * height;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}


Tags:

Liên kết rút gọn:

https://gokisoft.com/2995

Bình luận