After my tweaking, this is my entire updated code:
public class MyDate {
public int month, day, year;
public MyDate(int month , int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
int daysInMonth() {
/**
* Gives the number of days in a year
**/
switch (month){
case 1:day = 31;break;
case 2:if (isLeapYear())
day = 29;
else
day = 28;break;
case 3:day = 31;break;
case 4:day = 30;break;
case 5:day = 31;break;
case 6:day = 30;break;
case 7:day = 31;break;
case 8:day = 31;break;
case 9:day = 30;break;
case 10:day = 31;break;
case 11:day = 30;break;
case 12:day = 31;break;
default:
if ((month < 1)||(month > 12));
}
return day;
}
public String advanceByOneDay(){
/**
* advances date by one day
**/
String result= " ";
if(day+1> daysInMonth())
{
if(month+1 > 12)
{
month = 1;
day = 1;
year++;
}
else
{
month++;
day = 1;
}
}
else
{
day++;
}
return result;
}
private boolean isLeapYear(){
/**
* Decides if year is leap year.
**/
boolean leap = false;
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
leap = true;
}
else
leap = false;
}
}
return leap;
}
public static String getMonthInString(int month, int day, int year) {
/**
* changes input numerical month to written month
**/
String result = " ";
switch(month) {
case 1: result = "January"; break;
case 2: result = "February"; break;
case 3: result = "March"; break;
case 4: result = "April"; break;
case 5: result = "May"; break;
case 6: result = "June"; break;
case 7: result = "July"; break;
case 8: result = "August"; break;
case 9: result = "September"; break;
case 10: result = "October"; break;
case 11: result = "November"; break;
case 12: result = "December"; break;
default: result = "Error"; //break;
}
return result;
}
/*
public String toString1(){
return getMonthInString(day, day, day);
}
*/
public int getMonth() {return month;}
public int getDay() {return day;}
public int getYear() {return year;}
public void setMonth(int month){this.month = month;}
public void SetDay(int day){this.day = day;}
public void setSalary(int year) {this.year = year;}
}
import java.util.Scanner;
public class MyDateTester {
public static int month, day, year;
public static void main(String[] args) {
MyDate e = new MyDate(month, day, year);
Scanner input = new Scanner(System.in);
System.out.println("This program was written by Robert Read");
System.out.println("This program will allow you to input month, day, and year in digits, advance it by one day and print both dates in proper format.");
System.out.println("Enter the month by it numerical value (1 for January, 2 for February, etc): ");
int month= input.nextInt();
System.out.println("Enter the Day (between 1-31 depending on the month): ");
int day= input.nextInt();
System.out.println("Enter the four digit year; ");
int year= input.nextInt();
System.out.print(e.getMonthInString(month, day, year) + " " + e.day + ", " + e.year);
System.out.print(e.daysInMonth());
//System.out.println(e.advanceByOneDay(month, day, year) + " " + MyDate.getMonthInString(month, day, year) + " " + e.getDay() + ", " + e.getYear());
//advanceByOneDay does not respond by itself
//isLeapYear not working?
}
}