-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerimeterAssignmentRunner.java
More file actions
173 lines (155 loc) · 5.17 KB
/
PerimeterAssignmentRunner.java
File metadata and controls
173 lines (155 loc) · 5.17 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import edu.duke.*;
import java.io.File;
public class PerimeterAssignmentRunner {
public double getPerimeter (Shape s) {
// Start with totalPerim = 0
double totalPerim = 0.0;
// Start wth prevPt = the last point
Point prevPt = s.getLastPoint();
// For each point currPt in the shape,
for (Point currPt : s.getPoints()) {
// Find distance from prevPt point to currPt
double currDist = prevPt.distance(currPt);
// Update totalPerim by currDist
totalPerim = totalPerim + currDist;
// Update prevPt to be currPt
prevPt = currPt;
}
// totalPerim is the answer
return totalPerim;
}
public int getNumPoints (Shape s) {
int count=0;
for(Point currPt: s.getPoints())
{
count++;
}
// Put code here
return count;
}
public double getAverageLength(Shape s) {
// Put code here
double avg,count=0,totalPerim=0.0;
Point prev = s.getLastPoint();
for(Point curr: s.getPoints())
{
double currdist = prev.distance(curr);
totalPerim = totalPerim + currdist;
prev = curr;
count++;
}
avg=(totalPerim/count);
return avg;
}
public double getLargestSide(Shape s) {
// Put code here
double max=0;
Point prev = s.getLastPoint();
for(Point curr: s.getPoints())
{
double currdist = prev.distance(curr);
if(max<currdist)
{
max=currdist;
}
prev=curr;
}
return max;
}
public double getLargestX(Shape s) {
// Put code here
double larX=0.0,max=0.0,x;
Point prev = s.getLastPoint();
for(Point curr: s.getPoints())
{
x=curr.getX();
if(max<x)
{
max=x;
}
prev=curr;
}
return max;
}
public double getLargestPerimeterMultipleFiles() {
double largestperimeter=0.0;
DirectoryResource dr = new DirectoryResource();
FileResource largestfile=null;
for (File f : dr.selectedFiles())
{
FileResource fr = new FileResource(f);
Shape myshape= new Shape(fr);
double perim = getPerimeter(myshape);
if(perim > largestperimeter)
{
largestperimeter=perim;
}
}
return largestperimeter;
}
public File getFileWithLargestPerimeter() {
// Put code here
File fileName = null;
double largestPerim = 0;
DirectoryResource dr = new DirectoryResource();
for (File f : dr.selectedFiles()){
FileResource fr = new FileResource(f);
Shape s = new Shape(fr);
double currPerim = getPerimeter(s);
if (currPerim > largestPerim){
largestPerim = currPerim;
fileName = f;
}
}
return fileName;
}
public void testPerimeter () {
FileResource fr = new FileResource();
Shape s = new Shape(fr);
double length = getPerimeter(s);
//int numofpoints = getNumPoints(s);
System.out.println("Perimeter = " + length);
//System.out.println("No. of points = "+numofpoints);
double avgofSide = getAverageLength(s);
System.out.println("Average length = "+avgofSide);
double largestside = getLargestSide(s);
System.out.println("Largest Side ="+largestside);
//double largeX = getLargestX(s);
//System.out.println("Largest X = "+largeX);
//testPerimeterMultipleFiles();
}
public void testPerimeterMultipleFiles() {
double largest = getLargestPerimeterMultipleFiles();
System.out.println("Largest perimeter is : "+largest);
}
public void testFileWithLargestPerimeter() {
File fileName = getFileWithLargestPerimeter();
System.out.println("Name of file with largest perimeter is ="+fileName);
}
// This method creates a triangle that you can use to test your other methods
public void triangle(){
Shape triangle = new Shape();
triangle.addPoint(new Point(0,0));
triangle.addPoint(new Point(6,0));
triangle.addPoint(new Point(3,6));
for (Point p : triangle.getPoints()){
System.out.println(p);
}
double peri = getPerimeter(triangle);
System.out.println("perimeter = "+peri);
}
// This method prints names of all files in a chosen folder that you can use to test your other methods
public void printFileNames() {
DirectoryResource dr = new DirectoryResource();
for (File f : dr.selectedFiles()) {
System.out.println(f);
}
}
public static void main (String[] args) {
PerimeterAssignmentRunner pr = new PerimeterAssignmentRunner();
pr.testPerimeter();
//pr.printFileNames();
//pr.testFileWithLargestPerimeter();
//pr.testPerimeterMultipleFiles();
}
}