Java Documentation - Iddrisu Crispin Masahudu.docx

download Java Documentation - Iddrisu Crispin Masahudu.docx

of 7

Transcript of Java Documentation - Iddrisu Crispin Masahudu.docx

Name: Iddrisu Crispin MasahuduMODERN PROGRAMMING CONCEPTS IN ENGINEERING Student ID: 108014270893

HOMEWORK: JAVA DOCUMENTATIONIn addition to the Java libraries, the following libraries were used to complete the tasks: inf.v3d.obj inf.v3d.view Two (2) Classes are also created (in package homeworkAssignment) to complete the Basic Tasks: Triangle Class Test ClassThree (3) additional Classes are (in package additionalTasks) created to complete a few Additional Tasks: Circle Class Cuboid Class Parallelogram ClassTriangle ClassUML DiagramTriangle

- x1 : double- y1 : double- x2 : double- y2 : double- x3 : double- y3 : double

+ Triangle(x1:double, y1:double, x2:double, y2:double, x3:double, y3:double)- getx1(): double- gety1(): double- getx2(): double- gety2(): double- getx3(): double- gety3(): double+ draw(color: String, v: Viewer): void+ intersection(x1:double, y1:double, x2:double, y2:double, x3:double, y3:double, x4: double, y4: double) : Point2D.Double+ getIntersection(t: Triangle, v: Viewer): void+ centroid(v: Viewer): void+ getArea(v: Viewer): void+ getPerimeter(v: Viewer): void+ showLabels(v: Viewer): void+ getAngle_AB(v: Viewer): void+ getAngle_AC(v: Viewer): void+ getAngle_BC(v: Viewer): void

Table 1

The following imports are made for the creation of this class: import inf.v3d.obj.Polyline; import inf.v3d.obj.Sphere; import inf.v3d.obj.Text; import inf.v3d.view.Viewer; import java.awt.geom.Point2D;The Triangle class is created with the following attributes:private double x1;which is the x coordinate of vertex 1.private double y1;which is the y coordinate of vertex 1.private double x2;which is the x coordinate of vertex 2.private double y2;which is the y coordinate of vertex 2.private double x3;which is the x coordinate of vertex 3.private double y3; which is the y coordinate of vertex 3.

Triangles are therefore constructed using the constructor below: public Triangle (double x1, double y1, double x2, double y2, double x3, double y3).METHODS:Firstly, private get methods are created to enable easy access to the attributes of any constructed triangle within the Triangle Class. The other methods in the Triangle class are explained in detail below.

Draw METHOD:Triangles can be drawn in this class with: public void draw (String color , Viewer v). This method requires an input of the color (of the Triangle), and the Viewer in which the Triangles should be drawn.

Intersection METHOD:The next method in the Triangle Class is a method which determines, returns, and prints out the intersection point of two (2) lines. This method is initialized with:public Point2D.Double intersection (double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4), where double x1, is the x coordinate at point 1 of line 1. double y1, is the y coordinate at point 1 of line 1. double x2, is the x coordinate at point 2 of line 1. double y2, is the y coordinate at point 2 of line 1. double x3, is the x coordinate at point 1 of line 2. double y3, is the y coordinate at point 1 of line 2. double x4, is the x coordinate at point 2 of line 2. double y4, is the y coordinate at point 2 of line 2.The literature and idea behind this approach to solving the intersection of lines was found on the internet, and modified for this task. A summary of the idea is given below in Fig. 1:

Fig. 1Putting this into a code however, returns several values of xi and yi, which include values greater than and less than the input values of x-coordinates and y-coordinates. However, logically, there can be only one intersection point between two straight lines, and the intersection point should fall between the maximum and minimum values of all the inputs for x and y coordinates. This is done in the code with this expression:

Fig. 2The xi value that satisfies this condition is stored in an array point [0] and the yi in an array point [1]. These two values are then returned using: return new Point2D.Double(point[0], point[1]). This completes the method.

GetIntersection METHOD:The next method in the Triangle class is a method which determines the intersection point(s) between two Triangles, prints the point(s) of intersection and draws a Sphere(with radius = 0.15) at the point(s) of intersection. This method requires an input of a Triangle, and the Viewer in which the Triangles are to be drawn. This method is initialized with: public void getIntersection (Triangle t, Viewer v).There are nine (9) possible intersection points between the six (6) lines of the two (2) Triangles. This nine possible line intersection points are created using: public Point2D.Double intersection method above. These points are then stored in an Array of: Point2D.Double [] ip = {ip1, ip2, ip3, ip4, ip5, ip6, ip7, ip8, ip9};A for each loop of IntersectionPoints is then used to draw spheres at every intersection point which doesnt have coordinates (0, 0). This condition eliminates the inclusion of points (0, 0) to the intersection points since the for each loop returns all possible nine intersection points (i.e with (0, 0) inclusive). Fig. 3 shows the code of this method.

Fig. 3 GetCentroid METHOD:The getCentroid method in the Triangle Class determines the centroid of any Triangle constructed with the Triangle Class, prints the centroid point, and draws a Text C in the Viewer at this point. This method therefore requires a Viewer input. Mathematically, the centroid of any triangle, C is given by: C =. The code of this method is below in Fig. 4.

Fig. 4 GetArea METHOD:The getArea method determines, prints out, and draws the area of any Triangle contructed with the Triangle Class, into the viewer. This method also therefore requires a Viewer input. The area of a Triangle with three (3) vertices is mathematically is given by:A =. The code of this method is below in Fig. 6.

Fig. 5 GetPerimeter METHOD:The getPerimeter method determines, prints out, and draws the perimeter of any Triangle contructed with the Triangle Class, into the viewer. The Perimeter is computed by determining and summing the mangnitude of all three sides of a Triangle.

showLabels METHOD:The showLabels method draws Labels A B and C close to the Vertices of a Triangle constructed with the Triangle Class, into the viewer. GetAngles METHODs:The getAngle methods method determines, and prints out the angle between any two (2) sides of any Triangle contructed with the Triangle Class. The Cosine Rule is used to mathematically compute these angles in the program. Cosine Rule: a2 = b2 + c2 -2bc (cosA), where a, b, and c are the magnitude of the three sides of the Triangle and A is the angle between sides b and c.

Additional Task:Three (3) additional Classes are created to complete a few Additional Tasks: Circle Class Cuboid Class Parallelogram Class

Circle ClassUML DiagramCircle

- x : double- y : double- N = 200 : double- radius : double

+ Circle(x: double, y:double, radius: double)+ getX(): double+ getY(): double+ moveTo(x: double, y: double): void+ draw(v: Viewer): void+ getCircumference(v: Viewer): void+ getArea(v: Viewer): void

Table 2A Circle is created by approximating it to be an N-sided polygon using the Polyline Class (in the draw method), and its Circumference and Area are calculated using getCircumference and getArea methods respectively. The circles created can be moved to new coordinates using the moveTo method by inputting the new coordinates.

Cuboid ClassUML DiagramCuboid

- x : double- y : double- z : double- w : double- h : double- t : double

+ Cuboid(x: double, y:double, z: double, w: double, h: double, t: double)+ draw(v: Viewer): void+ getVolume(v: Viewer): void

Table 3A Cuboid is created by using the Polyline Class (in the draw method), and its Volume is calculated using getVolume method (by multiplying the length, width and height). This method draws the Volume next to the Cuboid in the Viewer.Parallelogram ClassUML DiagramParallelogram

- x : double- y : double- z : double- w : double- h : double- angle : double

+ Parallelogram(x: double, y:double, w: double, h: double, angle: double)+ draw(v: Viewer): void+ draw Vertices(v: Viewer): void+ getPoint1(): Point2D.Double+ getPoint2(): Point2D.Double+ getPoint3(): Point2D.Double+ getPoint4(): Point2D.Double+ getCentroid(v: Viewer): void+ getArea_Para(v: Viewer): void+ getPerimeter(v: Viewer): void

Table 4A Parallelogram is created by using the Polyline Class (in the draw method). The magnitude of the Slant side(s) = (h/sin (angle)). This then makes the drawing easier. The drawVertices method draws the labels of the vertices in the Viewer. The getPoint methods returns the coordinates of the vertices of the Parallelogram. Centroid, Area and Perimeter are calculated using the getCentroid, getArea_Para and getPerimeter methods respectively.

Test Class is used to test all the Classes (using two Viewers: viewer, and v).Page | 6