CS 100: Section Assignment S1

Solutions


1.

// S1_1
// Compute various sin and cos values using trigonometric identities.

import java.io.*;

public class S1_1 {
   public static void main( String args[]){
     
      // initialize Text object in to read from standard input.	
      	
	  TokenReader in = new TokenReader(System.in);
  
      double c60,c72,c36,s36,c30,s30,c6,s6,c3,s3,s27;

      c60 = 0.5;                            // cos(60)
      c72 = (Math.sqrt(5.0) - 1.0)/4.0;     // cos(72)
 
      c36 = Math.sqrt((1.0 + c72)/2.0);	    // cos(36)
      s36 = Math.sqrt(1-c36*c36);           // sin(36)
      c30 = Math.sqrt((1.0 + c60)/2.0);	    // cos(30)
      s30 = Math.sqrt(1-c30*c30);           // sin(36)
      c6  = c36*c30 + s36*s30;              // cos(6)
      c3  = Math.sqrt((1.0+c6)/2.0);        // cos(3)
      s3  = Math.sqrt(1-c3*c3);             // sin(3)
      s27 = c3*s30 - c30*s3;                // sin(27)
        
      // Note that Math.cos and Math.sin assume the argument is in radians.
      
      Format.println(System.out,"\ncos(3) = %10.6f  (via trig formula manipulation)",c3 );
      Format.println(System.out,"cos(3) = %10.6f  (via direct call)",Math.cos(Math.PI*3.0/180.0));
     
      Format.println(System.out,"\nsin(27) = %10.6f  (via trig formula manipulation)",s27 );
      Format.println(System.out,"sin(27) = %10.6f  (via direct call)",Math.sin(Math.PI*27.0/180.0));
      
      // Wait for user to enter input to ensure console window remains visible
	  in.waitUntilEnter();
   }
}

2.

// S1_2
// Seconds to hours-minutes-seconds.

import java.io.*;

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

      // initialize Text object in to read from standard input.
      TokenReader in = new TokenReader(System.in);

      int T;                // Time in seconds.
      int h, m, s;          // T = h hours + m minutes + s seconds.
      int WhatsLeft;        // T - 3600*h

      System.out.println("\nEnter time (seconds):"); 
      T = in.readInt();
      h = T / 3600;
      WhatsLeft = T % 3600;
      m = WhatsLeft / 60;
      s = WhatsLeft % 60;
      Format.println(System.out,"\n%9d  seconds =",T);
      Format.println(System.out,"                       %4d hours",h);
      Format.println(System.out,"                       %4d minutes",m);
      Format.println(System.out,"                       %4d seconds",s);
      
      // Wait for user to enter input to ensure console window remains visible
	  in.waitUntilEnter();
   }
}

 

3.

// S1_3
// Taxi Fare Computation.

import java.io.*;

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

      // initialize Text object in to read from standard input.
      TokenReader in = new TokenReader(System.in);

      double distance;      // Distance traveled (assumed in miles)
      double eighths;       // Distance traveled (in eighths of miles)
      double units;
      double fare;
      
      System.out.println("\nEnter distance (miles):"); 
      distance = in.readDouble();
      
      eighths = 8.0*distance;
      units = Math.ceil(eighths);
      fare = 5.0 + (units-1.0)*2.0;
      
      System.out.println("Fare = " + fare + " dollars");
      
      // Wait for user to enter input to ensure console window remains visible
	  in.waitUntilEnter();
   }
}

 

4.

// S1_4
// Draws an ellipse and the "bounding diamond".	


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

// Class CUCSDrawing: a simple graphics window.

public class CUCSDrawing extends Frame
{
	// Method paint is called by the system whenever the drawing window needs
	// to be refreshed, including when it is first created and when it is brought
	// to the front after being hidden by overlapping windows.
	
	// The ellipse and diamond have center (xc,yc) and the ellipse has
	// semiaxes a and b.
	
	final int xc = 250;
	final int yc = 150;
	final int a = 100;
	final int b = 60;     
	
	public void paint(Graphics g)
	{
	    int r;                            // The "radius" of the diamond.
		r = (int) Math.sqrt(a*a + b*b);
		g.drawLine(xc+r,yc,xc,yc-r);      // First to second base.
		g.drawLine(xc,yc-r,xc-r,yc);      // Second to third base.
		g.drawLine(xc-r,yc,xc,yc+r);      // Third base to home.
		g.drawLine(xc,yc+r,xc+r,yc);      // Home to first base.
		g.drawOval(xc-a,yc-b,2*a,2*b);    // The ellipse
	}	
}


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