This project is a demonstration of how to handle user input in Java and use object-oriented programming (OOP) concepts such as inheritance and polymorphism. The program allows users to select a type of triangle, provide relevant inputs, and calculate properties such as area and perimeter.
- Scanner Class: The program uses the
Scanner
class to read user input from the console. - Input Prompts: The user is prompted to enter specific values (e.g., side lengths, angles) based on the type of triangle they select.
- Validation: The program assumes valid input for simplicity. In a real-world application, additional validation would be required to handle invalid or unexpected input.
- The user is asked to select a triangle type by entering a number (e.g.,
1
for Equilateral Triangle). - Based on the selection, the program prompts the user for specific inputs, such as:
- Side lengths for an equilateral triangle.
- Two sides and an angle for an SSA triangle.
- One side and two angles for an AAS triangle.
- Purpose: Serves as the base class for all triangle types.
- Fields:
sides
: An array to store the lengths of the triangle's sides.angles
: An array to store the triangle's angles in degrees.area
: Stores the calculated area of the triangle.perimeter
: Stores the calculated perimeter of the triangle.
- Methods:
getInputAndCalculate()
: Abstract method implemented by subclasses to handle input and calculations.toString()
: Formats the triangle's properties (sides, angles, area, and perimeter) for display.
- Purpose: Represents an equilateral triangle where all sides and angles are equal.
- Input: The user provides the length of one side.
- Calculations:
- Area is calculated using the formula:
[ \text{Area} = \frac{\sqrt{3}}{4} \times \text{side}^2 ] - Perimeter is calculated as:
[ \text{Perimeter} = 3 \times \text{side} ]
- Area is calculated using the formula:
- Purpose: Represents a triangle defined by two sides and the angle between them.
- Input: The user provides two side lengths and the angle (in degrees) between them.
- Calculations:
- Area is calculated using the formula:
[ \text{Area} = 0.5 \times a \times b \times \sin(\text{angle}) ] - Perimeter is partially calculated (third side is not determined in this demo).
- Area is calculated using the formula:
- Purpose: Represents a triangle defined by one side and two angles.
- Input: The user provides one side length and two angles (in degrees).
- Calculations:
- The third angle is calculated as:
[ \text{Angle}_3 = 180 - (\text{Angle}_1 + \text{Angle}_2) ] - Area is calculated using the formula:
[ \text{Area} = \frac{\text{side}^2 \times \sin(\text{Angle}_1) \times \sin(\text{Angle}_2)}{2 \times \sin(\text{Angle}_3)} ] - Perimeter is partially calculated (other sides are not determined in this demo).
- The third angle is calculated as:
- Purpose: Manages the user interaction and triangle selection process.
- Responsibilities:
- Displays a menu for the user to select a triangle type.
- Creates an instance of the selected triangle type.
- Calls the
getInputAndCalculate()
method of the selected triangle to handle input and calculations.
- Compile the project using Maven or your preferred IDE.
- Run the
Main
class. - Follow the prompts to:
- Select a triangle type.
- Enter the required inputs.
- View the calculated properties of the triangle (sides, angles, area, and perimeter).
- User Input: Using the
Scanner
class to read and process user input. - OOP Concepts: Demonstrating inheritance, polymorphism, and abstraction.
- Mathematical Calculations: Applying trigonometric functions to calculate triangle properties.
This demo is designed to help students understand how to structure programs using OOP principles and handle user input effectively in Java.