Question: I'm stuck on this java program, I got the program to increment days, but i can't figure out the how to decrement the date the
I'm stuck on this java program, I got the program to increment days, but i can't figure out the how to decrement the date the user inputs. Answers get upvotes.
Problem
Create a Java application named Problem14 using the two files Dr. Hanna provided to youDate.java and Problem14.javathen change his code to add two non-trivial public, non-static methods to the Date class.
1. Increment a Date to the next day (for example, 1-31-2011 increments to 2-1-2011; 12-31-2010 increments to 1-1-2011).
2. Decrement a Date to the previous day (for example, 2-1-1953 decrements to 1-31-1953; 1-1-1954 decrements to 12-31-1953).
. Extra Credit (up to 5 points) Re-write the Date member functions Input() and Output() to use dialog boxes to accomplish input and output of Date objects.
//--------------------------------------------------------- // Date.java //---------------------------------------------------------
package problem14;
import java.util.Scanner;
public class Date { private String name; private int MM; private int DD; private int YYYY; private char separator;
//---------------------------------------------------- public Date(String name,char separator) //---------------------------------------------------- { this.name = name; MM = 12; DD = 1; this.YYYY = 1993; this.separator = separator; System.out.print("Date construction#1 of "); Output(); }
//---------------------------------------------------- public Date(String name,int MM,int DD,int YYYY,char separator) //---------------------------------------------------- { this.name = name; this.MM = MM; this.DD = DD; this.YYYY = YYYY; this.separator = separator; System.out.print("Date construction#2 of "); Output(); }
//---------------------------------------------------- public void finalize() //---------------------------------------------------- { System.out.print("Date destruction of "); Output(); }
//------------------------------------------------- public void Input() //------------------------------------------------- { Scanner IN = new Scanner(System.in);
System.out.print(name + " MM? "); MM = IN.nextInt(); System.out.print(name + " DD? "); DD = IN.nextInt(); System.out.print(name + " YYYY? "); YYYY = IN.nextInt(); }
//---------------------------------------------------- public void Output() //---------------------------------------------------- { System.out.printf("%s = %2d%c%2d%c%4d ",name,MM,separator,DD,separator,YYYY); }
//------------------------------------------------- public void SetMM(int MM) //------------------------------------------------- { this.MM = MM; }
//------------------------------------------------- public void SetDD(int DD) //------------------------------------------------- { this.DD = DD; }
//------------------------------------------------- public void SetYYYY(int YYYY) //------------------------------------------------- { this.YYYY = YYYY; }
//------------------------------------------------- public String GetName() //------------------------------------------------- { return( name ); }
//------------------------------------------------- public int GetMM() //------------------------------------------------- { return( MM ); }
//------------------------------------------------- public int GetDD() //------------------------------------------------- { return( DD ); }
//------------------------------------------------- public int GetYYYY() //------------------------------------------------- { return( YYYY ); }
//------------------------------------------------- public int GetSeparator() //------------------------------------------------- { return( separator ); } //------------------------------------------------- public void incrementDate() //------------------------------------------------- { int[] daysInMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // If leap year than Feb will contain 29 days if (isLeapYear(this.YYYY)) { daysInMonth[2] = 29; } /* * increment days if no of days if more that the days of a month increment a * month , if no of months ig greater that 12 then set it to 1 and increment a * year */ this.DD++; if (this.DD > daysInMonth[this.MM]) { this.DD = 1; this.MM++; if (this.MM > 12) { this.MM = 1; this.YYYY++; } } } //------------------------------------------------- public void decrementDate() //------------------------------------------------- { //missing code }
public boolean isLeapYear(int year) { return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); }
}
//--------------------------------------------------------- // Problem14.java //---------------------------------------------------------
package problem14;
import javax.swing.JOptionPane;
public class Problem14 { public static void main(String[] args) { Date date1 = new Date("date1",'-'); Date date2 = new Date("date2",1,5,1953,'.');
date1.SetMM(1); date1.SetDD(30); date1.SetYYYY(1979); date1.Output(); System.out.println();
date2.Input();
// OMG! An example of an attempted violation of the principle of information hiding!!! // System.out.printf("%s = %2d%c%2d%c%4d ",date2.name,date2.MM,date2.separator, // date2.DD,date2.separator,date2.YYYY); // generates a syntax error that complains about name having private access in the class Date.
// This is the correct way to access private instance variables using accessor methods. System.out.printf("%s = %2d%c%2d%c%4d ",date2.GetName(),date2.GetMM(),date2.GetSeparator(),date2.GetDD(),date2.GetSeparator(),date2.GetYYYY()); date2.incrementDate(); System.out.println("Next Day is "); date2.Output(); date1 = null; date2 = null;
System.out.println("Just before System.gc() call..."); System.gc();
JOptionPane.showMessageDialog(null,"Pause execution...");
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
