Write a program to print the area of a rectangle by creating a class named 'Area' having two methods. First method named as 'setDim' takes length and breadth of rectangle as parameters and the second method named as 'getArea' returns the area of the rectangle. Length and breadth of rectangle are entered through keyboard.
Question
Write a program to print the area of a rectangle by creating a class named 'Area' having two methods. First method named as 'setDim' takes length and breadth of rectangle as parameters and the second method named as 'getArea' returns the area of the rectangle. Length and breadth of rectangle are entered through keyboard.
Solution
Here is a simple Python program that accomplishes this:
class Area:
def setDim(self, length, breadth):
self.length = length
self.breadth = breadth
def getArea(self):
return self.length * self.breadth
# Create an instance of the Area class
rectangle = Area()
# Set the dimensions of the rectangle
length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: "))
rectangle.setDim(length, breadth)
# Get and print the area of the rectangle
print("The area of the rectangle is: ", rectangle.getArea())
In this program, we first define a class Area with two methods: setDim and getArea. setDim is used to set the dimensions of the rectangle (length and breadth), and getArea is used to calculate and return the area of the rectangle.
We then create an instance of the Area class, set the dimensions of the rectangle using user input, and finally calculate and print the area of the rectangle using the getArea method.
Similar Questions
Area of a RectangleCreate a Java program that calculates the area of a rectangle using variables to store the length and width.Print out the result.Constraints:NAExample:Input:5.04.0Output:20.0
If Write an algorithm that prints the rectangle size given width and length. A= W* LInput: Area of rectangle (A)Output: Width (W) and Length (L)Processing operations: A= W * L
Create an abstract class named shape that contains two integers and an empty method named printarea().Provide three classes named rectangle, triangle and circle such that each one of the classes extends the class Shape. Each of the inherited class from shape class should provide the implementation for the method printarea(). Get the input and calculate the area of rectangle, circle and triangle.In the Main class, create the objects for the three inherited classes and invoke the methods and display the area values of the different shapes.2(length of rectangle)3(breadth)5(triangle breadth)6(Triangle height)4(circle radius)
Select the correct answerWhat will be the output of the following Java code?class rectangle { int width; int length; int area; void area(int width, int length) { this.width = width; this.length = length; } } class Output { public static void main(String args[]) { area obj = new area(); obj.area(5 , 6); System.out.println(obj.length + " " + obj.width); } }Options6 55 630 00 30
Create a method display() that display the length, width, perimeter and area of an object created using an instantiation on rectangle class.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.