Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Tính chu vi diện tích hình chữ nhật và hình vuông

3 năm trước 1.252 lượt xem 1 bình luận

E mới học ai giúp e với ạ. E làm mãi k ra

viết 1 chương trình java: 
_nhập vào chiều dài và chiều rộng của 1 hcn
_in ra màn hình chu vi và diện tích của hcn đó
_cho biết hcn đó có phải là hv hay không.
_lưu ý: tạo ra class hcn và quy định các thuộc tính và hàm(hàm khởi tạo ko tham số và có tham số,getter setter, hàm tính chu vi tính di tích,
boolean).

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
Gkan đã bình luận 3 năm trước

import java.util.Scanner;

class Rectangle {

    private double length;
    private double width;

    public Rectangle() {
    }

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

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

    public double getPerimeter() {
        return (length + width) * 2;
    }

    public double getArea() {
        return length * width;
    }

    public boolean isSquare() {
        return length == width;
    }
}

public class Solution1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Length: ");
        double l = Double.parseDouble(sc.nextLine());
        System.out.print("Width: ");
        double w = Double.parseDouble(sc.nextLine());
        Rectangle rectangle = new Rectangle(l, w);
        System.out.println("Perimeter: " + rectangle.getPerimeter());
        System.out.println("Area: " + rectangle.getArea());

        System.out.println(rectangle.isSquare() == true ? "This is a square" : "Not a square");
    }
}

 

Câu hỏi mới nhất