Line.java |
| /* James Little wednesday 7th March 2001 Lines and points */ package jlittle_ex1; import basicIO.*; import java.text.*; public class Line { // datafields, type long for big calculations. private long [] xPoint; private long [] yPoint; private long [] hypotenuse; // s is the number of segments in the line. private int s; // line constructor accepts segments, line number and x, y arrays public Line(int segs, int a, long [] x, long [] y) { // segs is the number of segments in this line.. can be anything s=segs; // allocate array to array xPoint = x; yPoint = y; // might as well print the line number here :) Out.println("Line "+a); } // method think works out the distance of all segments and lines... also prints them public void think() { // local variables double distance=0; // length of one segment double totalHyp=0; // takes value of shortest distance double totalDist=0; // adds segment lengths DecimalFormat df = new DecimalFormat("0.0"); // for deciaml formatting // this loop calculates the segment length and prints it out // also calculates the total distance of the segments for(int i=0; i<s; i++){ distance=(Math.sqrt((xPoint[i+1]-xPoint[i])*(xPoint[i+1]-xPoint[i])+(yPoint[i+1]-yPoint[i])*(yPoint[i+1]-yPoint[i]))); Out.println("\tSegment "+(i+1)+" distance "+df.format(distance)+" units"); totalDist+=distance; } // prints the total of the line Out.println("\tTotal distance is: " + df.format(totalDist) + " units"); // long x and y accept the value of any difference between point 1 and point infinity, and square it long x=(xPoint[xPoint.length-1]-xPoint[0])*(xPoint[xPoint.length-1]-xPoint[0]); long y=(yPoint[yPoint.length-1]-yPoint[0])*(yPoint[yPoint.length-1]-yPoint[0]); // total hyp accepts the hypotenuse totalHyp=Math.sqrt(x+y); // if the total hypotenuse is only one unit, then i will print a singular version. if(totalHyp==1.0){ Out.println("\tStart-End shortest distance is: " + df.format(totalHyp) + " unit"); }else Out.println("\tStart-End shortest distance is: " + df.format(totalHyp) + " units"); } } |
James Little |