public class Rectangle {
int width, height; // Declare instance variables
// Constructor to initialize the instance variables
public Rectangle(int w, int h) {
width = w;
height = h;
}
// Method to calculate the area of the rectangle
int getArea() {
return width * height;
}
// Main method to run the program
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(2, 3); // Create a new Rectangle object
System.out.println(rectangle.getArea()); // Output the area of the rectangle
}
}class Rectangle:
def __init__(self, w=0, h=0):
self.width = w
self.height = h
def get_area(self):
return self.width * self.height
# Create a new Rectangle object
rectangle = Rectangle(2, 3)
# Output the area of the rectangle
print(rectangle.get_area())class Rectangle {
constructor(w, h) {
this.width = w; // Initialize the width property
this.height = h; // Initialize the height property
}
// Method to calculate the area of the rectangle
getArea() {
return this.width * this.height;
}
}
// Create a new Rectangle object
let rectangle = new Rectangle(2, 3);
// Output the area of the rectangle
console.log(rectangle.getArea());课程目录