SHARE
TWEET

SimpleDateClient

TBSpeed Oct 12th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* A client program to display SimpleDate object values
  2.    Anderson, Franceschi
  3.    Belloli, Thomas
  4. */
  5.  
  6. import java.awt.*;
  7. import javax.swing.JFrame;
  8.  
  9. public class SimpleDateClient extends JFrame
  10. {
  11.   private String action = "";
  12.  
  13.   private int animationPause = 2; // 2 seconds between animations
  14.  
  15.   private SimpleDate dateObj; // declare SimpleDate object reference
  16.  
  17.   public void workWithDates( )
  18.   {
  19.     animate( "dateObj reference declared" );
  20.  
  21.     /***** Add your code here *****/
  22.     /**** 1. Instantiate dateObj using an empty argument list  */
  23.  
  24.     dateObj = new SimpleDate();
  25.  
  26.     animate( "Instantiated dateObj - empty argument list" );
  27.  
  28.     /***** 2. Set the month to the month you were born */
  29.  
  30.     dateObj.setMonth(8);
  31.  
  32.     animate( "Set month to birth month" );
  33.  
  34.  
  35.     /***** 3. Set the day to the day of the month you were born */
  36.  
  37.     dateObj.setDay(21);
  38.  
  39.     animate( "Set day to birth day" );
  40.  
  41.  
  42.     /***** 4. Set the year to the year you were born */
  43.  
  44.     dateObj.setYear(1999);
  45.  
  46.     animate( "Set year to birth year" );
  47.  
  48.  
  49.     /***** 5. Call the nextDay method */
  50.  
  51.     dateObj.nextDay();
  52.  
  53.     animate( "Set the date to the next day" );
  54.  
  55.  
  56.     /***** 6. Set the day to 32, an illegal value */
  57.  
  58.     dateObj.setDay(32);
  59.  
  60.     animate( "Set day to 32" );
  61.  
  62.  
  63.     /***** 7. Set the month to 13, an illegal value */
  64.  
  65.     dateObj.setMonth(13);
  66.  
  67.     animate( "Set month to 13" );
  68.  
  69.  
  70.     /***** 8. Assign the value null to dateObj */
  71.  
  72.     dateObj = null;
  73.  
  74.     animate( "Set object reference to null" );
  75.  
  76.  
  77.     /***** 9. Attempt to set the month to 1 */
  78.  
  79.     dateObj.setMonth(1);
  80.  
  81.   }
  82.  
  83.   public SimpleDateClient( )
  84.   {
  85.     super( "A SimpleDate Object" );
  86.  
  87.     setSize( 300, 300 );
  88.     setVisible( true );
  89.   }
  90.  
  91.   public void paint( Graphics g )
  92.   {
  93.     super.paint( g );
  94.  
  95.     // display action
  96.     g.drawString( action, 50, 250 );
  97.  
  98.     // object reference
  99.     int sX = 50, sY = 75;
  100.     int boxL = 75, boxH = 20;
  101.     g.drawRect( sX, sY, boxL, boxH );
  102.     g.drawString( "dateObj", sX, sY - 10 );
  103.  
  104.     if ( dateObj != null )
  105.        draw( g );
  106.     else
  107.       g.drawString( "null", sX + 15, sY + 15 );
  108.   }
  109.  
  110.   public void draw( Graphics g )
  111.   {
  112.     int sX = 50, sY = 75;
  113.     int boxL = 75, boxH = 20;
  114.  
  115.     // arrow
  116.     g.drawLine( sX + boxL, sY + boxH / 2,
  117.                 sX + boxL + 25, sY + boxH / 2 );
  118.     g.drawLine( sX + boxL + 25, sY + boxH / 2,
  119.                 sX + boxL + 25, sY + boxH * 2 );
  120.     g.drawLine( sX + boxL + 25 - 5, sY + boxH * 2 - 5,
  121.                 sX + boxL + 25, sY + boxH * 2 );
  122.     g.drawLine( sX + boxL + 25 + 5, sY + boxH * 2 - 5,
  123.                 sX + boxL + 25, sY + boxH * 2 );
  124.  
  125.  
  126.     // month
  127.     g.drawString( "month", sX + boxL - 50, sY + 2 * boxH + 15 );
  128.     g.drawRect( sX + boxL, sY + 2 * boxH, boxL, boxH );
  129.     g.drawString( Integer.toString( dateObj.getMonth( ) ),
  130.                    sX + boxL + 5, sY + 2 * boxH + 15 );
  131.  
  132.     // day
  133.     g.drawString( "day", sX + boxL - 50, sY + 3 * boxH + 15 );
  134.     g.drawRect( sX + boxL, sY + 3 * boxH, boxL, boxH );
  135.     g.drawString( Integer.toString( dateObj.getDay( ) ),
  136.                   sX + boxL + 5, sY + 3 * boxH + 15 );
  137.  
  138.     // year
  139.     g.drawString( "year", sX + boxL - 50, sY + 4 * boxH + 15 );
  140.     g.drawRect( sX + boxL, sY + 4 * boxH, boxL, boxH );
  141.     g.drawString( Integer.toString( dateObj.getYear( ) ),
  142.                   sX + boxL + 5, sY + 4 * boxH + 15 );
  143.   }
  144.  
  145.   public void animate( String a )
  146.   {
  147.     action = a;
  148.     repaint( );
  149.     Pause.wait( (double) animationPause );
  150.   }
  151.  
  152.   public static void main( String [] args )
  153.   {
  154.     SimpleDateClient app = new SimpleDateClient( );
  155.     app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  156.     app.workWithDates( );
  157.   }
  158. }
RAW Paste Data
Top