letsdoitjava

date_array

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