-
Notifications
You must be signed in to change notification settings - Fork 0
Astavick/non-static-method-static-methods-and-a-constructor
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
package triangle_calculator;
import java.util.Scanner;
public class TriangleAreaCalculator {
// Constructor
public TriangleAreaCalculator() {
// Constructor logic can be added here if needed
}
// Non-static method to get input from the user and calculate the area
public void getInputAndCalculateArea() {
// Create Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Enter the base and height of the triangle
System.out.println("Enter the base of the triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the triangle:");
double height = scanner.nextDouble();
// Calculate the area of the triangle
double area = calculateArea(base, height);
// Output the calculated area of the triangle
System.out.println("The area of the triangle is: " + area);
scanner.close();
}
// Static method to calculate the area of a triangle
public static double calculateArea(double base, double height) {
return 0.5 * base * height;
}
// Static method to display a message
public static void displayMessage() {
System.out.println("This is a message from the TriangleAreaCalculator class.");
}
public static void main(String[] args) {
// Call the non-static method using an instance of the class
TriangleAreaCalculator calculator = new TriangleAreaCalculator();
calculator.getInputAndCalculateArea();
// Call the static method directly using the class name
displayMessage();
}
}
About
No description, website, or topics provided.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published