CS 100: Section Assignment S7

Solutions


Problem 1.

// Looking for blue moons.

import java.io.*;

public class S7_1
{	
    public static final int y1=1600;
	public static final int y2=2100;
	
	public static void main(String args[])
	{
           TokenReader in = new TokenReader(System.in);
	   
	   int k=1;
	   for(int y=y1;y<=y2;y++)
	   {
	      if (Date.isBlueMoonYear(y))
	      {
	         if(k%15==0)
	            System.out.println(" " + y);
	         else
	            System.out.print(" " + y);
	         k=k+1;
	      }
	   }
	   in.waitUntilEnter();
	}

}



// Yields true if there are 13 new moons during year y
   public static boolean isBlueMoonYear(int y)
   {
      Date D1 = new Date("January",1,y);
      Date D2 = new Date("January",1,y+1);
      int n1 = (int) Math.floor(D1.sinceBaseNewMoon()/T);
      int n2 = (int) Math.floor(D2.sinceBaseNewMoon()/T);
      return (n2==n1+13);
   }


/* Output:
 1601 1604 1606 1609 1612 1614 1617 1620 1623 1625 1628 1631 1633 1636 1639
 1642 1644 1647 1650 1652 1655 1658 1661 1663 1666 1669 1671 1674 1677 1680
 1682 1685 1688 1690 1693 1696 1699 1701 1704 1707 1709 1712 1715 1718 1720
 1723 1726 1728 1731 1734 1737 1739 1742 1745 1747 1750 1753 1756 1758 1761
 1764 1766 1769 1772 1775 1777 1780 1783 1785 1788 1791 1794 1796 1799 1802
 1805 1807 1810 1813 1815 1818 1821 1824 1826 1829 1832 1834 1837 1840 1843
 1845 1848 1851 1853 1856 1859 1862 1864 1867 1870 1872 1875 1878 1880 1883
 1886 1889 1891 1894 1897 1900 1902 1905 1908 1910 1913 1916 1919 1921 1924
 1927 1929 1932 1935 1938 1940 1943 1946 1948 1951 1954 1957 1959 1962 1965
 1967 1970 1973 1976 1978 1981 1984 1986 1989 1992 1995 1997 2000 2003 2005
 2008 2011 2014 2016 2019 2022 2024 2027 2030 2033 2035 2038 2041 2043 2046
 2049 2052 2054 2057 2060 2062 2065 2068 2071 2073 2076 2079 2081 2084 2087
 2090 2092 2095 2098 2100
*/

   

Problem 2.

   
   // Some Roman Numeral multiplications

import java.io.*;

public class S7_2
{	
	public static void main(String args[])
	{
           TokenReader in = new TokenReader(System.in);
	   
	   String a,b,p;
	   for (int i=3;i<=6;i++)
	   {
	      for (int j=7;j<=9;j++)
	      {
	         a = RN.valueOf(i);
	         b = RN.valueOf(j);
	         p = RN.prod(a,b);
	         System.out.println(a + " times " + b + " = " + p);
	      }
	   }
	   in.waitUntilEnter();
	}

}

// Yields the Roman numeral associated with the product of the Roman
	// numerals encoded in r1 and r2.
	public static String prod(String r1,String r2)
	{
	   int prod = valueOf(r1)*valueOf(r2);
	   return valueOf(prod);
	}
	
	

/* Output

III times VII = XXI
III times VIII = XXIV
III times IX = XXVII
IV times VII = XXVIII
IV times VIII = XXXII
IV times IX = XXXVI
V times VII = XXXV
V times VIII = XL
V times IX = XLV
VI times VII = XLII
VI times VIII = XLVIII
VI times IX = LIV

*/
   

Problem 3.

   
   // Reverse of a string

import java.io.*;

public class S7_3
{	
	public static void main(String args[])
	{
       TokenReader in = new TokenReader(System.in);
	   
	   String s1,s2;
	   s1 = "zyxwvutsrqponmlkjihgfedcba";
	   s2 = "";
	   for (int k=0;k< s1.length();k++)
	   {
	      s2 = s1.substring(k,k+1) + s2;
	   }
	   System.out.println(s2);
	   in.waitUntilEnter();
	}

}


Problem 4a

public void paint(Graphics g)
	{
	   int red,green,blue;
	   Color C;
	   double f1,f2;
	   for(int k=0;k<=w;k++)
	   {
	      // Draw the kth vertical appropriately colored.
	      f2 = (double)k/w;  // The fraction of the "right" color in the final mix.
	      f1 = 1-f2;         // The fraction of the "left"  color in the final mix.
	      red   = (int) (f1*r1 + f2*r2);
	      green = (int) (f1*g1 + f2*g2);
	      blue  = (int) (f1*b1 + f2*b2);
	      // Create the mixed color and use it to draw the kth vertical
	      if (k%2==0)
	         C = new Color(red,green,blue);
	      else
	         C = new Color(255-red,255-green,255-blue);
	      g.setColor(C);
	      g.drawLine(hL+k,vB,hL+k,vT);
	   }
	
	}
	
	

Problem 4b


	public void paint(Graphics g)
	{
	   int red,green;
	   Color C;
	   for(int i=0;i&lt16;i++)
	   {
	      // Draw the ith row of the grid
	      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;
	         if (i<=j)
	            C = new Color(red,green,blue);
	         else
	            C = new Color(255-red,255-green,255-blue);
	         g.setColor(C);
	         g.fillRect(hL+j*s,vT+i*s,s,s);
	         
	         // Draw a black perimeter around the tile.
	         g.setColor(Color.black);
	         g.drawRect(hL+j*s,vT+i*s,s,s);
	      }
	   }  
	}