CS 100: Section Assignment S3
Solutions
// S3_1 // T-grid import java.io.*; import java.awt.*; public class CUCSDrawing extends Frame { final int left = 100; // Horizontal cooordinate of the left edge. final int top = 100; // Vertical coordinate of the top edge. final int n = 5; // Each "box" that makes up the grid is n-by-n. final int s = 20; // The grid line spacein (in pixels). public void paint(Graphics g) { int h; // the horizontal coordinate of the current vertical grid line int v; // the vertical coordinate of the current horizontal grid line. int L; // the length of the current grid line. int k; // the index of the current grid line. h= left; v = top; L = n*s; // Note that although there are 3n rows (columns) of boxes, there are 3n+1 // horizontal (vertical) grid lines. for (k=1;k<=3*n+1;k=k+1) { if (n+1<=k && k<=2*n+1) // The middle third has "full" grid lines". { g.drawLine(left,v,left+3*L,v); g.drawLine(h,top,h,top+3*L); } else // The top third or last third has grid lines that are indented and short. { g.drawLine(left+L,v,left+2*L,v); g.drawLine(h,top+L,h,top+2*L); } h=h+s; v=v+s; } } } public class S3_1 { public static void main(String args[]) { CUCSDrawing d = new CUCSDrawing(); d.resize(900,800); d.move(0,75); d.setTitle("P1C: The T-Grid"); d.show(); d.toFront(); } }
// S3_2: Rational approximations to PI import java.io.*; public class S3_2 { public static void main(String args[]) { TokenReader in = new TokenReader(System.in); final int qMax = 20; // Maximum denominator to try. int p,q; // p/q is an approximation to pi double error; // the absolute error in p/q. for(q=1;q<=qMax;q=q+1) { p = (int) Math.round(Math.PI*q); error = Math.abs(Math.PI - (double)p/(double)q); System.out.println(p + " " + q + " " + error); } in.waitUntilEnter(); } } /* Output: 3 1 0.14159265358979312 6 2 0.14159265358979312 9 3 0.14159265358979312 13 4 0.10840734641020688 16 5 0.05840734641020706 19 6 0.025074013076873403 22 7 0.0012644892673496777 25 8 0.016592653589793116 28 9 0.030481542478681956 31 10 0.04159265358979303 35 11 0.04022552822838854 38 12 0.025074013076873403 41 13 0.012253500256360628 44 14 0.0012644892673496777 47 15 0.008259320256459812 50 16 0.016592653589793116 53 17 0.023945594766263678 57 18 0.025074013076873403 60 19 0.016302083252312194 63 20 0.008407346410206795 */
// S3_3 import java.io.*; public class S3_3 { public static void main(String args[]) { TokenReader in = new TokenReader(System.in); final int qMax = 10000; // The largest denominator to check. int p,q; // p/q approximates pi. double error; // Absolute error: | p/q - pi| int pBest,qBest; // pBest/qBest is the closest approximation found double smallestSoFar; // Absolute error: | pBest/qBest - pi| // Initializations pBest = 3; qBest = 1; smallestSoFar = Math.abs(3.0-Math.PI); for(q=2;q<=qMax;q=q+1) { p = (int) Math.round(Math.PI*q); error = Math.abs(Math.PI - (double)p/(double)q); if (error<smallestSoFar) // An improvement has been found. { smallestSoFar = error; pBest = p; qBest = q; } } System.out.println("pBest = " + pBest + " qBest = " + qBest + " error = " + smallestSoFar); in.waitUntilEnter(); } } /* Output: pBest = 355 qBest = 113 error = 2.667641894049666E-7 */
// S3_4 import java.io.*; public class S3_4 { public static void main(String args[]) { TokenReader in = new TokenReader(System.in); final int turnMax = 100; // The maximum number of turns allowed. System.out.println("Enter a positive integer: "); long nStart = in.readInt(); long n = nStart; // The current integer in the sequence. long turn = 0; // The number of turns required to reach the current integer. while (n != 1 && turn<turnMax) { if (n%2==0) // n is even so divide by 2. { n = n/2; } else // n is not even so multiply by three and add one. { n = 3*n+1; } turn = turn+1; } if (n==1) { // One has been reached so print the score. System.out.println("nStart = " + nStart + " turns = " + turn ); } else { System.out.println("nStart = " + nStart + " and it will take more than " + turnMax + " turns to reach one."); } in.waitUntilEnter(); } } /* Sample runs: Enter a positive integer: 17 nStart = 17 turns = 12 Enter a positive integer: 1023 nStart = 1023 turns = 62 Enter a positive integer: 2047 nStart = 2047 and it will take more than 100 turns to reach one. */