0% found this document useful (0 votes)
20 views4 pages

Activity12 10 24

The document outlines the creation of a Java class named RectangleAreaCalculator, which includes a method to calculate the area of a rectangle given its length and width. It emphasizes input validation to ensure non-negative values and provides example usages in the main method to demonstrate the area calculation. The final output showcases the calculated area for specified dimensions.

Uploaded by

janrey1396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Activity12 10 24

The document outlines the creation of a Java class named RectangleAreaCalculator, which includes a method to calculate the area of a rectangle given its length and width. It emphasizes input validation to ensure non-negative values and provides example usages in the main method to demonstrate the area calculation. The final output showcases the calculated area for specified dimensions.

Uploaded by

janrey1396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ACTIVITY

12/10/24

Create a Java class name RectangleAreaCalculator

1. Define a method calculateArea that.


public class RectangleAreaCalculator {

// Method to calculate the area of a rectangle


public double calculateArea(double length, double width) {
// Check if the input values are valid
if (length < 0 || width < 0) {
throw new IllegalArgumentException("Length and width must be non-negative.");
}
return length * width;
}

public static void main(String[] args) {


RectangleAreaCalculator calculator = new RectangleAreaCalculator();

// Example usage
double length = 5.0;
double width = 4.0;
double area = calculator.calculateArea(length, width);
System.out.println("The area of the rectangle is: " + area);
}
}

2. Accepts two parameters length and width (both of type of double).


public class RectangleAreaCalculator {

// Method to calculate the area of a rectangle


public double calculateArea(double length, double width) {
// Validation: Check if length and width are non-negative
if (length < 0 || width < 0) {
throw new IllegalArgumentException("Length and width must be non-negative.");
}
return length * width; // Area calculation
}

public static void main(String[] args) {


// Create an instance of RectangleAreaCalculator
RectangleAreaCalculator calculator = new RectangleAreaCalculator();

// Example dimensions
double length = 6.0;
double width = 5.0;

// Calculate area
double area = calculator.calculateArea(length, width);

// Output the area


System.out.println("The area of the rectangle with length " + length + " and width "
+ width + " is: " + area);
}
}

3. Return the calculate area as a double.


public class RectangleAreaCalculator {

// Method to calculate the area of a rectangle and return it as a double


public double calculateArea(double length, double width) {
// Validation: Ensure that length and width are non-negative
if (length < 0 || width < 0) {
throw new IllegalArgumentException("Length and width must be non-negative.");
}

// Calculate area
double area = length * width;
return area; // Returning area as a double
}

public static void main(String[] args) {


// Create instance of RectangleAreaCalculator
RectangleAreaCalculator calculator = new RectangleAreaCalculator();
// Define example dimensions
double length = 7.0;
double width = 6.0;

// Calculate area and store the result


double area = calculator.calculateArea(length, width);

// Output the calculated area


System.out.println("The area of the rectangle with length " + length +
" and width " + width + " is: " + area);
}
}

Write a main method to:

1. Call the calculateArea method with example values


public class RectangleAreaCalculator {

// Method to calculate the area of a rectangle


public double calculateArea(double length, double width) {
// Validate that length and width are non-negative
if (length < 0 || width < 0) {
throw new IllegalArgumentException("Length and width must be non-negative.");
}
return length * width; // Calculate and return the area
}

public static void main(String[] args) {


// Create an instance of RectangleAreaCalculator
RectangleAreaCalculator calculator = new RectangleAreaCalculator();

// Example dimensions
double length = 8.5;
double width = 4.2;

// Call calculateArea method with example values


double area = calculator.calculateArea(length, width);

// Output the calculated area


System.out.println("The area of the rectangle with length " + length +
" and width " + width + " is: " + area);
}
}

2. Display the result.


The area of the rectangle with length 8.5 and width 4.2 is: 35.7

You might also like