basic java homework
DESCRIPTION
Java, Programming, GeometryTRANSCRIPT
1
HOMEWORK
Modern Programming Concepts in Engineering
CE: PO-04
Name : T. Arriessa Sukhairi
Matericulation Num : 108013235855
2
JAVA Homework
In order to do the Java Homework, I make 4 classes to make the basic task and some
additional class. There are:
1. Point3D Class.
2. Triangle Class.
3. Object Class.
4. Circle Class.
The UML diagram from this class is shown in figure 1.
In addition, to test the program is working or not I also make 3 test program classes:
1. TriangleTest Class.
2. ObjectTest Class.
3. Object3DTest Class.
Point3D Class
A class name Point3D which models of 3D is design as shown in the class diagram
(figure 1) this Class contains:
- Five private member variable x (double), y (double), z (double), PointName
(String), and EdgeName (String).
- A constructor that will construct a point with the given x, y and z coordinates.
- Getter and setter is using to set and get the coordinates value for the private
variables x, y and z.
- A method distance which returns the distance between one point to another given
point.
- Setter and getter PointName and EdgeName is using to set and get the name of the
Point and Edge of an Object in Object Class.
- drawPointName and drawEdgePoint is to draw the name of point and edge in the
Test Program.
3
Figure 1: UML Class Diagram
Triangle
- SideAB : double
- SideBC : double
- SideAC : double
+ Triangle (x1 : double, y1 : double,
z1 : double, x2 : double, y2 :
double, z2 : double, x3 : double, y3
: double, z3 : double)
+ Triangle (A : Point3D, B : Point3D,
C : Point3D)
+ draw (color : String, v : Viewer) :
void
+ getPerimeter ( ) : double
+ getArea ( ) : double
+ areaValue ( ) : double
+ Text ( color : String, v : Viewer) :
void
+ getCentroid ( color : String, v :
Viewer) : void
+ getIntersection ( T1 :Triangle,
color : String, v : viewer ) : void
Object
+ Object(Point3D [])
+ Object(int)
+ set (i : int, punkt : Point3D) : void
+ get ( index : int) : Point3D
+ getN ( ) : int
+ draw (color : String , v : Viewer) :
void
+ getArea ( ) : double
+ getCentroid ( ) : Point3D
+ drawCentroid (color : String, v :
Viewer) : void
+ getIntersection (T1 : Object, color
: String, v : Viewer) : void
+ cogGetCentroid (P1 : Object, color
: String, v : Viewer) : void
Point3D
- x : double
- y : double
- z : double
- PointName : String
- EdgeName : String
+ Point3D (x : double, y : double, z :
double)
+ getX ( ) : double
+ getY ( ) : double
+ getZ ( ) : double
+ setX ( ) : void
+ setY ( ) : void
+ setZ ( ) : void
+ distance (Point3D) : double
+ getPointName ( ) : String
+ setPointName (PointName : String) :
void
+ drawPointName (color : String, v :
Viewer) : void
+ getEdgeName ( ) : String
+ setEdgeName (EdgeName : String) :
void
+ drawEdgeName (P1 : Point3D, color :
String, v : Viewer) : void
Circle
- r : double
- Static N : int
+ Circle (x1 : double, y1 : double, z1 :
double, r : double)
+ getArea ( ) : double
-P
1
0..1
- punkt 0..*
- P1 0..1
- P2 0..1
- P3 0..1
4
Triangle Class
A class call Triangle is design as shown in the class diagram (figure 1). Triangle class
is a target class from Point3D. This class contains:
- 6 private member valuables, in which 3 of them are multiplicities from Point3D
class (P1, P2, P3) and the other member is SideAB, SideBC and SideAC.
- A constructor that construct a triangle with 3 points P1 (x1,y1,z1), P2 (x2,y2,z2),
P3 (x3,y3,z3).
- An overloaded constructor that construct a triangle given 3 instance of Point3D.
- A draw method that draw or show a triangle in the TriangleTest class.
- A getPerimeter ( ) method that returns the length of perimeter (sideAB, SideBC,
SideAC) in double. To compute the perimeter from triangle I used the equation:
πππ = πππππ΄π΅ + πππππ΅πΆ+πππππ΄πΆ
2 (1)
πππ = πππππππ‘ππ
πππππ΄π΅ = β(π₯π+1 β π₯π )2
+ (π¦π+1 β π¦π )2
+ (π§π+1 β π§π )2 (2)
Figure 2. getPerimeter method
- A getArea ( ) method that returns the area of triangle in double, by using equation:
π΄ = βπππ β (πππ β πππππ΄π΅) β (πππ β πππππ΅πΆ) β (πππ β πππππ΄πΆ) (3)
Figure 3. getArea method
- An areaValue ( ) method that store the value of area in double.
5
- A Text (color : String, v : Viewer) method is using to show the text of Area, Point
and Edge of aTriangle.
- A getCentroid (color : String, v : Viewer) method that draw or show centre of gravity
of a triangle. To get a centroid a centroid for a triangle I use the equation:
(4) Figure 4. The Centroid Formula (source: http://formulas.tutorvista.com/math/centroid-formula.html)
Figure 5. getCentroid Method
- A getIntersection ( ) method that find and show the intersection between two triangles. In this
triangle class we use an array only to find this intersection between two triangles. In this
method I use for loop. To get an intersection point I use an equation:
(5) Figure 6. get an Intersection equation ( source : Wikipedia)
6
Figure 6. getIntersection method
Figure 7. getIntersection Condition
Object Class
An object class is design to construct a lot of shape such as rectangle, star, L-Beam,
not only a triangle. These class contain a lot of Point. This class also a target class from
Point 3D. This class contain:
- A private valuable in which is a multiplicity from Point3D class.
- A constructor that can construct many Points.
7
- A set and get method here is used to set and get coordinate from the Point3D.
- A getN method is using to get a Point from another object.
- A draw method is used to draw or to show the shape in ObjectTest and Object3DTest
class.
- A getArea ( ) method that returns the area of an Object in double, by using polygon
equation :
(5) Figure 8. Area of Polygon Formula (source: Borke, Paul , 1988, calculating the area and centroid of a polygon).
Figure 9. getArea Method
getArea() : double
areaValue = 0
area = 0
i = 0; i < this.punkt.length - 1; i = i + 1
area = (xi * yi+1 - xi+1 * y1)
areaValue = (area / 2)
return areaValue
Figure 10. getArea Nassi-Schneiderman diagram
- A getCentroid method is used to get a center of gravity of an object. To get a centroid,
I use a formula to find a centroid in polygon.
8
(6)
Figure 11. get Centroid equation (source : : Borke, Paul , 1988, calculating the area and centroid of a polygon).
Figure 12. getCentroid Method
getCentroid() : Point3D
px = 0
py = 0
i = 0; i < this.punkt.length - 1; i = i + 1
px = 1/6A (xi + xi+1) (xi * yi+1 - xi+1 * y1)
py = 1/6A (yi + yi+1) (xi * yi+1 - xi+1 * y1)
return Point3D (px,py,0.0)
Figure 13. getCentroid Nassi-Schneiderman
- A drawCentroid method is used to show the centre of gravity from an Object in Test
class.
- A getIntersection method is used to get and show the intersection point between an
object with another object. To get an intersection point I use the same formula that I
have applied in Triangle class.
9
Figure 14. getIntersection method
Figure 15. getIntersection condition
- A cogGetCentroid is a method to get and show the centroid between two Objects in
the test class. To get a centroid between two objects I use equation :
ππππ₯ = (π₯1 β π΄1)(π₯2 β π΄2)
π΄1 + π΄2
ππππ¦ = (π¦1 β π΄1)(π¦2 β π΄2)
π΄1 + π΄2
(7)
Figure 16 . cogGetCentroid method.
10
Circle Class
A class call Circle have connection with two classes. First of all this class also one of
the target class from Point3D. Secondly, this class is extends class from Object class. In
this class only contain 2 method, because the other method can be operated from the
Object class. This class contain:
- Three private member variable P1 (Point3D), r (double), N =100 (int).
- A constructor that construct a circle with one center point P1 (x1,y1,z1), r as a radius,
and N the number of point that use in calculation to construct the outline of circle.
Figure 17. How to construct circle from polygon.
To construct a circle I use the equation:
ππ₯ = ππ₯ + π β cos(2 β π β π
π β 1)
ππ¦ = ππ¦ + π β cos(2 β π β π
π β 1)
Thus:
r = radius.
i = counts from 0 to N β 1.
N = the number of Points.
11
Px = Center Point of circle in x coordinate.
Py = Center Point of circle in y coordinate.
Figure 18. Circle constructor method
- A getArea ( ) method that returns the area of triangle in double, by using equation :
π΄πππ = ππ2 (8)
Figure 19. circle getArea method.
TriangleTest Class
TriangleTest
+ static main (String [] ) : void
Figure 20. TriangleTest UML diagram
This class is constructed to test the Triangle class. To draw a Triangle in this class, I
have to input a coordinate from each points of triangle to draw a triangle. Then, construct
a triangle by using that point.
Figure 21.input point
12
To draw a triangle and get some parameter that I want to know, I have to put an input
such as getArea, getPerimeter, getCentroid, getIntersection and Text.
Figure 22. parameter input
To print and to show the parameter, I have to type the system.out.println and viewer
set visible (true) command.
Figure 23. system.out.println command
Figure 24. output from the program
13
ObjectTest Class
ObjectTest
+ static main (String [] ) : void
Figure 25. UML digram ObjectTest Class
This class is constructed to test the Object class. In this class I can construct many
object with input a coordinate from each points of the object. Then, construct an by using
that point.
14
Figure 26. ObjectTest Class
Figure 27. Output from Object Class
Object3DTest Class
Object3DTest
+ static main (String [] ) : void
Figure 28. Object3DTest UML diagram
This class is constructed to test the Object class in 3D shape. I have to input a coordinate
from each points and then construct it to be a 3D object.
15
Figure 29. Coordinate and object input
I have to give an input to set the name of Point and edge between two Points. Then, I
have to introduce viewer and set it visible command to show the text and the 3D object.
Figure 30. Set Name of Point
16
Figure 31. The output of Point3D program