Abstract Sometimes you need to iterate, but you do not know in advance the exact
ID: 3803244 • Letter: A
Question
Abstract
Sometimes you need to iterate, but you do not know in advance the exact number of iterations. In such cases, you will need to detect when you're supposed to stop.
In this exercise, you'll have a pop-goes-the-weasel function which tells you when to stop. For example, you might need to call the stopping function exactly 3 times. And since this is MATH 2370, you must call it with the argument 2370.
Your Task
In this exercise, you need to write the following functions.
MATLAB Signature Description Constraints crank(pgtw) Evaluates pgtw(2370) until you get a true value. Using recursion.Explanation / Answer
import java.util.ArrayList;
import java.util.List;
public category Cluster purpose centroid;
public int id;
//Creates a brand new Cluster
public Cluster(int id)
public List getPoints() come points;
}
public void addPoint(Point point)
public void setPoints(List points)
public purpose getCentroid() come centroid;
}
public void setCentroid(Point centroid) center of mass;
}
public int getId()
public void clear()
public void plotCluster()
System.out.println("]");
}
}
Point.java
package com.dataonfocus.clustering;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public category purpose personal double x = 0;
non-public double y = 0;
non-public int cluster_number = 0;
public Point(double x, double y)
public void setX(double x)
public double getX() come back this.x;
}
public void setY(double y)
public double getY() come back this.y;
}
public void setCluster(int n)
public int getCluster() come back this.cluster_number;
}
//Calculates the space between 2 points.
protected static double distance(Point p, purpose centroid) come back science.sqrt(Math.pow((centroid.getY() - p.getY()), 2) + Math.pow((centroid.getX() - p.getX()), 2));
}
//Creates random purpose
protected static purpose createRandomPoint(int min, int max) come back new Point(x,y);
}
protected static List createRandomPoints(int min, int max, int number)
come points;
}
public String toString() come "("+x+","+y+")";
}
}
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
package com.dataonfocus.clustering;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public category purpose {
non-public double x = 0;
non-public double y = 0;
non-public int cluster_number = 0;
public Point(double x, double y)
public void setX(double x)
public double getX() come back this.x;
}
public void setY(double y)
public double getY() come back this.y;
}
public void setCluster(int n)
public int getCluster() come back this.cluster_number;
}
//Calculates the space between 2 points.
protected static double distance(Point p, purpose centroid) come back science.sqrt(Math.pow((centroid.getY() - p.getY()), 2) + Math.pow((centroid.getX() - p.getX()), 2));
}
//Creates random purpose
protected static purpose createRandomPoint(int min, int max) come back new Point(x,y);
}
protected static List createRandomPoints(int min, int max, int number)
come points;
}
public String toString() come "("+x+","+y+")";
}
}