CS 100: Programming Assignment P2

Solution


 

// P2A: Examine the Stirling's Approximation of n!	

import java.io.*;

public class P2A
{
	
	public static void main(String args[])
	{
		int	n; 
		long nfact;      // n factorial        
		double s;        // Stirling's approximation to n!.
		double absErr;   // The absolute error in s.
		double relErr;   // The relative error in s.
		
		// initialize Text object in to read from standard input.
			TokenReader in = new TokenReader(System.in);
		
		// Read and write some numbers and strings
			System.out.println("  n                  n!       Absolute Error     Relative Error");
			System.out.println("---------------------------------------------------------------");
			nfact = 1;
			for (n=1;n<=20;n=n+1){
			   nfact = n*nfact;       // The new n factorial is n times the old n factorial.
			   s = Math.sqrt(2*Math.PI/n)*Math.pow(n/Math.exp(1),n);
			   absErr = Math.abs(s-nfact);
			   relErr = absErr/nfact;
			   Format.print(System.out,"  %2d",n);
			   Format.print(System.out,"  %20d",nfact);
			   Format.print(System.out,"      %10.3e",absErr);
			   Format.println(System.out,"          %10.3e",relErr);
			}
			
			
		// Wait for user to enter input to ensure console window remains visible
			in.waitUntilEnter();
	}

}

 

 
 // P2B: Diamonds and Squares

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

// Class CUCSDrawing: a simple graphics window.

public class CUCSDrawing extends Frame
{
	
	final int xc = 500;
	final int yc = 300;   // (xc,yc) is the center
	final int r0 = 250;   // The radius of outermost square.
	final int rMin = 5;   // The radius threshold.
	
	
	public void paint(Graphics g)
	{
	    int r;            // The current radius.
	    int k=1;          // Index of the current square/diamond
	    
	    r = r0;
	    while (r>=rMin)
	    {
		   if (k%2==0)
		   {
		      // Draw a diamond.
		      g.drawLine(xc+r,yc,xc,yc-r);
		      g.drawLine(xc,yc-r,xc-r,yc);
		      g.drawLine(xc-r,yc,xc,yc+r);
		      g.drawLine(xc,yc+r,xc+r,yc);
		      r = r/2;  
		   }
		   else 
		   {
		      // Draw a square.
		      g.drawRect(xc-r,yc-r,2*r,2*r);
		   }
		   k=k+1;     
		}
	}
}

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






// P2C: Hubcaps

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

// Class CUCSDrawing: a simple graphics window.

public class CUCSDrawing extends Frame
{
	
	final int xc = 500;
	final int yc = 300;    // (xc,yc) is the center of the hubcap
	final int r1 = 150;    // The radius of the inner circle.
	final int r2 = 250;    // The radius of the outer circle.
	final int n = 60;      // Half the number of spokes.
	final int p = 2;    // The spoke shift factor.
	
	public void paint(Graphics g)
	{
	    double theta = 2*Math.PI/n;  // The "step angle" in radians.
	    int h,v;                     // (h,v) is a point on the outer circle.
	    int hPlus,vPlus;             // (hPlus,vPlus) is a point on the inner circle.
	    int hMinus,vMinus;           // (hMinus,vMinus) is another point on the inner circle.
	    int k;
	    
	    g.drawOval(xc-r2,yc-r2,2*r2,2*r2);   // Draw the outer circle.
	    g.drawOval(xc-r1,yc-r1,2*r1,2*r1);   // Draw the inner circle
	    
		for (k=1;k<=n;k=k+1)
		{
		   // (h,v) = (xc+r1*cos(k*theta),yc-r1*sin(k*theta))
		   h      = (int)(xc + r2*Math.cos(k*theta));
		   v      = (int)(yc + r2*Math.sin(k*theta));
		   
		   // (hPlus,vPlus) = (xc+r1*cos((k+p)*theta),yc-r1*sin((k+p)*theta))
		   hPlus  = (int)(xc + r1*Math.cos((k+p)*theta));
		   vPlus  = (int)(yc + r1*Math.sin((k+p)*theta));
		   
		   // (hMinus,vMinus) = (xc+r1*cos((k-p)*theta),yc-r1*sin((k-p)*theta))
		   hMinus = (int)(xc + r1*Math.cos((k-p)*theta));
		   vMinus = (int)(yc + r1*Math.sin((k-p)*theta));
		   
		   // Connect (h,v) and (hPlus,vPlus)
		   g.drawLine(h,v,hPlus,vPlus);
		   
		   // Connect (h,v) and (hMinus,vMinus)
		   g.drawLine(h,v,hMinus,vMinus);
		   
		}
	}
	
}


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