Executable class
package m4;
import javax.swing.*;
public class M4_2E6 {
public static void main(String[] args) {
//get date variables
int m1 = Integer.parseInt(
JOptionPane.showInputDialog("Enter the beginning month: "));
int d1 = Integer.parseInt(
JOptionPane.showInputDialog("Enter the beginning date: "));
int y1 = Integer.parseInt(
JOptionPane.showInputDialog("Enter the beginning year: "));
int n = Integer.parseInt(
JOptionPane.showInputDialog("Enter a number of days: "));
//create date objects
MyDate begDate = new MyDate (m1, d1, y1);
MyDate curDate = new MyDate (m1, d1, y1);
//add
curDate.addDays(n);
//show
System.out.println(curDate.showDate() + " is " + n +
" days after " + begDate.showDate());
System.exit(0);
}
}
MyDate class
package m4;
public class MyDate {
private int month, day, year;
//constructor method
MyDate(int m, int d, int y){
month = m;
day = d;
year = y;
}
int getDay(){
return day;
}
int getMonth(){
return month;
}
int getYear(){
return year;
}
void addDays(int n){
int counter = 0;
while (counter<n){
int modays = 31;
day++;
boolean leap = false;
//test leap year
if (((year%4 == 0 & year%100 != 0))|(year%400 == 0)){
leap = true;
}
//fix date increments
if(month == 4 | month == 6 | month == 9 | month == 11){
modays = 30;
}
if (month == 2 & leap){
modays = 29;
}
if (month == 2 & !leap){
modays = 28;
}
if (day>modays){
day=1;
month++;
}
if(month>12){
month=1;
year++;
}
}
}
String showDate(){
//intialize strings
String m="December";
String Date=null;
//set m according to months
if (month==1){
m = "January";
}
else if (month==2){
m = "February";
}
else if (month==3){
m = "March";
}
else if (month==4){
m = "April";
}
else if (month==5){
m = "May";
}
else if (month==6){
m = "June";
}
else if (month==7){
m = "July";
}
else if (month==8){
m = "August";
}
else if (month==9){
m = "September";
}
else if (month==10){
m = "October";
}
else if (month==11){
m = "November";
}
Date = m + " " + day + ", " + year;
return Date;
}
}
I can't figure out what the problem is, I just wrote a similar program that calculates the number of days between two dates with different classes and it works correctly. I think it's something stupid I'm just not catching...help!

New Topic/Question
Reply



MultiQuote







|