By GokiSoft.com| 09:18 31/10/2020|
Java Basic

[Share Code] Java basic- Test 30 phút - Lập trình hướng đối tượng java BT2054

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

/**
 *
 * @author student
 */
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;
    }
    
}


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

/**
 *
 * @author student
 */
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;
    }
    
}


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

/**
 *
 * @author student
 */
public class Main {
    public static void main(String[] args) {
        HinhTron hinhTron = new HinhTron(12.5f);
        HinhChuNhat hcn = new HinhChuNhat(5.2f, 7.6f);
        
        Shape[] list = new Shape[2];
        list[0] = hinhTron;
        list[1] = hcn;
        
        double total = tinhTongDienTich(list);
        
        System.out.println("Tong dien tich: " + total);
    }
    
    public static double tinhTongDienTich(Shape[] list) {
        double total = 0;
        for (Shape shape : list) {
            total += shape.tinhDienTich();
        }
        
        return total;
    }
}


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

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


Tags:

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

https://gokisoft.com/2054

Bình luận