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

Question #201816

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-02T03:01:34-0400
class Rectangle {
	// four members: two data members of type int with private access
	private int width;
	private int height;


	/**
	 * Set_values() to initialize the values of rectangle
	 * 
	 * @param width
	 * @param height
	 */
	public void Set_values(int width, int height) {
		this.width = width;
		this.height = height;
	}


	/**
	 * area() to return the area of rectangle.
	 * 
	 * @return
	 */
	public int area() {
		return this.width * this.height;
	}
}


public class Q201816 {


	public static void main(String[] args) {
		Rectangle rect = new Rectangle();
		rect.Set_values(5, 4);
		System.out.println("Width = 5");
		System.out.println("Height = 4");
		System.out.println("Rectangle area: " + 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