Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The dateArray class prints the values of the dateArr array backwards.
- // Renee Waggoner
- // July 22, 2019
- // Special Requirements: None
- package date_array;
- public class DateArray {
- public static void main(String[] args) {
- MyDate dateArr[] = new MyDate[4];
- MyDate date1 = new MyDate("May", 16, 1984);
- MyDate date2 = new MyDate("November", 14, 1978);
- MyDate date3 = new MyDate("September", 21, 1980);
- MyDate date4 = new MyDate("July", 3, 1987);
- // Note: don't forget arrays always start at zero
- dateArr[0] = date1;
- dateArr[1] = date2;
- dateArr[2] = date3;
- dateArr[3] = date4;
- // Note: remember to use for loops for arrays
- // initialization, condition, counter, print
- for(int i = dateArr.length - 1; i >= 0; i--)
- {
- // String toString() method - returns a String containing your 3 instance variables.
- System.out.println(dateArr[i].toString());
- }
- }
- }
- =======================================================================================================================================
- // MyDate classes printed using a toString() method
- // Renee Waggoner
- // July 22, 2019
- // Special Requirements: None
- package date_array;
- // The MyDate class is public, which means that it can be used in other classes.
- public class MyDate {
- // Instance variables are declared at the beginning of the class definition,
- // outside of any method. By itself, this code fragment is a legal class definition:
- // the instance variables are private, which means they can only be accessed from inside the MyDate class
- private int day , year;
- private String month;
- // public means it can be accessed by toString method
- // Constructor: MyDate(String month, int day, int year) - the code stores the values in instance variables
- public MyDate(String month, int day, int year) {
- // constructor with parameter
- this.month = month;
- this.day = day;
- this.year = year;
- }
- public String toString() {
- return month + " " + day + ", " + year;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement