Hi,
I have implemented increment() method. Please find the below updated code.
DaysBetween Class:
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Scanner;
class DateClass {
protected int year;
protected int month;
protected int day;
public static final int MINYEAR = 1583;
// Constructor
public DateClass(int newMonth, int newDay, int newYear)
{
month = newMonth;
day = newDay;
year = newYear;
}
// Observers
public int getYear()
{
return year;
}
public int getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public int lilian()
{
// Returns the Lilian Day Number of this date.
// Precondition: This Date is a valid date after 10/14/1582.
//
// Computes the number of days between 1/1/0 and this date as if no calendar
// reforms took place, then subtracts 578,100 so that October 15, 1582 is day 1.
final int subDays = 578100; // number of calculated days from 1/1/0 to 10/14/1582 November
17, 1858
int numDays;
// Add days in years.
numDays = year * 365;
// Add days in the months.
if (month <= 2)
numDays = numDays + (month - 1) * 31;
else
numDays = numDays + ((month - 1) * 31) - ((4 * (month-1) + 27) / 10);
// Add days in the days.
numDays = numDays + day;
// Take care of leap years.
numDays = numDays + (year / 4) - (year / 100) + (year / 400);
// Handle special case of leap year but not yet leap day.
if (month < 3)
{
if ((year % 4) == 0) numDays = numDays - 1;
if ((year % 100) == 0) numDays = numDays + 1;
if ((year % 400) == 0) numDays = numDays - 1;
}
// Subtract extra days up to 10/14/1582.
numDays = numDays - subDays;
return numDays;
}
Override
public String toString()
// Returns this date as a String.
{
String monthString = new DateFormatSymbols().getMonths()[month-1];
return(monthString + "/" + day + "/" + year);
}
public class mjd
{
public int mjd()
{
final int subDays = 678941;
int numDays;
numDays = year * 365;
if (month <= 2)
numDays = numDays + (month - 1) * 31;
else
numDays = numDays + ((month -1) * 31) - ((4 * (month-1) + 27)/10);
numDays = numDays + day;
numDays = numDays + (year / 4) - (year / 100) + (year / 400);
if (month < 3)
{
if ((year % 4) == 0) numDays = numDays -1;
if ((year % 100) == 0) numDays = numDays + 1;
if ((year % 400) == 0) numDays -= numDays -1;
}
// Days subtracted up to 10/14/1582
numDays = numDays - subDays;
return numDays;
}
}
public class djd
{
public int djd()
{
final int subDays = 693961; // number of calculated days from 1/1/0 to January 1,1900
int numDays;
// Add days in years.
numDays = year * 365;
// Add days in the months.
if (month <= 2)
numDays = numDays + (month - 1) * 31;
else
numDays = numDays + ((month - 1) * 31) - ((4 * (month-1) + 27) / 10);
// Add days in the days.
numDays = numDays + day;
// Take care of leap years.
numDays = numDays + (year / 4) - (year / 100) + (year / 400);
// Handle special case of leap year but not yet leap day.
if (month < 3)
{
if ((year % 4) == 0) numDays = numDays - 1;
if ((year % 100) == 0) numDays = numDays + 1;
if ((year % 400) == 0) numDays = numDays - 1;
}
// Subtract extra days up to 10/14/1582.
numDays = numDays - subDays;
return numDays;
}
}
}
public class DaysBetween
{
public static void main(String[] args)
{
Scanner conIn = new Scanner(System.in);
int day, month, year;
System.out.println("Enter two 'modern' dates: month day year");
System.out.println("For example, January 12, 1954, would be: 1 12 1954");
System.out.println();
System.out.println("Modern dates occur after " + DateClass.MINYEAR + ".");
System.out.println();
System.out.println("Enter the first date:");
month = conIn.nextInt();
day = conIn.nextInt();
year = conIn.nextInt();
DateClass date1 = new DateClass(month, day, year);
System.out.println("Enter the second date:");
month = conIn.nextInt();
day = conIn.nextInt();
year = conIn.nextInt();
DateClass date2 = new DateClass(month, day, year);
if ((date1.getYear() <= DateClass.MINYEAR)
||
(date2.getYear() <= DateClass.MINYEAR))
System.out.println("You entered a 'pre-modern' date.");
else
{
System.out.println("The number of days between");
System.out.print(date1);
System.out.print(" and ");
System.out.print(date2);
System.out.print(" is ");
System.out.println(Math.abs(date1.lilian() - date2.lilian()));
}
}
}
IncDate Class:
class IncDate extends DateClass{
public IncDate(int newMonth, int newDay, int newYear)
{
super(newMonth, newDay, newYear);
}
public void increment()
// Increments this IncDate to represent the next day.
// For example, if this = 6/30/2005, then this becomes 7/1/2005.
{
// Increment algorithm goes here
if (month == 1) {
if(day > 30){
day = 1;
month = 2;
}
else{
day = day + 1;
}
}
else if (month == 2) {
if ((year%4 == 0 && year%100 !=0)|| year%400 == 0)
{
if(day > 28){
day = 1;
month = 3;
}
else{
day = day + 1;
}
}
else{
if(day > 27){
day = 1;
month = 3;
}
else{
day = day + 1;
}
}
}
else if (month == 3) {
if(day > 30){
day = 1;
month = 4;
}
else{
day = day + 1;
}
}
else if (month ==4) {
if(day > 29){
day = 1;
month = 5;
}
else{
day = day + 1;
}
}
else if (month==5) {
if(day > 30){
day = 1;
month = 6;
}
else{
day = day + 1;
}
}
else if (month==6) {
if(day > 29){
day = 1;
month = 7;
}
else{
day = day + 1;
}
}
else if (month==7) {
if(day > 30){
day = 1;
month = 8;
}
else{
day = day + 1;
}
}
else if (month == 8) {
if(day > 30){
day = 1;
month = 9;
}
else{
day = day + 1;
}
}
else if (month==9) {
if(day > 29){
day = 1;
month = 10;
}
else{
day = day + 1;
}
}
else if (month==10) {
if(day > 30){
day = 1;
month = 11;
}
else{
day = day + 1;
}
}
else if (month==11) {
if(day > 29){
day = 1;
month = 12;
}
else{
day = day + 1;
}
}
else if (month==12) {
if(day > 30){
day = 1;
month = 1;
year = year + 1;
}
else{
day = day + 1;
}
}
}
}
Solution
Hi,
I have implemented increment() method. Please find the below updated code.
DaysBetween Class:
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Scanner;
class DateClass {
protected int year;
protected int month;
protected int day;
public static final int MINYEAR = 1583;
// Constructor
public DateClass(int newMonth, int newDay, int newYear)
{
month = newMonth;
day = newDay;
year = newYear;
}
// Observers
public int getYear()
{
return year;
}
public int getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public int lilian()
{
// Returns the Lilian Day Number of this date.
// Precondition: This Date is a valid date after 10/14/1582.
//
// Computes the number of days between 1/1/0 and this date as if no calendar
// reforms took place, then subtracts 578,100 so that October 15, 1582 is day 1.
final int subDays = 578100; // number of calculated days from 1/1/0 to 10/14/1582 November
17, 1858
int numDays;
// Add days in years.
numDays = year * 365;
// Add days in the months.
if (month <= 2)
numDays = numDays + (month - 1) * 31;
else
numDays = numDays + ((month - 1) * 31) - ((4 * (month-1) + 27) / 10);
// Add days in the days.
numDays = numDays + day;
// Take care of leap years.
numDays = numDays + (year / 4) - (year / 100) + (year / 400);
// Handle special case of leap year but not yet leap day.
if (month < 3)
{
if ((year % 4) == 0) numDays = numDays - 1;
if ((year % 100) == 0) numDays = numDays + 1;
if ((year % 400) == 0) numDays = numDays - 1;
}
// Subtract extra days up to 10/14/1582.
numDays = numDays - subDays;
return numDays;
}
Override
public String toString()
// Returns this date as a String.
{
String monthString = new DateFormatSymbols().getMonths()[month-1];
return(monthString + "/" + day + "/" + year);
}
public class mjd
{
public int mjd()
{
final int subDays = 678941;
int numDays;
numDays = year * 365;
if (month <= 2)
numDays = numDays + (month - 1) * 31;
else
numDays = numDays + ((month -1) * 31) - ((4 * (month-1) + 27)/10);
numDays = numDays + day;
numDays = numDays + (year / 4) - (year / 100) + (year / 400);
if (month < 3)
{
if ((year % 4) == 0) numDays = numDays -1;
if ((year % 100) == 0) numDays = numDays + 1;
if ((year % 400) == 0) numDays -= numDays -1;
}
// Days subtracted up to 10/14/1582
numDays = numDays - subDays;
return numDays;
}
}
public class djd
{
public int djd()
{
final int subDays = 693961; // number of calculated days from 1/1/0 to January 1,1900
int numDays;
// Add days in years.
numDays = year * 365;
// Add days in the months.
if (month <= 2)
numDays = numDays + (month - 1) * 31;
else
numDays = numDays + ((month - 1) * 31) - ((4 * (month-1) + 27) / 10);
// Add days in the days.
numDays = numDays + day;
// Take care of leap years.
numDays = numDays + (year / 4) - (year / 100) + (year / 400);
// Handle special case of leap year but not yet leap day.
if (month < 3)
{
if ((year % 4) == 0) numDays = numDays - 1;
if ((year % 100) == 0) numDays = numDays + 1;
if ((year % 400) == 0) numDays = numDays - 1;
}
// Subtract extra days up to 10/14/1582.
numDays = numDays - subDays;
return numDays;
}
}
}
public class DaysBetween
{
public static void main(String[] args)
{
Scanner conIn = new Scanner(System.in);
int day, month, year;
System.out.println("Enter two 'modern' dates: month day year");
System.out.println("For example, January 12, 1954, would be: 1 12 1954");
System.out.println();
System.out.println("Modern dates occur after " + DateClass.MINYEAR + ".");
System.out.println();
System.out.println("Enter the first date:");
month = conIn.nextInt();
day = conIn.nextInt();
year = conIn.nextInt();
DateClass date1 = new DateClass(month, day, year);
System.out.println("Enter the second date:");
month = conIn.nextInt();
day = conIn.nextInt();
year = conIn.nextInt();
DateClass date2 = new DateClass(month, day, year);
if ((date1.getYear() <= DateClass.MINYEAR)
||
(date2.getYear() <= DateClass.MINYEAR))
System.out.println("You entered a 'pre-modern' date.");
else
{
System.out.println("The number of days between");
System.out.print(date1);
System.out.print(" and ");
System.out.print(date2);
System.out.print(" is ");
System.out.println(Math.abs(date1.lilian() - date2.lilian()));
}
}
}
IncDate Class:
class IncDate extends DateClass{
public IncDate(int newMonth, int newDay, int newYear)
{
super(newMonth, newDay, newYear);
}
public void increment()
// Increments this IncDate to represent the next day.
// For example, if this = 6/30/2005, then this becomes 7/1/2005.
{
// Increment algorithm goes here
if (month == 1) {
if(day > 30){
day = 1;
month = 2;
}
else{
day = day + 1;
}
}
else if (month == 2) {
if ((year%4 == 0 && year%100 !=0)|| year%400 == 0)
{
if(day > 28){
day = 1;
month = 3;
}
else{
day = day + 1;
}
}
else{
if(day > 27){
day = 1;
month = 3;
}
else{
day = day + 1;
}
}
}
else if (month == 3) {
if(day > 30){
day = 1;
month = 4;
}
else{
day = day + 1;
}
}
else if (month ==4) {
if(day > 29){
day = 1;
month = 5;
}
else{
day = day + 1;
}
}
else if (month==5) {
if(day > 30){
day = 1;
month = 6;
}
else{
day = day + 1;
}
}
else if (month==6) {
if(day > 29){
day = 1;
month = 7;
}
else{
day = day + 1;
}
}
else if (month==7) {
if(day > 30){
day = 1;
month = 8;
}
else{
day = day + 1;
}
}
else if (month == 8) {
if(day > 30){
day = 1;
month = 9;
}
else{
day = day + 1;
}
}
else if (month==9) {
if(day > 29){
day = 1;
month = 10;
}
else{
day = day + 1;
}
}
else if (month==10) {
if(day > 30){
day = 1;
month = 11;
}
else{
day = day + 1;
}
}
else if (month==11) {
if(day > 29){
day = 1;
month = 12;
}
else{
day = day + 1;
}
}
else if (month==12) {
if(day > 30){
day = 1;
month = 1;
year = year + 1;
}
else{
day = day + 1;
}
}
}
}

Hi,I have implemented increment() method. Please find the below up.pdf

  • 1.
    Hi, I have implementedincrement() method. Please find the below updated code. DaysBetween Class: import java.text.DateFormatSymbols; import java.util.Calendar; import java.util.Scanner; class DateClass { protected int year; protected int month; protected int day; public static final int MINYEAR = 1583; // Constructor public DateClass(int newMonth, int newDay, int newYear) { month = newMonth; day = newDay; year = newYear; } // Observers public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } public int lilian() { // Returns the Lilian Day Number of this date. // Precondition: This Date is a valid date after 10/14/1582.
  • 2.
    // // Computes thenumber of days between 1/1/0 and this date as if no calendar // reforms took place, then subtracts 578,100 so that October 15, 1582 is day 1. final int subDays = 578100; // number of calculated days from 1/1/0 to 10/14/1582 November 17, 1858 int numDays; // Add days in years. numDays = year * 365; // Add days in the months. if (month <= 2) numDays = numDays + (month - 1) * 31; else numDays = numDays + ((month - 1) * 31) - ((4 * (month-1) + 27) / 10); // Add days in the days. numDays = numDays + day; // Take care of leap years. numDays = numDays + (year / 4) - (year / 100) + (year / 400); // Handle special case of leap year but not yet leap day. if (month < 3) { if ((year % 4) == 0) numDays = numDays - 1; if ((year % 100) == 0) numDays = numDays + 1; if ((year % 400) == 0) numDays = numDays - 1; } // Subtract extra days up to 10/14/1582. numDays = numDays - subDays; return numDays; } Override public String toString() // Returns this date as a String. { String monthString = new DateFormatSymbols().getMonths()[month-1]; return(monthString + "/" + day + "/" + year); }
  • 3.
    public class mjd { publicint mjd() { final int subDays = 678941; int numDays; numDays = year * 365; if (month <= 2) numDays = numDays + (month - 1) * 31; else numDays = numDays + ((month -1) * 31) - ((4 * (month-1) + 27)/10); numDays = numDays + day; numDays = numDays + (year / 4) - (year / 100) + (year / 400); if (month < 3) { if ((year % 4) == 0) numDays = numDays -1; if ((year % 100) == 0) numDays = numDays + 1; if ((year % 400) == 0) numDays -= numDays -1; } // Days subtracted up to 10/14/1582 numDays = numDays - subDays; return numDays; } } public class djd { public int djd() { final int subDays = 693961; // number of calculated days from 1/1/0 to January 1,1900 int numDays; // Add days in years. numDays = year * 365; // Add days in the months. if (month <= 2) numDays = numDays + (month - 1) * 31; else
  • 4.
    numDays = numDays+ ((month - 1) * 31) - ((4 * (month-1) + 27) / 10); // Add days in the days. numDays = numDays + day; // Take care of leap years. numDays = numDays + (year / 4) - (year / 100) + (year / 400); // Handle special case of leap year but not yet leap day. if (month < 3) { if ((year % 4) == 0) numDays = numDays - 1; if ((year % 100) == 0) numDays = numDays + 1; if ((year % 400) == 0) numDays = numDays - 1; } // Subtract extra days up to 10/14/1582. numDays = numDays - subDays; return numDays; } } } public class DaysBetween { public static void main(String[] args) { Scanner conIn = new Scanner(System.in); int day, month, year; System.out.println("Enter two 'modern' dates: month day year"); System.out.println("For example, January 12, 1954, would be: 1 12 1954"); System.out.println(); System.out.println("Modern dates occur after " + DateClass.MINYEAR + "."); System.out.println(); System.out.println("Enter the first date:"); month = conIn.nextInt(); day = conIn.nextInt(); year = conIn.nextInt(); DateClass date1 = new DateClass(month, day, year);
  • 5.
    System.out.println("Enter the seconddate:"); month = conIn.nextInt(); day = conIn.nextInt(); year = conIn.nextInt(); DateClass date2 = new DateClass(month, day, year); if ((date1.getYear() <= DateClass.MINYEAR) || (date2.getYear() <= DateClass.MINYEAR)) System.out.println("You entered a 'pre-modern' date."); else { System.out.println("The number of days between"); System.out.print(date1); System.out.print(" and "); System.out.print(date2); System.out.print(" is "); System.out.println(Math.abs(date1.lilian() - date2.lilian())); } } } IncDate Class: class IncDate extends DateClass{ public IncDate(int newMonth, int newDay, int newYear) { super(newMonth, newDay, newYear); } public void increment() // Increments this IncDate to represent the next day. // For example, if this = 6/30/2005, then this becomes 7/1/2005. { // Increment algorithm goes here if (month == 1) { if(day > 30){ day = 1;
  • 6.
    month = 2; } else{ day= day + 1; } } else if (month == 2) { if ((year%4 == 0 && year%100 !=0)|| year%400 == 0) { if(day > 28){ day = 1; month = 3; } else{ day = day + 1; } } else{ if(day > 27){ day = 1; month = 3; } else{ day = day + 1; } } } else if (month == 3) { if(day > 30){ day = 1; month = 4; } else{ day = day + 1; }
  • 7.
    } else if (month==4) { if(day > 29){ day = 1; month = 5; } else{ day = day + 1; } } else if (month==5) { if(day > 30){ day = 1; month = 6; } else{ day = day + 1; } } else if (month==6) { if(day > 29){ day = 1; month = 7; } else{ day = day + 1; } } else if (month==7) { if(day > 30){ day = 1; month = 8; } else{ day = day + 1; }
  • 8.
    } else if (month== 8) { if(day > 30){ day = 1; month = 9; } else{ day = day + 1; } } else if (month==9) { if(day > 29){ day = 1; month = 10; } else{ day = day + 1; } } else if (month==10) { if(day > 30){ day = 1; month = 11; } else{ day = day + 1; } } else if (month==11) { if(day > 29){ day = 1; month = 12; } else{ day = day + 1; }
  • 9.
    } else if (month==12){ if(day > 30){ day = 1; month = 1; year = year + 1; } else{ day = day + 1; } } } } Solution Hi, I have implemented increment() method. Please find the below updated code. DaysBetween Class: import java.text.DateFormatSymbols; import java.util.Calendar; import java.util.Scanner; class DateClass { protected int year; protected int month; protected int day; public static final int MINYEAR = 1583; // Constructor public DateClass(int newMonth, int newDay, int newYear) { month = newMonth; day = newDay; year = newYear; }
  • 10.
    // Observers public intgetYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } public int lilian() { // Returns the Lilian Day Number of this date. // Precondition: This Date is a valid date after 10/14/1582. // // Computes the number of days between 1/1/0 and this date as if no calendar // reforms took place, then subtracts 578,100 so that October 15, 1582 is day 1. final int subDays = 578100; // number of calculated days from 1/1/0 to 10/14/1582 November 17, 1858 int numDays; // Add days in years. numDays = year * 365; // Add days in the months. if (month <= 2) numDays = numDays + (month - 1) * 31; else numDays = numDays + ((month - 1) * 31) - ((4 * (month-1) + 27) / 10); // Add days in the days. numDays = numDays + day; // Take care of leap years. numDays = numDays + (year / 4) - (year / 100) + (year / 400); // Handle special case of leap year but not yet leap day.
  • 11.
    if (month <3) { if ((year % 4) == 0) numDays = numDays - 1; if ((year % 100) == 0) numDays = numDays + 1; if ((year % 400) == 0) numDays = numDays - 1; } // Subtract extra days up to 10/14/1582. numDays = numDays - subDays; return numDays; } Override public String toString() // Returns this date as a String. { String monthString = new DateFormatSymbols().getMonths()[month-1]; return(monthString + "/" + day + "/" + year); } public class mjd { public int mjd() { final int subDays = 678941; int numDays; numDays = year * 365; if (month <= 2) numDays = numDays + (month - 1) * 31; else numDays = numDays + ((month -1) * 31) - ((4 * (month-1) + 27)/10); numDays = numDays + day; numDays = numDays + (year / 4) - (year / 100) + (year / 400); if (month < 3) { if ((year % 4) == 0) numDays = numDays -1; if ((year % 100) == 0) numDays = numDays + 1; if ((year % 400) == 0) numDays -= numDays -1; }
  • 12.
    // Days subtractedup to 10/14/1582 numDays = numDays - subDays; return numDays; } } public class djd { public int djd() { final int subDays = 693961; // number of calculated days from 1/1/0 to January 1,1900 int numDays; // Add days in years. numDays = year * 365; // Add days in the months. if (month <= 2) numDays = numDays + (month - 1) * 31; else numDays = numDays + ((month - 1) * 31) - ((4 * (month-1) + 27) / 10); // Add days in the days. numDays = numDays + day; // Take care of leap years. numDays = numDays + (year / 4) - (year / 100) + (year / 400); // Handle special case of leap year but not yet leap day. if (month < 3) { if ((year % 4) == 0) numDays = numDays - 1; if ((year % 100) == 0) numDays = numDays + 1; if ((year % 400) == 0) numDays = numDays - 1; } // Subtract extra days up to 10/14/1582. numDays = numDays - subDays; return numDays; } } } public class DaysBetween
  • 13.
    { public static voidmain(String[] args) { Scanner conIn = new Scanner(System.in); int day, month, year; System.out.println("Enter two 'modern' dates: month day year"); System.out.println("For example, January 12, 1954, would be: 1 12 1954"); System.out.println(); System.out.println("Modern dates occur after " + DateClass.MINYEAR + "."); System.out.println(); System.out.println("Enter the first date:"); month = conIn.nextInt(); day = conIn.nextInt(); year = conIn.nextInt(); DateClass date1 = new DateClass(month, day, year); System.out.println("Enter the second date:"); month = conIn.nextInt(); day = conIn.nextInt(); year = conIn.nextInt(); DateClass date2 = new DateClass(month, day, year); if ((date1.getYear() <= DateClass.MINYEAR) || (date2.getYear() <= DateClass.MINYEAR)) System.out.println("You entered a 'pre-modern' date."); else { System.out.println("The number of days between"); System.out.print(date1); System.out.print(" and "); System.out.print(date2); System.out.print(" is "); System.out.println(Math.abs(date1.lilian() - date2.lilian()));
  • 14.
    } } } IncDate Class: class IncDateextends DateClass{ public IncDate(int newMonth, int newDay, int newYear) { super(newMonth, newDay, newYear); } public void increment() // Increments this IncDate to represent the next day. // For example, if this = 6/30/2005, then this becomes 7/1/2005. { // Increment algorithm goes here if (month == 1) { if(day > 30){ day = 1; month = 2; } else{ day = day + 1; } } else if (month == 2) { if ((year%4 == 0 && year%100 !=0)|| year%400 == 0) { if(day > 28){ day = 1; month = 3; } else{ day = day + 1; } } else{
  • 15.
    if(day > 27){ day= 1; month = 3; } else{ day = day + 1; } } } else if (month == 3) { if(day > 30){ day = 1; month = 4; } else{ day = day + 1; } } else if (month ==4) { if(day > 29){ day = 1; month = 5; } else{ day = day + 1; } } else if (month==5) { if(day > 30){ day = 1; month = 6; } else{ day = day + 1; } }
  • 16.
    else if (month==6){ if(day > 29){ day = 1; month = 7; } else{ day = day + 1; } } else if (month==7) { if(day > 30){ day = 1; month = 8; } else{ day = day + 1; } } else if (month == 8) { if(day > 30){ day = 1; month = 9; } else{ day = day + 1; } } else if (month==9) { if(day > 29){ day = 1; month = 10; } else{ day = day + 1; } }
  • 17.
    else if (month==10){ if(day > 30){ day = 1; month = 11; } else{ day = day + 1; } } else if (month==11) { if(day > 29){ day = 1; month = 12; } else{ day = day + 1; } } else if (month==12) { if(day > 30){ day = 1; month = 1; year = year + 1; } else{ day = day + 1; } } } }