import java.lang.*; import java.text.NumberFormat; /** Lab 3, Assignment 3 Simple program that finds the speed of a cable car between support columns (app supports an arbitrary number of columns at arbitrary postions) */ public class ass3{ public static void main(String [] args){ final int BOUNDARY_DISTANCE = 9; //distance between "near" and "far" final int SAMPLING_DISTANCE = 3; //how often to calc the current speed/position int supports[] = {0, 150, 300}; //position of the supports relative to the first int[] boundary_supports = new int[supports.length*2]; //init array with boundary positions int current_pos = 0; //current position of the car int closest_support=0; //the current closest support int closest_support_distance=100000000; double current_velocity=0; boolean inside_boundary=true; //is car inside the boundary String inside_txt; NumberFormat r = NumberFormat.getInstance(); r.setMaximumFractionDigits(2); //populate the boundary positions array for(int i=0;i<supports.length;i++){ boundary_supports[i*2]=supports[i]-BOUNDARY_DISTANCE; //place lower boundary position based on support position boundary_supports[i*2+1]=supports[i]+BOUNDARY_DISTANCE; //place upper boundary position based on support position } /* //test code System.out.println("supports located at"); for(int i=0; i<supports.length;i++){ System.out.println(supports[i]+" meters"); } System.out.println(""); System.out.println("boundary locations at"); for(int i=0; i<boundary_supports.length;i++){ System.out.println(boundary_supports[i]+" meters"); } */ //move cable car, check if it's inside or outside the boundary position, check which support is closest, print speed and position int total_distance = supports[supports.length-1]; for(current_pos=0; current_pos<=total_distance; current_pos=current_pos+SAMPLING_DISTANCE){//increment distance, from start to finish for(int i=1; i<boundary_supports.length; i++){ //perform a check at each boundary position in the array if(current_pos<boundary_supports[i] && current_pos>boundary_supports[i-1]){ //if position is between the current and previous boundary position, then check if it's inside or outside the boundary if(i%2==0){ //if it is greater than the boundary position, check to see if it's at an even position inside_boundary=false; //if it's at an even position, then it's outside of the boundary } else{ inside_boundary=true; //if it's at an odd position, then it's inside the boundary } } } //find the closest support for(int j=0;j<supports.length;j++){ if(Math.abs(current_pos-supports[j])<closest_support_distance){ closest_support_distance=Math.abs(current_pos-supports[j]); closest_support=j+1; } } ///* //Generate speed values for the positions if(inside_boundary==true){ //if car is inside the boundary, calc speed as follows current_velocity=0.7396+0.0057*closest_support_distance*closest_support_distance; } else{ //if it's not inside, then it's outside the boundary, and calc therefore like follows current_velocity=0.1906+0.12*closest_support_distance-0.00081967*closest_support_distance*closest_support_distance; } System.out.println("Currently at "+current_pos+" meters, with a speed of "+r.format(current_velocity)+" m/s"); //*/ ///* //test code System.out.println("Additionally, the closest support is "+closest_support+", at "+closest_support_distance+" meters."); System.out.println(""); //*/ /* //test code if(inside_boundary==true) inside_txt="is"; else inside_txt="is not"; System.out.println("Currently at "+current_pos+" meters, which "+inside_txt+" inside the boundary"); */ closest_support_distance=100000000; //reset closest support distance for next loop iteration } } }