This repository was archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangle.java
More file actions
94 lines (90 loc) · 1.64 KB
/
Rectangle.java
File metadata and controls
94 lines (90 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Week 6 Homework
* @author tdap2572
*
*/
public class Rectangle {
//instance fields
private double width;
private double height;
//constructors
/**
* Constructs a default rectangle (width=2, height=1).
*/
public Rectangle(){
width=2;
height=1;
}
/**
* Constructs a user defined rectangle.
*/
public Rectangle(double w, double h){
width=w;
height=h;
}
//methods
/**
* Changes the width
* @param w (width)
*/
public void changeWidth(double w){
if(w>0) width=w;
}
/**
* Changes the height
* @param h (height)
*/
public void changeHeight(double h){
if(h>0) height=h;
}
/**
* Gets the area of the rectangle
* @return the area of the rectangle
*/
public double getArea(){
double area = width*height;
return area;
}
/**
* Gets the perimeter of the rectangle
* @return the perimeter of the rectangle
*/
public double getPerimeter(){
double perimeter = 2*(width+height);
return perimeter;
}
/**
* Gets the X coordinate of the centre
* @return the X coordinate
*/
public double getX(){
double x = width/2;
return x;
}
/**
* Gets the Y coordinate of the centre
* @return the Y coordinate
*/
public double getY(){
double y = height/2;
return y;
}
/**
* Gets the (X,Y) coordinates of the centre
* @return the (X,Y) coordinates
*/
public String getCentre(){
double x = width/2;
double y = height/2;
String centre = String.format("(%.2f, %.2f)",x,y);
return centre;
}
/**
* Gets the diagonal length of the rectangle
* @return the diagonal
*/
public double getDiagonal(){
double r = Math.sqrt((Math.pow(width, 2))+(Math.pow(height, 2)));
return r;
}
}