By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,106 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

Can anyone help me with this program?

P: 23
Need to write a program that does this:
Create class Date with the following capabilities:

a) Output the date in multiple formats, such as:

MM/DD/YYYY
June 14, 2005
DDD YYYY
b) Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day number in the year. [Hint: To convert the string representation of the month to a numeric value, compare strings using the equals method. For example, it s1 and s2 are strings, the method call s1.equals(s2) returns true if the strings are identical and otherwise returns false.]

I'm starting from scratch thus the new thread! :) And if anyone can tell me how to delete the other one or wants to delete it for me, feel free to do so!
Dec 15 '06 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
Need to write a program that does this:
Create class Date with the following capabilities:

a) Output the date in multiple formats, such as:

MM/DD/YYYY
June 14, 2005
DDD YYYY
b) Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day number in the year. [Hint: To convert the string representation of the month to a numeric value, compare strings using the equals method. For example, it s1 and s2 are strings, the method call s1.equals(s2) returns true if the strings are identical and otherwise returns false.]

I'm starting from scratch thus the new thread! :) And if anyone can tell me how to delete the other one or wants to delete it for me, feel free to do so!
Write code for the class and the constructors and post what you think they should look like then we can help from there.
Dec 15 '06 #2

P: 23
This is what I got so far.
Two java files. First one is

public class Date
{

private int month;
private int day;
private int year;
private static String[] months ={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

public Date ()
{
this (0,0,0);
}
public Date (int m)
{
this (m, 0,0);
}
public Date(int m, int d, int y)
{
setDateClass( m ,d ,y );
}
public Date(Date DateClass)
{
this(DateClass.getmonth(), DateClass.getday(), DateClass.getyear());
}
public void setDateClass(int m, int d, int y)
{
setmonth(m);
setday(d);
setyear(y);
}
public void setmonth( int m)
{
month =((m >=0 && m <=12)? m : 0);
}
public void setday( int d)
{
day = ((d >=0 && d <=31)? d : 0);
}
public void setyear(int y)
{
year = ((y >= 0 && y <=2007)? y : 0);
}
public int getmonth()
{
return month;
}
public int getday()
{
return day;
}
public int getyear()
{
return year;
}
}



Second one is


import java.util.StringTokenizer;
import java.io.*;

public class DateClass
{
public static void main( String args[])
{

int month=12;
int day=14;
int year=2006;



Date s1= new Date(month, day, year);


System.out.println( s1.getmonth()+"/" +s1.getday()+"/" +s1.getyear() );
}


}
Dec 17 '06 #3

P: 23
I am absolutely horrible at constructors and I am just absolutely guessing at what Im doing here. The string array I made for the months was on the advice of another member here, but I have no clue why its going in there at all etc etc.
For arguments sake, let's all assume that my total knowledge of Java is how to spell the word! :P
Dec 17 '06 #4

Ganon11
Expert 2.5K+
P: 3,652
Hint: To convert the string representation of the month to a numeric value, compare strings using the equals method. For example, it s1 and s2 are strings, the method call s1.equals(s2) returns true if the strings are identical and otherwise returns false.
This is why I suggested using a static array of strings holding the months. Suppose you have a constructor with the following header:

Expand|Select|Wrap|Line Numbers
  1. public Date(String mon, int day, int year)
But your month is held by an integer, not a String, so you must determine what month the user sent you. You can do this by comparing the String argument mon to each element in the Date.months array. To compare two strings, use s1.equalsIgnoreCase(s2), where s1 and s2 are string variables (s1 would be the argument mon, s2 would be an element (0 through 11) of Date.months). Your hint tells you to use equals, but equalsIgnoreCase will be much better, since "february", "February", and "FEBRUARY" will all be treated as different by .equals(), but identical by .equalsIgnoreCase().

In order to check, declare an integer variable (call it num) that will hold the position of the month indicated by the user's string. Then, using a for loop (with your index going from 0 to 11), compare mon to each string in Date.months (If your index variable was i, you would compare mon to Date.months[i] inside the loop). If the strings are equal, set num equal to the index and break the loop. Outside of the loop, increment num by 1 (Suppose mon is "February". "February" is held at the 2nd position of your array, but that index is 1, so you must increment num so that your output will reflect the correct month).

Now that you have an integer value for your month, you can call another constructor, or you can use your setMonth(int) method.
Dec 17 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.