Hey all =] , I am having a few problems ending this coding homework and i would love if you could help me on how to solve this problem, well my problem is that i try to print the toStirng in my constructor class and for the months it prints the number rather than the name for example i input month, day, year and i input 04 , 30, 1992. I would like it to print April 4, 1992 But it prints 04, 30, 1992 so how do i make it print the month
Run Test Results:
what year is it?
1992
what month number is it?
04
what day of the month is it?
30
The date you have enterd is : 4, 30, 1992
and my code is
MAIN
import java.util.*;
public class DateTest {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println ("what year is it?");
int year = reader.nextInt();
System.out.println ("what month number is it?");
int month = reader.nextInt();
System.out.println ("what day of the month is it?");
int day = reader.nextInt();
Date date = new Date (month, day, year);
System.out.println(date.toString());
}
}
Constructor class
public class Date {
private int day;
private int month;
private int year;
public Date(int monthIn, int dayIn, int yearIn ){
day = dayIn;
month = monthIn;
year = yearIn;
//end of constructor
}
//getters
public int getDay(){
return day;
}
public int getMonth(){
return month;
}
public int getYear(){
return year;
}
//setters
public void setYear(int year){//setting the year used Integer.MAX and MIN_VALUE because the year can go up to infinity and past in infinity
this.year = year;
if (year >= Integer.MIN_VALUE && year <= Integer.MAX_VALUE)//used min and max in integer
{
return;
}
}
public void setMonth(int month){
this.month = month;
if (month >= 1 && month <= 12){//month between 1 and 12
switch (month)//switch case to print out the name of the month istead of number
{
case 1: System.out.println ("January");break;
case 2: System.out.println ("February");break;
case 3: System.out.println ("March");break;
case 4: System.out.println ("April");break;
case 5: System.out.println ("May");break;
case 6: System.out.println ("June");break;
case 7: System.out.println ("July");break;
case 8: System.out.println ("August");break;
case 9: System.out.println ("September");break;
case 10: System.out.println ("October");break;
case 11: System.out.println ("November");break;
case 12: System.out.println ("December");break;
}
return;
}
else
{
System.out.println("This is an invalid month " + this.month);
}
}
public void setDay(int day){
this.day = day;
if (day >= 1 && day <= 31){//day for month between 1 and 31
return;
}
else
{
System.out.println("This is an invalid day " + this.day);
}
}
@Override
public String toString(){//to string summerizing all the inputs on output.
return ("The date you have enterd is : " + this.month +", " + this.day+", " + this.year);
}
}