Answer to Question #202378 in Java | JSP | JSF for Shema Derrick

Question #202378

Write a java program using class called Rectangle and an object called rect. This class should have four members: two data members of type int with private access and two member functions with public access: set_values() and area().Set_values() to initialize the values of rectangle and area() to return the area of rectangle. Then finally write a main function to implement the above class


1
Expert's answer
2021-06-02T15:22:34-0400
public class Rectangle {
    private int width;
    private int height;

    public void set_values(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public int area() {
        return width * height;
    }
}


public class Main {
    public static void main(String[] args) {
        Rectangle rect = new Rectangle();
        rect.set_values(10, 6);
        System.out.println(rect.area());
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS