Advertisement
letsdoitjava

Date Array/My Date

Jul 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. // The dateArray class prints the values of the dateArr array backwards.
  2. // Renee Waggoner
  3. // July 22, 2019
  4. // Special Requirements: None
  5.  
  6. package date_array;
  7.  
  8. public class DateArray {
  9.     public static void main(String[] args) {
  10.  
  11.         MyDate dateArr[] = new MyDate[4];
  12.  
  13.         MyDate date1 = new MyDate("May", 16, 1984);
  14.         MyDate date2 = new MyDate("November", 14, 1978);
  15.         MyDate date3 = new MyDate("September", 21, 1980);
  16.         MyDate date4 = new MyDate("July", 3, 1987);
  17.  
  18.         // Note: don't forget arrays always start at zero
  19.         dateArr[0] = date1;
  20.         dateArr[1] = date2;
  21.         dateArr[2] = date3;
  22.         dateArr[3] = date4;
  23.  
  24.         // Note: remember to use for loops for arrays
  25.         // initialization, condition, counter, print
  26.         for(int i = dateArr.length - 1; i >= 0; i--)
  27.         {
  28.             // String toString() method - returns a String containing your 3 instance variables.
  29.             System.out.println(dateArr[i].toString());
  30.         }
  31.     }
  32. }
  33.  
  34. =======================================================================================================================================
  35. // MyDate classes printed using a toString() method
  36. // Renee Waggoner
  37. // July 22, 2019
  38. // Special Requirements: None
  39.  
  40. package date_array;
  41.  
  42. // The MyDate class is public, which means that it can be used in other classes.
  43. public class MyDate {
  44.     // Instance variables are declared at the beginning of the class definition,
  45.     // outside of any method. By itself, this code fragment is a legal class definition:
  46.  
  47.     // the instance variables are private, which means they can only be accessed from inside the MyDate class
  48.     private int day , year;
  49.     private String month;
  50.  
  51.     // public means it can be accessed by toString method
  52.     // Constructor: MyDate(String month, int day, int year) - the code stores the values in instance variables
  53.     public MyDate(String month, int day, int year) {
  54.        
  55.         // constructor with parameter
  56.         this.month = month;
  57.         this.day = day;
  58.         this.year = year;
  59.     }
  60.     public String toString() {
  61.         return month + " " + day + ", " + year;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement