0% found this document useful (0 votes)
11 views5 pages

Patel Arth COSCLab2

The document describes a Java program that defines Triangle and GeometricObject classes. The Triangle class extends GeometricObject and defines properties for triangle sides, getter/setter methods, and methods to calculate perimeter and output triangle details. The GeometricObjectTest class tests the Triangle class by getting side input from the user, creating a Triangle object, and outputting its perimeter and details. The program allows the user to input a triangle's sides, color, and whether it is filled, and displays the perimeter and full triangle details.

Uploaded by

Arth Patel
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)
11 views5 pages

Patel Arth COSCLab2

The document describes a Java program that defines Triangle and GeometricObject classes. The Triangle class extends GeometricObject and defines properties for triangle sides, getter/setter methods, and methods to calculate perimeter and output triangle details. The GeometricObjectTest class tests the Triangle class by getting side input from the user, creating a Triangle object, and outputting its perimeter and details. The program allows the user to input a triangle's sides, color, and whether it is filled, and displays the perimeter and full triangle details.

Uploaded by

Arth Patel
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/ 5

Lab 2

Course: COSC 1047 Introduction to Computer Science II


Name: Arth Patel
Section: COSC 1047 J
Student number: 239548040
Q1
There are total two file that I created for the given question that I mentioned below.

Triangle.java

// create a Triangle class that extend Geometricobject


public class Triangle extends Geometricobject
{
private double side_1 = 1.0;
private double side_2 = 1.0;
private double side_3 = 1.0;
public Triangle(double side_1, double side_2, double side_3)
{
this.side_1 = side_1;
this.side_2 = side_2;
this.side_3 = side_3;
}

// no-arg constructor
public Triangle()
{
this.side_1 = 0;
this.side_2 = 0;
this.side_3 = 0;

//Get And Set Method


public double getSide_1()
{
return side_1;
}

public double getSide_2()


{
return side_2;
}

public double getSide_3()


{
return side_3;
}

public void setSide_1(double side_1)


{
this.side_1 = side_1;
}

public void setSide_2(double side_2)


{
this.side_2 = side_2;
}

public void setSide_3(double side_3)


{
this.side_3 = side_3;
}

// getPerimeter method
public double getPerimeter()
{
return this.side_1 + this.side_2 + this.side_3;
}

// toString method
public String toString()
{
return "\nside1 = " + side_1
+ "\nside2 = " + side_2
+ "\nside3 = " + side_3
+ "\ncolor = " + this.getColor()
+ "\nfilled = " + this.isFilled();
}

}
GeometricObjectTest.java

import java.util.Scanner;
class Geometricobject
{
private String color;
private boolean filled;

public Geometricobject(String color, boolean filled)


{
this.color = color;
this.filled = filled;
}

public String getColor()


{
return color;
}
public void setFilled(boolean filled)
{
this.filled = filled;
}

public void setColor(String color)


{
this.color = color;
}

public boolean isFilled()


{
return filled;
}

}
public class GeometricobjectTest
{

public static void main(String[] args)


{
// Scanner and use array for getting data from user
Scanner input = new Scanner(System.in);
double[] sides = new double[3];
for(int i = 0; i < 3; i++)
{
System.out.print("Side " + (i + 1) + " is :" );
sides[i] = input.nextDouble();

input.nextLine();
System.out.print("Is triangle filled (true/false): ");
boolean filled = input.nextBoolean();

System.out.print("Enter color: ");


String color = input.nextLine();

input.close();
Triangle triangle = new Triangle(sides[0], sides[1], sides[2]);
triangle.setColor(color);
triangle.setFilled(filled);

System.out.println("\n triangle Perimeter: " + triangle.getPerimeter());


System.out.println("\n Triangle Details: " + triangle);
}
}

Output:
Methodology:

Firstly, the Triangle class extends geometricobject class and its inherits color and
filled attributes. Instance variable represents the sides of the tringle that is Side1,
Side2, and Side3 and apply getter and setter method for this sides. After that, I used
getperimeter method for the calculation of triangle perimeter. At last, I use tostring
method for triangle properties.

GeometricObjectTest this class is used for the test of Triangle class. Where I use
array to get the sides of triangle from user. First user enter three side of Triangle and
after that input color and value for triangle fill with the color or not. Last, it prints
Triangle Perimeter and its detail.

Conclusion:

Overall, we create two different java file that use one of class details in other and
print the amount or output that user want.

You might also like