Advertisement
TBSpeed

SimpleDateClient

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