CS 100: Lecture L28

May 6

| Back to Lecture Index |


import java.io.*;
public class L28
{	
	public static void main(String args[])
	{
       TokenReader in = new TokenReader(System.in);
	   
	   // Legal pedal sprocket values:
	   int[] pAvail = {52,48,42,39,32,28};
	   
	   // An initial bike
	   int[] p = {52,48,42};
	   int[] w = {42,28,23,20,17,14,13};
	   Bike bestB = new Bike(p,w);
	   double bestD = bestB.D();
	   int count=1;
	   
	   Bike B;
	   double D; 
	   for(int i0=0;i0< 2;i0++)
	    for(int i1=i0+1;i1< 5;i1++)
	     for(int i2=i1+1;i2< 6;i2++)
	     {
	        // p[0] > p[1]> p[2]
	        p[0]=pAvail[i0]; p[1]=pAvail[i1]; p[2]=pAvail[i2];
	        // Force highest gear ratio to equal 4 and the lowest gear ratio to be 1.
	        w[0]=p[2];            
	        w[6]=p[0]/4;
	        // Now choose w[1],...,w[5] so that w[0] > w[1] > w[2] > w[3] > w[4] > w[5] > w[6]
	        for(w[5]=w[6]+1;w[5]< w[0];w[5]++)
	         for(w[4]=w[5]+1;w[4]< w[0];w[4]++)
	          for(w[3]=w[4]+1;w[3]< w[0];w[3]++)
	           for(w[2]=w[3]+1;w[2]< w[0];w[2]++)
	            for(w[1]=w[2]+1;w[1]< w[0];w[1]++)
	            {
	               count++;
	               B = new Bike(p,w);
	               D = B.D();
	               if(D < bestD)
	               {
	                  bestD = D;
	                  bestB = B;
	               }
	            }
	      } 
	   System.out.println("Number of Bikes checked = " + count);
	   bestB.ShowBike();     
	   in.waitUntilEnter();
	}

}

public class Bike
{	
   private int[] pedalSprockets;  // pedalSprockets[i] = # teeth on the ith pedal sprocket.
   private int[] wheelSprockets;  // wheelSprockets[i] = # teeth on the ith wheel sprocket.
   private double[] g;            // The gear ratios sorted from smallest to largest.
   
   public Bike(int[]p, int[]w)
   {
      int n = p.length;
      pedalSprockets = new int[n];
      for(int i=0;i< n;i++)
         pedalSprockets[i] = p[i];
      int m = w.length;
      wheelSprockets = new int[m];
      for(int i=0;i< m;i++)
         wheelSprockets[i] = w[i];
         
      int nGears = n*m;
      g = new double[nGears];
      int k=0;
      for(int i=0;i< n;i++)
      {
         for(int j=0;j< m;j++)
         {
            g[k] = (double)p[i]/w[j];
            k++;
         }
      }
      sort(g);
    }
    
    // Objective function that measures the "distance" to the idea bike that has equally spaced
    // gear ratios from 1 to 4.
    public double D()
    {
       double s = 0;
       double giPerfect;
       for(int i=0;i< g.length;i++)
       { 
          giPerfect = 1 + .15*i;
          // s = s+Math.abs(g[i] - giPerfect);
          s = Math.max(s,Math.abs(g[i] - giPerfect));
       }
       return s;
    }
    
    // Permute the values in t so sorted from smallest to largest
    public static void sort(double[] t)
    {
       int n=t.length;
       double temp;
	   int pass=1;
	   int swaps;      
	   boolean Sorted = false;
       while (!Sorted) 
       {
          swaps=0;
	      for (int j = 0; j < n - pass; j++) 
	      {
		     if (t[j+1]< t[j]) 
		     {
			    temp   = t[j+1];
				t[j+1] = t[j];
				t[j]   = temp;
				swaps++;
		     }
		  }
		  pass++;
		  Sorted = (pass == n) || swaps==0;
	   }
	}
	
	// Pretty print the bike.
	public void ShowBike()
	{
	   System.out.println();
	   for(int i=0;i< pedalSprockets.length;i++)
	      System.out.print(pedalSprockets[i] + " ");
	   System.out.println();
	   for(int i=0;i< wheelSprockets.length;i++)
	      System.out.print(wheelSprockets[i] + " ");
	   System.out.println("");
	   System.out.println("");
	   for(int i=0;i< g.length;i++)
	      if (i>0 && i%11==0)
	         Format.println(System.out,"%5.2f ",g[i]);
	      else
	         Format.print(System.out,"%5.2f ",g[i]);
	   System.out.println("\n");
	   System.out.println("D = "+this.D());
	}
	   
	     
}