package class6_2_LinearObserver_start; import java.util.Random; /** * Author: Josiah Yoder et al. * Class: SExxxx-xx * Date: 1/7/14 7:45 AM * Lesson: Week x, Day x */ public class Example { public static void main(String[] ignored) { Example ex = new Example(); ex.run(); } public void run() { Random gen = new Random(); double positionMeters = 0; double oldPosition = 0; double smoothedPosition = 0; for(int i= 0; i<10; i++) { positionMeters += gen.nextDouble(); // Compute position System.out.println("Now we are at: "+positionMeters+" m"); // Compute velocity double velocity = positionMeters - oldPosition; oldPosition = positionMeters; System.out.println("Velocity: "+velocity+" m/s"); // Compute average position smoothedPosition = 0.9 * smoothedPosition + 0.1 * positionMeters; System.out.println("Smoothed position: "+smoothedPosition); // Compute "named" location String namedLocation; if(Math.abs(positionMeters) < 1) { namedLocation = "Home"; } else if(Math.abs(positionMeters-10)<1) { namedLocation = "Work"; } else { namedLocation = ""; } if(namedLocation.length() > 0) { System.out.println("We are at "+namedLocation); } // Separator between steps... System.out.println(); } } }