CS 100: Lecture L13

March 9

| Back to Lecture Index |


 

Roman Numerals Cont'd

import java.io.*;
public class L12
{
   public static void main(String args[])
   {
           TokenReader in = new TokenReader(System.in);
	 	   
	   RN.println("MCMXCIX");
	   RN.println("IIX");
	   RN.println("CCCXC");
	   RN.println("MDI");
	   RN.println("DCDIV");
	   RN.println("CVL");
	   RN.println("MMI");
	   RN.println("MIM");
	   RN.println("XIV");
	   RN.println("XVIV");
	   RN.println("IIII");
	   RN.println("IXC");

	   in.waitUntilEnter();
    }

}

/* Output:

MCMXCIX = 1999
IIX evaluates to 10 and 10 evaluates to X
CCCXC = 390
MDI = 1501
DCDIV evaluates to 904 and 904 evaluates to CMIV
CVL evaluates to 155 and 155 evaluates to CLV
MMI = 2001
MIM evaluates to 2001 and 2001 evaluates to MMI
XIV = 14
XVIV evaluates to 19 and 19 evaluates to XIX
IIII evaluates to 4 and 4 evaluates to IV
IXC evaluates to 89 and 89 evaluates to LXXXIX

*/
// Methods for dealing with Roman numerals
public class RN 
{
      
   // Yields the value of the Roman numeral specified by the first character
   // of r. It is assumed that r is a valid one or two character Roman numeral    
   public static int valueOfShort(String r)
   {
       String First  = r.substring(0,1);
       int s;
       // Determine the first character and assign its value to s without regard 
       // to the second character.
       if (First.equals("I"))
          {s = 1;}
       else if (First.equals("V"))
          {s = 5;}
       else if (First.equals("X"))
          {s = 10;}
       else if (First.equals("L"))
          {s = 50;} 
       else if (First.equals("C"))
          {s = 100;}
       else if (First.equals("D"))
          {s = 500;}
       else
          {s = 1000;}
          
       // Identify those situations where the value is to be negated
       boolean specialI = r.equals("IV") || r.equals("IX");
       boolean specialX = r.equals("XL") || r.equals("XC"); 
       boolean specialC = r.equals("CD") || r.equals("CM");
       
       if (specialI || specialX || specialC )
          {return -s;}
       else
          {return s;}
   }
   
   // Yields the value of r where it is assumed that r is a valid Roman numeral string
   public static int valueOf(String r)
   {
      int sum = 0;
      String s;
      for(int k=0;k<r.length();k++)
      {  
         // Determine the value of the kth character via valueOfShort.
         // Hand over to this method the right length-1 or length-2 substring.
         if(k==r.length()-1)
            // End of string so just hand over the last character.
            s = r.substring(k,k+1);   
         else
            // Handover the length-2 string made up of this and the next character.
            s = r.substring(k,k+2);   
            
         sum = sum + RN.valueOfShort(s);  
      }
      return sum;
   }
   
   
   // Yields the Roman numeral equivalent of nVal. 
   public static String valueOf(int nVal)
   {
      int n = nVal;
      String s = "";
      while (n>0)
       {
          // Diminish n  by the largest of    1000  900  500  400 100  90  50  40  10   9  5   4 or  1
          // and concatenate to s the string     M   CM    D   CD   C  XC   L  XL   X  IX  V  IV or  I
          if (n>=1000) 
             {n=n-1000;s=s+"M";}
          else if (n>=900)
             {n=n-900;s=s+"CM";}
          else if (n>=500)
             {n=n-500;s=s+"D";}
          else if (n>=400)
             {n=n-400;s=s+"CD";}
          else if (n>=100)
             {n=n-100;s=s+"C";}
          else if (n>=90)
             {n=n-90;s=s+"XC";}
          else if (n>=50)
             {n=n-50;s=s+"L";}
          else if (n>=40)
             {n=n-40;s=s+"XL";}
          else if (n>=10)
             {n=n-10;s=s+"X";}
          else if (n==9)
             {n=n-9;s=s+"IX";}
          else if (n>=5)
             {n=n-5;s=s+"V";}
          else if (n==4)
             {n=n-4;s=s+"IV";}
          else
             {n=n-1;s=s+"I";}
       }
       return s;       
    }
    
    // Prints r and its value if it is a legal Roman numeral and an "illegal" message otherwise
    public static void println(String r)
    {
       int n = valueOf(r);
       String s1 = valueOf(n);
       if (s1.equals(r))
       {
          System.out.println(r + " = " + n);
       }
       else
       {
	  System.out.println(r + " evaluates to " + n + " and " + n + " evaluates to " + s1 );
       }
    }
}
      

Drawing a Color Gradient

// Illustrate the building of colors	

import java.awt.*;

public class DisplayColor extends Frame
{
    // Color "gradient" is in the rectangle with up left corner (hL,vT),
    // bottom edge at v = vB, and width w.
    public static final int vT = 100;
    public static final int vB = 500;
    public static final int hL = 100;
    public static final int w  = 600;
    
    // (r1,g1,b1) is the "left edge" color.
    public static final int r1 =   0;
    public static final int g1 =   255;
    public static final int b1 =   255;
    
    // (r2,g2,b2) is the "right edge" color.
    public static final int r2 = 255;
    public static final int g2 =   0;
    public static final int b2 = 255;
    
    
    
    
	public void paint(Graphics g)
	{
	   int red,green,blue;
	   Color C;
	   double f1,f2;
	   for(int k=0;k<=w;k++)
	   {
	      f2 = (double)k/w;
	      f1 = 1-f2;
	      // Mix the left and right colors in proportion f1:f2
	      red   = (int) (f1*r1 + f2*r2);
	      green = (int) (f1*g1 + f2*g2);
	      blue  = (int) (f1*b1 + f2*b2);
	      C = new Color(red,green,blue);
	      g.setColor(C);
	      g.drawLine(hL+k,vB,hL+k,vT);
	   }
	
	}
	
}

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

Drawing a Color Grid

// Illustrate the building of colors	

import java.awt.*;

public class ShowColorGrid extends Frame
{
    // The grid is 16-by-16 with s-by-s tiles. The upper left
    // corner of the upper left tile is at (hL,vT). 
    public static final int vT = 50;
    public static final int hL = 100;
    public static final int s  = 35;
    
    // The rgb value of the upper left tile is (0,0,blue).
    public static final int blue = 255;
    
    // Draws a 16-by-16 color grid where the (r,g,b) value for the
    // tile in row i and column j is given by (16*i,16*j,blue)
	public void paint(Graphics g)
	{
	   int red,green;
	   Color C;
	   for(int i=0;i&lt16;i++)
	   {
	      for(int j=0;j&lt16;j++)
	      {
	         // Display the tile that is in row i and column j of the grid.
	         
	         // Build the right color.
	         red   = 16*i;
	         green = 16*j;
	         C = new Color(red,green,blue);
	         g.setColor(C);
	         g.fillRect(hL+j*s,vT+i*s,s,s);
	         
	         // Draw a black perimeter.
	         g.setColor(Color.black);
	         g.drawRect(hL+j*s,vT+i*s,s,s);
	      }
	   }  
	}	
}

public class L13b
{
	public static void main(String args[])
	{
		ShowColorGrid d = new ShowColorGrid();
		d.resize(900,700);                     
		d.move(0,75);
		d.setTitle("Show Color Grid");                 
		d.show();
		d.toFront();
	}
}