import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; /** * */ /** * @author Donald Yessick * */ public class Lab16 { public static void main(String[] args) { // get time in milliseconds since 1970 Jan 1 long timestamp = System.currentTimeMillis(); Date now = new Date(); // initialize to current UTC Date/Time SimpleDateFormat sdf = new SimpleDateFormat( "EEEE yyyy/MM/dd hh:mm:ss aa zz : zzzzzz" ); sdf.setTimeZone( TimeZone.getDefault() ); // local time String dateString = sdf.format( now ); System.out.println("milliseconds since 1970 Jan 1="+timestamp); System.out.println("Today is "+dateString); //compareComputeSum(); long timestamp2 = System.currentTimeMillis(); System.out.println("time elapsed:"+(timestamp2-timestamp)+"ms"); long timestamp3 = System.currentTimeMillis(); System.out.println("give or take a few "+(timestamp3-timestamp2+"ms")); } /* loops remember this? (1 + 2 + .. + n-1 + n) = ((n+1)*n)/2 */ //TODO: compute sum here using a for loop public static int loopComputeSum_1_to(int n) { int sum=0; //compute sum here using a for loop return sum; } //TODO: compute sum here using a direct method public static int computeSum_1_to(int n) { int sum=0; //compute sum here using a direct method return sum; } //TODO: public static int computeLog(int n, int base) { //compute log(n, base) using while loop //log (n, b) examples // n < 1 --> int log (n, b) is undefined // n = 1 --> log (n, b) is 0 // n = b --> log (n, b) is 1 // n = b*b --> log (n, b) is 2 // n = b*b*b --> log (n, b) is 3 // n = b^b --> log (n, b) is b // n = b^n --> log (n, b) is n // n = b^k --> log (n, b) is k // our job is to find the nearest k such that //b^k <= n //b^(k+1) > n int log=0; return log; } //TODO: public static int computePower(int base,int n) { //compute b^n using while loop //examples //b^0 =1 //b^1 = b //b^2 = b*b int pow = 1; return pow; } //TODO: create a string representing n in base public static String convertToBase( int n, int base) { return ""; } //TODO: reverse the String (reverse ("abc").equals( "321" ); public static String reverse( String s ) { return ""; } //TODO: reverse the number (reverse (123) == 321 public static int reverse( int n ) { return 0; } /* nothing to do here */ public static void compareComputeSum() { long timestamp1; long timestamp2; long timestamp3; int sum; int k; timestamp1 = System.currentTimeMillis(); //compute sum here using a for loop sum=0; k = 1; while (sum < Integer.MAX_VALUE / k) { sum = loopComputeSum_1_to(k); k++; } timestamp2 = System.currentTimeMillis(); sum=0; k = 1; //compute sum here using a direct method while (sum < Integer.MAX_VALUE / k) { sum = loopComputeSum_1_to(k); k++; } timestamp3 = System.currentTimeMillis(); k--; System.out.println("Computed "+k+" sums"); System.out.println("time elapsed (looping method):"+(timestamp2-timestamp1)+"ms"); System.out.println("sum 1..k(looping method):"+loopComputeSum_1_to(k)); System.out.println("time elapsed (direct method):"+(timestamp3-timestamp2)+"ms"); System.out.println("sum 1..k(direct method):"+computeSum_1_to(k)); } }