Introduction
In this guide, we will create separate Java programs to calculate the area of a square, rectangle, and triangle. Each program will prompt the user to enter the necessary dimensions for the respective shape and then compute and display the area.
Problem Statement
Create separate Java programs that:
- Calculate the area of a square given the length of its side.
- Calculate the area of a rectangle given its length and width.
- Calculate the area of a triangle given its base and height.
Example 1: Area of Square
- Input: Side =
5
- Output: Area =
25
Example 2: Area of Rectangle
- Input: Length =
10
, Width =5
- Output: Area =
50
Example 3: Area of Triangle
- Input: Base =
8
, Height =6
- Output: Area =
24
Program 1: Calculate the Area of a Square
Java Program
import java.util.Scanner;
/**
* Java Program to Calculate Area of Square
* Author: https://www.javaguides.net/
*/
public class SquareAreaCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt for the side length of the square
System.out.print("Enter the side of the square: ");
double side = scanner.nextDouble();
// Calculate the area of the square
double squareArea = side * side;
// Display the result
System.out.println("Area of the square: " + squareArea);
}
}
Output Example
Enter the side of the square: 5
Area of the square: 25.0
Explanation
- Input: The program prompts the user to enter the side length of the square.
- Calculation: It calculates the area using the formula ( \text{Area} = \text{side} \times \text{side} ).
- Output: The calculated area is then displayed to the user.
Program 2: Calculate the Area of a Rectangle
Java Program
import java.util.Scanner;
/**
* Java Program to Calculate Area of Rectangle
* Author: https://www.javaguides.net/
*/
public class RectangleAreaCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt for the length and width of the rectangle
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = scanner.nextDouble();
// Calculate the area of the rectangle
double rectangleArea = length * width;
// Display the result
System.out.println("Area of the rectangle: " + rectangleArea);
}
}
Output Example
Enter the length of the rectangle: 10
Enter the width of the rectangle: 5
Area of the rectangle: 50.0
Explanation
- Input: The program prompts the user to enter the length and width of the rectangle.
- Calculation: It calculates the area using the formula ( \text{Area} = \text{length} \times \text{width} ).
- Output: The calculated area is displayed to the user.
Program 3: Calculate the Area of a Triangle
Java Program
import java.util.Scanner;
/**
* Java Program to Calculate Area of Triangle
* Author: https://www.javaguides.net/
*/
public class TriangleAreaCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt for the base and height of the triangle
System.out.print("Enter the base of the triangle: ");
double base = scanner.nextDouble();
System.out.print("Enter the height of the triangle: ");
double height = scanner.nextDouble();
// Calculate the area of the triangle
double triangleArea = 0.5 * base * height;
// Display the result
System.out.println("Area of the triangle: " + triangleArea);
}
}
Output Example
Enter the base of the triangle: 8
Enter the height of the triangle: 6
Area of the triangle: 24.0
Explanation
- Input: The program prompts the user to enter the base and height of the triangle.
- Calculation: It calculates the area using the formula ( \text{Area} = 0.5 \times \text{base} \times \text{height} ).
- Output: The calculated area is displayed to the user.
Conclusion
These Java programs provide a simple and effective way to calculate the area of a square, rectangle, and triangle individually. By separating the programs, each is easy to understand, test, and maintain. This approach also allows for easy modifications or enhancements specific to each shape's area calculation.
Related Java Programs
String Programs in Java with OutputJava Program to Count Number of Duplicate Words in String
Java Program to Count Number of Words in Given String
Java Program to Count the Number of Occurrences of Substring in a String
Java Program to Count the Occurrences of Each Character in String
Java Program to Merge Two String Arrays
Java Program to Remove Duplicate Words from String
Java Program to Reverse a String(5 ways)
Java Program to Reverse Each Word of a String
Java Program to Swap Two Strings
How to Check if the String Contains Only Digits
How to Check if the String Contains Only Letters
How to Check If the String Contains Only Letters or Digits
Java Program to Check if Input String is Palindrome
Java Program to Find all Permutations of String
How to Remove or Trim All White Spaces from a String in Java
How to Remove Leading and Trailing White Space From a String in Java
Java Program to Count Duplicate Characters in a String
Remove Character from String in Java (Java 8)
Java Program to Count Vowels and Consonants in a String (Java 8)
4 Ways to Find First Non-Repeated Character in String in Java
Comments
Post a Comment
Leave Comment