MODULE 3 - SHEET 1


public class Fibonacci
 { public static void main(String[] args)
    { int a=1, b=1, c=a+b, sum=a+b+c;
      while (c < 1000)
       { a = b;
         b = c;
         c = a+b;
         sum += c;
       }
      System.out.println("First term over 1000 is " + c);
      System.out.println("The sum to that term is " + sum);
    }
 }

// This yields:
//
// First term over 1000 is 1597
// The sum to that term is 4180



public class Mins
 { public static void main(String[] args)
    { int mins = 125;
      System.out.println("Time is " + mins/60 + " hours "
                                    + mins%60 + " minutes");
    }
 }



public class FloatIntroA
 { public static void main(String[] args)
    { int i=2;
      float x;
      x = 1.32f/1.1f;
      if (x == 1.2f)
         System.out.println("Don't bank on this!");
      else
         System.out.println("What I feared!");
      x = i;
      System.out.println("x = " + x);
      x = i/3;
      System.out.println("x = " + x);
      x = (float)i/(float)3;
      System.out.println("x = " + x);
    }
 }

// This yields:
//
// Don't bank on this!
// x = 2.0
// x = 0.0
// x = 0.6666667



public class FloatIntroB
 { public static void main(String[] args)
    { int i=2;
      float x;
      x = 1.32f/1.1f;
      System.out.println(x == 1.2f ? "Don't bank on this!" : "What I feared!");
      x = i;
      System.out.println("x = " + x);
      x = i/3;
      System.out.println("x = " + x);
      x = (float)i/(float)3;
      System.out.println("x = " + x);
    }
 }

// This yields:
//
// Don't bank on this!
// x = 2.0
// x = 0.0
// x = 0.6666667


MODULE 3 - SHEET 2

public class Greenfly { public static void main(String[] args) { int m=1; int[] im = new int[8]; for (int i=1; i<=7; i++) im[i] = 0; for (int day=1; day<=28; day++) { m += im[7]; for (int i=7; i>1; i--) im[i] = im[i-1]; im[1] = 8*m; } int total = im[1]+im[2]+im[3]+im[4]+im[5]+im[6]+im[7]+m; System.out.println("After 28 days the greenfly total is " + total); } } // This yields: // // After 28 days the greenfly total is 1161889 public class DoubleIntro { public static void main(String[] args) { float f; double x = 2.0d; double y = Math.sqrt(x); System.out.println("y = " + y); f = (float)y; System.out.println("f = " + f); y = (double)f; System.out.println("y = " + y); x = Math.sqrt(144.0d); System.out.println("x = " + x); x = 0d; y = 1d; double z = Math.atan2(y,x)*180d/Math.PI; System.out.println("z = " + z); } } // This yields: // // y = 1.4142135623730951 // f = 1.4142135 // y = 1.4142135381698608 // x = 12.0 // z = 90.0 public class BoolIntro { public static void main(String[] args) { boolean p, q, r; p = true; q = 5<2; r = p && q || !(5<2); if (r) { System.out.println("p = " + p); System.out.println("q = " + q); System.out.println("r = " + r); } } } // This yields: // // p = true // q = false // r = true