JAVA - Hình Thoi

JAVA - Hình Thoi

 

Question:


use java


Answer:


import java.util.Scanner;

 

class Rhombus {

    // side of rhombus

    private int side;

    // diagonals of rhombus

    private int d1, d2;

    private boolean filled;

    private String color;

 

    // constructor

    Rhombus(int side, int d1, int d2) {

        this.side = side;

        this.d1 = d1;

        this.d2 = d2;

    }

 

    public void setColor(String color) {

        this.color = color;

    }

 

    public void setFilled(boolean filled) {

        this.filled = filled;

    }

 

    public String toString() {

        return "Rhombus: side = " + side + ", diagonal 1 = " + d1 + ", diagonal 2 = " + d2;

    }

 

    // getters

    public String getColor() {

        return color;

    }

 

    public boolean getFilled() {

        return filled;

    }

 

    // perimeter

    public double getPerimeter() {

        return side * 4;

    }

 

    // area

    public double getArea() {

        return d1 * d2 / 2;

    }

 

    public static void main(String[] args) {

        // read side and diagonals

        System.out.print("Enter the side and two diagonals of a rhombus: ");

        int side, d1, d2;

        Scanner sc = new Scanner(System.in);

        side = sc.nextInt();

        d1 = sc.nextInt();

        d2 = sc.nextInt();

        System.out.print("Enter a color: ");

        String color = sc.next();

        System.out.print("Enter a boolean value to indicate if the triangle is filled: ");

        boolean filled = sc.nextBoolean();

        // create rhombus

        Rhombus rh = new Rhombus(side, d1, d2);

        // set color and filled

        rh.setColor(color);

        rh.setFilled(filled);

        // print

        System.out.println(rh);

        System.out.println("Area: " + rh.getArea());

        System.out.println("Perimeter: " + rh.getPerimeter());

        System.out.println("Color: " + rh.getColor());

        System.out.println("Filled: " + rh.getFilled());

    }

}

.

Output:


.

Previous Post
Next Post

post written by:

Related Posts

0 Comments: