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

SBL Assignment 2 Edited

The document contains Java code for a geometric object hierarchy, specifically a Triangle class that extends an abstract GeometricObject class. It includes methods for calculating area and perimeter, as well as input handling for triangle properties and validation of triangle sides. The test program demonstrates creating a valid triangle and handling exceptions for invalid triangles.

Uploaded by

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

SBL Assignment 2 Edited

The document contains Java code for a geometric object hierarchy, specifically a Triangle class that extends an abstract GeometricObject class. It includes methods for calculating area and perimeter, as well as input handling for triangle properties and validation of triangle sides. The test program demonstrates creating a valid triangle and handling exceptions for invalid triangles.

Uploaded by

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

Assignment 2

Prince Kaswala B3 153

CODE:

GeometricObject .java public abstract


class GeometricObject {
private String color = "white"; private boolean
filled;
public GeometricObject() {
this.color = "white"; this.filled
= false; } protected GeometricObject(String color,
boolean filled) {
this.color = color; this.filled = filled;
} public String
getColor() {
return color;
} public void setColor(String
color) {
this.color = color;
} public boolean
isFilled() {
return filled;
} public void setFilled(boolean
filled) {
this.filled = filled;
} public abstract double
getArea(); public abstract double
getPerimeter();
}

Triangle.java
public class Triangle extends GeometricObject
{
private double side1 = 1.0; private double side2
= 1.0; private double side3 = 1.0;
public Triangle() {
super();
}

// Constructor with specified sides, throws TriangleException if sides are


invalid public Triangle(double side1, double side2, double side3) throws
TriangleException { super(); if
(!isValidTriangle(side1, side2, side3)) { throw new
TriangleException("The sides do not form a valid
triangle."); } this.side1 = side1; this.side2 =
side2; this.side3 = side3;
} public double
getSide1() {
return side1;
} public double
getSide2() {
return side2;
} public double
getSide3() {
return side3;
}

@Override public double getArea() { double s = (side1 +


side2 + side3) / 2.0; return Math.sqrt(s * (s - side1) * (s -
side2) * (s - side3));
}

@Override public double getPerimeter()


{ return side1
+ side2 + side3;
}

@Override public String toString() { return "Triangle: side1 = "


+ side1 + " side2 = " + side2 + " side3 = " +
side3;
} private boolean isValidTriangle(double s1, double s2, double
s3) { return (s1 + s2 > s3) && (s2 + s3 > s1) && (s1 + s3 > s2);
}}

TriangleException.java
public class TriangleException extends Exception
{
public TriangleException(String message) { super(message);
}}
Test program:
import java.util.Scanner;
public class Test { public static void
main(String[] args) { Scanner input = new
Scanner(System.in);
// Prompt user for three sides of the triangle
System.out.print("Enter side1 of the triangle: "); double
side1 = input.nextDouble();
System.out.print("Enter side2 of the triangle: ");
double side2 = input.nextDouble();
System.out.print("Enter side3 of the triangle: ");
double side3 = input.nextDouble();
// Prompt for color and filled properties
System.out.print("Enter the color of the triangle: ");
String color = input.next();
System.out.print("Is the triangle filled (true/false): ");
boolean filled = input.nextBoolean();

// Try creating the triangle object with given sides


try {
Triangle triangle = new Triangle(side1, side2, side3);
triangle.setColor(color);
triangle.setFilled(filled);

// Display triangle properties


System.out.println("\nTriangle details:");
System.out.println("Area: " + triangle.getArea());
System.out.println("Perimeter: " + triangle.getPerimeter());
System.out.println("Color: " + triangle.getColor());
System.out.println("Filled: " + triangle.isFilled());
System.out.println(triangle.toString());
} catch (TriangleException ex) {
System.out.println("Error: " + ex.getMessage());
}

// Demonstration of an invalid triangle


System.out.println("\nTrying to create an invalid triangle:");
try {
Triangle invalidTriangle = new Triangle(1, 2, 3); // invalid triangle
sides
} catch (TriangleException ex) {
System.out.println("Error: " + ex.getMessage());
}

input.close();
}}

OUTPUT:
Enter side1 of the triangle: 3
Enter side2 of the triangle: 4
Enter side3 of the triangle: 5
Enter the color of the triangle: green
Is the triangle filled (true/false): true
Triangle details:
Area: 6.0
Perimeter: 12.0
Color: green
Filled: true
Triangle: side1 = 3.0 side2 = 4.0 side3 = 5.0
Trying to create an invalid triangle:
Error: The sides do not form a valid triangle.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy