Starting from:

$30

IT124-Program 4 Point and Triangle Class Solved

Create a point class

·        Each point consists of the X,Y coordinates

·        Point p1 = new Point(3.4, 5.7);

·        There will be two attributes: (xValue and yValue; each of type double)

·        There will be three public methods: getX, getY and equals, which returns a Boolean when called with p1.equals(p2)

 

Create a triangle class

·        Constructor accepts the name (String) and 3 points as vertices.

·        Triangle myTri[0] = new Triangle(name,p1, p2, p3);

·        The attributes of a triangle will be:

o   name (a string)

o   vert1, vert2, vert3 (each of type Point)

o   side1, side2, side3 (each of type double)

·        There will be a private method:

o   sideLength(p1,p2) will return the distance between two points. This will be called from the constructor.

·        There will be two public methods:

o   myTri[0].listVertices() will result in printing the vertices’ coordinates, nicely formatted.

o   myTri[0].listSides() will result in printing the triangle’s sides’ lengths, nicely formatted.

 

Methods:

 

·        sideLength(p1,p2) will return the distance between two points. This will be called from the constructor.

·        printName() will return the name of the triangle as a nicely formatted string.

·        listVertices() will return the vertices’ coordinates as a nicely formatted string.

·        listSides() will return the triangle’s sides’ lengths as a nicely formatted 3-line string.

 

main

·        read in a line of data from points.txt (see GetPoints)

·        this data will be used to create three points

·        throw an exception if the three points are collinear or there are duplicates

·        the three points will be used to create a triangle

·        the triangle will be placed in an arraylist of triangles (named myTri)

·        each of the methods (above) will be applied to the triangle

·        the triangle’s data will be printed

·        this will be repeated for multiple triangles, until the end of the data file is reached

 

 


 

sample output for each triangle

 

The name of the triangle is Sally.

Vertices: (1.4, 2.3), (3.2, 4.1), (5.0, 6.9)

Side 1 is 5.84

Side 2 is 3.33

Side 3 is 2.55

 

sample input file (Note: The data file will have one space to separate items.)

 

Sally 1.4 2.3 3.2 4.1 5.0 6.9

Mickey -4 0 4 0 0 6.9282

FredEQ -2.161 -3.366 2.161 3.366 -5.83 3.743

Bob 3.54 5.46 -4.54 5.557 3.1 -2.1

Alex 0 4 0 2 0 -2

Randi -4 3 1 3 6 3

 

 

The input file will be named triInput.txt (Hard-code the name into your program.)

 

The output should be displayed on the screen and written to the file called XXmyTri.txt

More products