0% found this document useful (0 votes)
1 views

Assignment 02

Uploaded by

Prince Kaswala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Assignment 02

Uploaded by

Prince Kaswala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment 02

Roll No:154 Name: Vedant P Ambwad Div: B Batch: B3

Program:

// GeometricObject.java

abstract class GeometricObject


{
private String color = "white"; // Default color private
boolean filled; // Default filled status

// No-arg constructor public


GeometricObject() {
}

// Constructor with parameters protected GeometricObject(String


color, boolean filled) { this.color
= color; this.filled
= filled;
}

// Accessor methods 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;
}

// Abstract methods public abstract


double getArea(); public abstract
double getPerimeter();
}

// TriangleException.java class
TriangleException extends Exception {
public TriangleException(String message) {
super(message);
}
}

// Triangle.java

class Triangle extends GeometricObject


{
private double side1;
private double side2;
private double side3;

// No-arg constructor public Triangle()


throws TriangleException {
this(1.0, 1.0, 1.0); // Default triangle
}

// Constructor with sides


public Triangle(double side1, double side2, double side3) throws
TriangleException {

super(); // Call to the superclass constructor if (side1 + side2


<= side3 || side1 + side3 <= side2 || side2 +
side3 <= side1) { throw new TriangleException("Invalid
triangle sides: " +
side1 + ", " + side2 + ", " + side3);
}
this.side1 = side1;
this.side2
= side2; this.side3
=
side3;
}

// Accessor methods public


double getSide1()
{
return side1;
}

public double getSide2() {


return side2;
}

public double getSide3() {


return side3;
}
// Override getArea() method
@Override public
double getArea() {
double s = (side1 +
side2 + side3) / 2; return
Math.sqrt(s * (s - side1)
* (s - side2) * (s -
side3));
}

// Override getPerimeter() method

@Override public double


getPerimeter() {
return side1 + side2 +
side3;
}

// Override toString() method


@Override
public String toString() {
return "Triangle with sides: " + side1 + ", " + side2 + ", " + side3
+
"\nColor: " + getColor()
+ "\nFilled: " +
isFilled();
}
}
// TriangleTest.java
import java.util.Scanner;

public class TriangleTest { public static


void main(String[] args) {
Scanner scanner = new
Scanner(System.in);

Try
{

System.out.println("Enter three sides of the triangle:"); double side1 =


scanner.nextDouble();
double side2 = scanner.nextDouble(); double side3 =
scanner.nextDouble();
System.out.println("Enter a color for the triangle:"); String
color = scanner.next(); System.out.println("Enter a color for
the triangle:");
String color = scanner.next();

System.out.println("Is the triangle filled (true/false)?");

boolean filled = scanner.nextBoolean();

Triangle triangle = new Triangle(side1, side2,


side3); triangle.setColor(color);
triangle.setFilled(filled);

System.out.println("Triangle created successfully!");


System.out.println("Area: " + triangle.getArea());
System.out.println("Perimeter: " + triangle.getPerimeter());
System.out.println(triangle);
} catch (TriangleException e) {
System.out.println("Attempted to create an illegal triangle: "
+ e.getMessage());
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
} finally {
scanner.close();
}
}
}

Output:

PS C:\Users\aditya\Desktop\Java> javac GeometricObject.java


Triangle.java TriangleTest.java
>>
PS C:\Users\aditya\Desktop\Java> java TriangleTest
>>
Enter three sides of the triangle:
345
Enter a color for the triangle:
blue
Is the triangle filled (true/false)?
true
Triangle created successfully!
Area: 6.0
Perimeter: 12.0
Triangle with sides: 3.0, 4.0,
5.0 Color: blue
Filled: true
Attempted to create an illegal triangle: Invalid triangle sides: 1.0, 2.0,
3.0
Conclusion:

The Java program successfully implements an object-oriented


approach to create a triangle, including validation for valid sides
using exceptions. It calculates and displays the triangle's area,
perimeter, color, and filled status, demonstrating effective error
handling and user input processing.

You might also like