Java Files for P4

 

Date.java

public class Date
{
   // An instance of this class is a particular date.
   
   private String month;    
   private int day;   
   private int year;
   
   // "Moon" constants.
   
   // T is the average time (in days) between new Moons.
   public static final double T = 29.530588;  // 29 days + 12 hours + 44 minutes + 2.8 seconds
 
   // tBase is the time (in days) since the last new Moon at the (GMT) start of January 1, 1600.
   public static final double tBase    = 14.532069;
   
   
   // Constructor. Creates a Date object with month s, day d, and year y.
   public Date(String s, int d, int y)
   {
      month = s;
      day   = d;
      year  = y;
   }
   
     
   // Yields the relative index of this date. 
   // (Days are indexed from 1 starting with January 1.)
   public int relDayIndex()
   {
       int s;
       if (month.equals("January"))
          s = day;
       else if (month.equals("February"))
          s = day+31;
       else if (month.equals("March"))
          s = day+59;
       else if (month.equals("April"))
          s = day+90;
       else if (month.equals("May"))
          s = day+120;
       else if (month.equals("June"))
          s = day+151;
       else if (month.equals("July"))
          s = day+181;
       else if (month.equals("August"))
          s = day+212;
       else if (month.equals("September"))
          s = day+243;
       else if (month.equals("October"))
          s = day+273;
       else if (month.equals("November"))
          s = day+304;
       else
          s = day+334;
       return s;         
   }
   
   // Yields a string that specifies this date.
   public String prettyDate()
      {return month + " " + day + ", " + year;} 
       
}


P4C.java




import java.awt.*;
public class MoonPhase extends Frame
{
    
    // Graphics g draws a moon with t-value t0. 
    // The center is at (hc,vc) and the radius is r.
    public static void drawMoon(Graphics g, int hc, int vc, int r, double t0)
    {
      
      
    }
 
	public void paint(Graphics g)
	{
	  // Test cases for debugging drawMoon:
	  
	  // drawMoon(g,300,300,100,10);
	  // drawMoon(g,600,300,100,20);
	  
	  // Test cases for debugging drawWeekOfMoons
	  
	  // drawWeekOfMoons(g,100,200,30,0.0);
	  // drawWeekOfMoons(g,100,300,30,16.0);
	  
	  // Test case for debugging drawMonthOfMoons
	  
	  // drawMonthOfMoons(g,100,100,30,0.0);
	  
	         
	  
	}
	
}

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

P4A.java


// Checks the methods leapYear, prettyDate, and relDayIndex.
import java.io.*;
public class P4A
{	
	public static void main(String args[])
	{
       TokenReader in = new TokenReader(System.in);
       
       
       // Print the leap years from 1892 to 2012.
	    
	   System.out.println("Leap years during 1892-2012:\n");
	   int count=0;
	   for (int yr=1892;yr<=2012;yr++)
	   {
	      if (Date.leapYear(yr))
	      {
	         System.out.print("   " + yr);
	         count++;
	         if (count%10==0)
	            System.out.println("");
	      }
	   }
	   
	   // Display the relative day indices for the first of each month
	   // during year y.
	   
	   int y = 1996;
	   
	   Date D;
	   System.out.println("\nSome relative day indices:\n");
	   D = new Date("January",1,y);
	   Format.println(System.out,"     %3d = " + D.prettyDate(),D.relDayIndex());
	   
	   D = new Date("February",1,y);
	   Format.println(System.out,"     %3d = " + D.prettyDate(),D.relDayIndex());
	   
	   D = new Date("March",1,y);
	   Format.println(System.out,"     %3d = " + D.prettyDate(),D.relDayIndex());
	   
	   D = new Date("April",1,y);
	   Format.println(System.out,"     %3d = " + D.prettyDate(),D.relDayIndex());
	   
	   D = new Date("May",1,y);
	   Format.println(System.out,"     %3d = " + D.prettyDate(),D.relDayIndex());
	    
	   D = new Date("June",1,y);
	   Format.println(System.out,"     %3d = " + D.prettyDate(),D.relDayIndex());
	   
	   D = new Date("July",1,y);
	   Format.println(System.out,"     %3d = " + D.prettyDate(),D.relDayIndex());
	   
	   D = new Date("August",1,y);
	   Format.println(System.out,"     %3d = " + D.prettyDate(),D.relDayIndex());
	   
	   D = new Date("September",1,y);
	   Format.println(System.out,"     %3d = " + D.prettyDate(),D.relDayIndex());
	   
	   D = new Date("October",1,y);
	   Format.println(System.out,"     %3d = " + D.prettyDate(),D.relDayIndex());
	   
	   D = new Date("November",1,y);
	   Format.println(System.out,"     %3d = " + D.prettyDate(),D.relDayIndex());
	   
	   D = new Date("December",1,y);
	   Format.println(System.out,"     %3d = " + D.prettyDate(),D.relDayIndex());
	   
	   
	   in.waitUntilEnter();   
     }
}


P4B.java

// Checks the methods absDayIndex and elapsed.
import java.io.*;
public class P4B
{	
	public static void main(String args[])
	{
       TokenReader in = new TokenReader(System.in);
	   
	   System.out.println("\nSome Absolute Day Indices:\n");
	   Date D1 = new Date("July",4,1776);
	   Date D2 = new Date("August",27,1998);
	   Date D3 = new Date("March",11,1999);
	   D3 = new Date("January",9,1997);
	   
	   Format.println(System.out,"%8d = " + D1.prettyDate(),D1.absDayIndex());
	   Format.println(System.out,"%8d = " + D2.prettyDate(),D2.absDayIndex());
	   Format.println(System.out,"%8d = " + D3.prettyDate(),D3.absDayIndex());
	   
	   System.out.println("\nThe associated values of sinceBaseNewMoon:\n");
	   double t1 = D1.sinceBaseNewMoon();
	   Format.println(System.out,"%14.4f = " + D1.prettyDate(),t1);
	   double t2 = D2.sinceBaseNewMoon();
	   Format.println(System.out,"%14.4f = " + D2.prettyDate(),t2);
	   double t3 = D3.sinceBaseNewMoon();
	   Format.println(System.out,"%14.4f = " + D3.prettyDate(),t3);
	   
	   System.out.println("\nWaxing or Waning?:\n");
	   if (Date.isWaxing(t1))
	      { System.out.println("The Moon is waxing on " + D1.prettyDate()); }
	   else
	      { System.out.println("The Moon is waning on " + D1.prettyDate()); }
	      
	   if (Date.isWaxing(t2))
	      { System.out.println("The Moon is waxing on " + D2.prettyDate()); }
	   else
	      { System.out.println("The Moon is waning on " + D2.prettyDate()); }
	   
	   if (Date.isWaxing(t3))
	      { System.out.println("The Moon is waxing on " + D3.prettyDate()); }
	   else
	      { System.out.println("The Moon is waning on " + D3.prettyDate()); }
	      
       in.waitUntilEnter();
     }
}