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);	
	}
}

Recommended Answers

All 4 Replies

You have written a wrong function

public String toString(){//to string summerizing all the inputs on output.
return ("The date you have enterd is : " + this.month +", " + this.day+", " + this.year);
}

1) as this.month function prints the integer value
2) you must use setMonth(month) function to print the month....
3) Also i think you have to modify your function of setMonth(month) because its not efficient, because when you are using switch then you can use default statement, there is no need to use if statement here.

You use a switch to convert month number to name, which is a pretty clunky way to do it. Better to have an array of 12 Strings ("Jan","Feb" etc) and use the month number as an index to access the correct name.

Wow thank you guys i will use both your methods and see withone works the best =]. Thank you again for the help

happy Coding

try this code

class apples
{
public static void main(String[] args)
{
orange obj=new orange(4,7,1990);
}
}

class orange
{
private int day;
private int mounth;
private int year;

public orange(int d, int m, int y)
{
day=d;
mounth=m;
year=y;
System.out.printf("object has been initialized with value %s",this);
}
public String toString()
{
return String.format("%d/%d/%d",day, mounth, year);
}

}

it will help you...happy coding.:)

commented: Spoon Feeding not Allowed Here. -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.