Here's a simple Java program that calculates the area and perimeter of a rectangle. It includes clear comments to explain each part:
// This program demonstrates basic arithmetic operations in Java
// to calculate properties of a rectangle (area and perimeter)
public class RectangleCalculator {
public static void main(String[] args) {
// Define dimensions of the rectangle (sample values)
double length = 5.5;
double width = 3.2;
// Calculate area of rectangle: length * width
double area = length * width;
// Calculate perimeter of rectangle: 2 * (length + width)
double perimeter = 2 * (length + width);
// Print results to the console
System.out.println("Rectangle Dimensions:");
System.out.println("Length = " + length + " units");
System.out.println("Width = " + width + " units");
System.out.println("Area = " + area + " square units");
System.out.println("Perimeter = " + perimeter + " units");
}
}
When you run this program, it will output:
Rectangle Dimensions:
Length = 5.5 units
Width = 3.2 units
Area = 17.6 square units
Perimeter = 17.4 units
This program is beginner-friendly and shows core Java concepts like variables, basic arithmetic, and printing output. You can modify the length and width values to test different rectangles!


作者声明:本文包含人工智能生成内容。