16
1 HOMEWORK Modern Programming Concepts in Engineering CE: PO-04 Name : T. Arriessa Sukhairi Matericulation Num : 108013235855

Basic Java Homework

Embed Size (px)

DESCRIPTION

Java, Programming, Geometry

Citation preview

Page 1: Basic Java Homework

1

HOMEWORK

Modern Programming Concepts in Engineering

CE: PO-04

Name : T. Arriessa Sukhairi

Matericulation Num : 108013235855

Page 2: Basic Java Homework

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.

Page 3: Basic Java Homework

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

Page 4: Basic Java Homework

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.

Page 5: Basic Java Homework

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)

Page 6: Basic Java Homework

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.

Page 7: Basic Java Homework

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.

Page 8: Basic Java Homework

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.

Page 9: Basic Java Homework

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.

Page 10: Basic Java Homework

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.

Page 11: Basic Java Homework

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

Page 12: Basic Java Homework

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

Page 13: Basic Java Homework

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.

Page 14: Basic Java Homework

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.

Page 15: Basic Java Homework

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

Page 16: Basic Java Homework

16

Figure 31. The output of Point3D program