CS 100: Lecture L4

February 4

| Back to Lecture Index |


 

// Example L4A: Draws a staircase with colored steps.

import java.io.*;
import java.awt.*;

public class CUCSDrawing extends Frame
{
    
	final int h0 = 50;
	final int v0 = 50;     // The leftmost step has upper left corner (h0,v0).
	final int delh = 70;   // The tread length.
	final int delv = 40;   // The riser height.
	final int n    = 10;   // Number of steps.
	
	public void paint(Graphics g)
	{
	    int h;
	    int v;
	    
	    h = h0;
	    v = v0;
	    int k;
	    for (k=1;k<=n;k=k+1)
	    {
	       if (k%2 ==0)
	       {
	          g.setColor(Color.blue);
	       }
	       else
	       {
	          g.setColor(Color.green);
	       }
	       g.fillRect(h,v,delh,delv);
	       h = h+delh;
	       v = v+delv;
	    }
 
    }
	
}

public class L4A
{
	public static void main(String args[])
	{
		CUCSDrawing d = new CUCSDrawing();
		d.resize(1000,800);
		d.move(0,75);
		d.setTitle("L4A");
		d.show();
		d.toFront();
	}
}

// Example L4B: Powers of Two

import java.io.*;

public class L4B {
   
   public static void main( String args[]){
      TokenReader in = new TokenReader(System.in);
      
      final int n = 10;
      int k;     // The exponent of two.
      long x;    // 2-to-the-k
      
      // Print the nth power of two. Solution 1 using a for-loop
      
      x = 1;
      for (k=0;k&ltn;k=k+1)
      {
         x = 2*x;
      }
      System.out.println("2-to-the " + n + "= " + x);
      
      // Print the nth power of two. Solution 2 using a while-loop
      
      x = 1;
      k = 0;
      while (k< n)
      {
         x = 2*x;
         k = k+1;
      }
      System.out.println("2-to-the " + n + "= " + x);
      
      
      // Print the first power of two that is > one billion.
      
      x = 1;
      while (x<=1000000000)
      {
         x = 2*x;
      }
      System.out.println("First power of two bigger than a billion is " + x);
      
      
      // Print the first power of two that is > one billion and report the power.
      
      x = 1;
      k = 0;
      while (x<=1000000000)
      {
         x = 2*x;
         k=k+1;
      }
      System.out.println("2-to-the " + k + "= " + x);
      
      in.waitUntilEnter();
   }
}

 

// Example L4C: Coin Tosses

import java.io.*;

public class L4C {
   
   public static void main( String args[]){

      TokenReader in = new TokenReader(System.in);
      
      
      final int n = 100;   // Number of coin tosses.
      int k;
      double x;            // A random number between 0 and 1.
      int Heads = 0;       // Number of "heads".
      for (k=1;k<=n;k=k+1)
      {
         x = Math.random();
         if (x <= .5)
         {
            Heads = Heads+1;
            System.out.println("H");
         }
         else
         {
            System.out.println("T");
         }
      }
      System.out.println("\n   n = " + n + "   Heads = " + Heads);
      
      in.waitUntilEnter();
   }
}

 

// Example L4D: Checking if a value is in (not in) an interval.

import java.io.*;

public class L4D {
   
   public static void main( String args[]){

      TokenReader in = new TokenReader(System.in);
      
      final int n = 10000;   // Number of trials.
      int k;
      double x;            // A random number between 0 and 1.
      int count1=0;        // Number of times x is in [.3,.6]
      int count2=0;        // Number of times x is not in [.7,.9]
      
      
      for (k=1;k<=n;k=k+1)
      {
         x = Math.random();
         
         if ((.3<=x) && (x<=.6))
         // x is in the interval [.3,.6], i.e., .3 <= x <= .6
         {   
            count1 = count1+1;
         }
         
         if ((x<.7) || (.9&ltx))
         // x is not in the interval [.7,.9], i.e., either x <. 7 or .9 < x
         {
            count2 = count2+1;
         }
      }
      System.out.println("n = " + n + "\n   count1 = " + count1 + "\n   count2 = " + count2);
      
      in.waitUntilEnter();
   }
}