Skip to content

Instantly share code, notes, and snippets.

@speters33w
Created April 12, 2022 00:03
Show Gist options
  • Save speters33w/0eda46a941ef2dd54dd20dcd0fb844d8 to your computer and use it in GitHub Desktop.
Save speters33w/0eda46a941ef2dd54dd20dcd0fb844d8 to your computer and use it in GitHub Desktop.
Coursera Duke PerimeterAssignmentRunner
import edu.duke.DirectoryResource;
import edu.duke.FileResource;
import edu.duke.Point;
import edu.duke.Shape;
import java.io.File;
/* example input file:
example.txt
2, 2
7, 3
6, 9
3, 5
*/
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 void printPoints (Shape s) {
//Prints the points in the file
System.out.println("The points in your shape are:");
//For each point currPt in the shape,
for (Point currPt : s.getPoints()) {
//print it to the console
System.out.println(currPt);
}
}
public int getNumPoints (Shape s) {
// Start with numPoints = 0
int numPoints = 0;
// For each point currPt in the shape,
for (Point currPt : s.getPoints()) {
//add 1 to numPoints
numPoints++;
}
// and return the integer numPoints to the caller
return numPoints;
}
public double getAverageLength(Shape s) {
//Get the total perimeter (double) from getPerimeter()
double totalPerim = getPerimeter(s);
//Get the number of points (int) from getNumPoints()
int numPoints = getNumPoints(s);
//Do the math and return to caller
return totalPerim / numPoints;
}
public double getLargestSide(Shape s) {
// Start with largestSide = 0
double largestSide = 0.0d;
// 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);
// If currDist is greater largestSide,
if (currDist > largestSide) {
// make currDist largestSide
largestSide = currDist;
}
// Update prevPt to be currPt
prevPt = currPt;
}
// largestSide is the answer
return largestSide;
}
public double getLargestX(Shape s) {
// Start with largestX = 0
double largestX = 0.0d;
// For each point currPt in the shape,
for (Point currPt : s.getPoints()) {
// Find the x value of currPoint
int currX = currPt.getX();
// If currDist is greater largestSide,
if (currX > largestX) {
// make currDist largestSide
largestX = currX;
}
}
// largestSide is the answer
return largestX;
}
public double getLargestPerimeterMultipleFiles() {
//Open a dialog to allow multiple file selection
DirectoryResource dr = new DirectoryResource();
// Start with largestPerimeter = 0
double largestPerimeter = 0.0;
// For each selected File f in dr,
for (File f : dr.selectedFiles()) {
//create a file resource fr
FileResource fr = new FileResource(f);
//Create a Shape from the resource fr,
Shape s = new Shape(fr);
//and get its perimeter
double perimeter = getPerimeter(s);
//print the perimeter for test purposes
System.out.println(perimeter);
//Compare the newest perimeter to see if it is larger,
if (perimeter > largestPerimeter) {
//then change the largest perimeter if it is
largestPerimeter = perimeter;
}
}
return largestPerimeter;
}
public String getFileWithLargestPerimeter() {
String fileName = null;
//Open a dialog to allow multiple file selection
DirectoryResource dr = new DirectoryResource();
// Start with largestPerimeter = 0
double largestPerimeter = 0.0;
// For each selected File f in dr,
for (File f : dr.selectedFiles()) {
//create a file resource fr
FileResource fr = new FileResource(f);
//Create a Shape from the resource fr,
Shape s = new Shape(fr);
//and get its perimeter
double perimeter = getPerimeter(s);
//print the perimeter for test purposes
// System.out.println("Perimeter "+ perimeter + " is in " + f.getName());
//Compare the newest perimeter to see if it is larger,
if (perimeter > largestPerimeter) {
//then change the largest perimeter if it is
largestPerimeter = perimeter;
fileName = f.getName();
}
}
return fileName;
}
public void testPerimeter (FileResource fr) {
//Create a Shape object from the FileResource fr
Shape s = new Shape(fr);
//Print the points for test verification
printPoints(s);
//Get the perimeter and print it
double length = getPerimeter(s);
System.out.println("The perimeter of your shape is " + length + ".");
//Get the number of points in fr and print the total
int numPoints = getNumPoints(s);
System.out.println("Your shape has " + numPoints + " points.");
//Get the average of line lengths of the sides in fr and print the total
double averageLength = getAverageLength(s);
System.out.println("The average length of a side in your shape is " + averageLength + ".");
//Get the largest length of the sides in fr and print the total
double largestSide = getLargestSide(s);
System.out.println("The longest side in your shape is " + largestSide+ ".");
//Get the largest x value of the coordinates in fr and print the total
double largestX = getLargestX(s);
System.out.println("The largest x value in your shape is " + largestX + ".");
}
public void testPerimeterMultipleFiles() {
double largestPerimeter = getLargestPerimeterMultipleFiles();
System.out.println("The largest perimeter in your selected shapes is " + largestPerimeter + ".");
}
public void testFileWithLargestPerimeter() {
System.out.println("The filename containing the shape with the largest perimeter in your selected files is: \n"
+ getFileWithLargestPerimeter() + ".");
}
// 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();
FileResource fr = new FileResource(); //comment out to run part 3
pr.testPerimeter(fr); //comment out to run part 3
pr.testPerimeterMultipleFiles(); //comment out to run parts 1 and 2
pr.testFileWithLargestPerimeter(); //comment out to run parts 1 and 2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment