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

Need help with date output program

P: 23
I need a java prog to do the following:
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.]


Here's what I got so far but i am at a dead stop now with no clue how to proceed further.

public class Date
{

private int month;
private int day;
private int year;


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



class NameName
{
private String name;
private int day;
private int year;

public NameName()
{
this ("",0,0);
}
public void NameName(String n, int d, int y)
{
setDateClass2(n, d, y);
}
public NameName(NameName DateClass2)
{
this(DateClass2.getname(),DateClass2.getday(),Date Class2.getyear());
}
public void setDateClass2(String n)
{
setname(n);
setday(D);
setyear(Y);
}
public void setname( String n )
{
name=n;
}
public String getname()
{
return name;
}
public void setday(int D)
{
day = ((D >=0 && D <=31)? D : 0);
}
public int getday()
{
return day;
}
public void setyear(int Y)
{
year =((Y >=0 && Y <=2007)? Y : 0);
}
public int getyear()
{
return year;
}}



The second part of the prog is here:


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

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

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



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


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


}

Anyone know where I am screwing the pooch here?
Dec 14 '06 #1
Share this Question
Share on Google+
7 Replies


Ganon11
Expert 2.5K+
P: 3,652
I think your NameName class is unnecessary. It looks like the assignment asks you to have a constructor that will take a string for the month "such as "February" instead of a number (such as 2). You would then compare the given string to a set of constant strings to see which month is represented by the given string. For instance, if the user passed "march" to the constructor, you would compare "march" to "January" (false), February (false), and "March" (true!). Knowing that the string represents March, you would set month equal to 3.

Next, I think the problem asks you to provide methods for output in the class itself - what you have now is output formatting in your main(). You should just be able to use a single function call, or something like this:

Expand|Select|Wrap|Line Numbers
  1. public class Test {
  2.     public static void main(String[] args) {
  3.         Date today = new Date("December", 14, 2006);
  4.         System.out.println(today.toString());
  5.     }
  6. }
Dec 14 '06 #2

P: 23
So get rid of the entire Date.java after class NameName?
Dec 14 '06 #3

Ganon11
Expert 2.5K+
P: 3,652
I think so - after all, your problem specification says "Create class Date" not "Create two classes Date and whatever"
Dec 14 '06 #4

P: 23
OK I sorta agree with you there, but I have no idea how to change the stuff from ints to strings n whatnot.
Basically I know I eventually need 3 this:
this(int,int,int)
this(string,int,int)
this(int,int)

I have no idea how to get there from here though.
Dec 14 '06 #5

Ganon11
Expert 2.5K+
P: 3,652
Well, as I said, you will need to compare the string given as an argument to the constructor and compare it to some constants.

Start by defining a static member of the Date class as follows:

Expand|Select|Wrap|Line Numbers
  1. private static String[] months; // {"January", "February", "March", etc etc "December" }
Then, inside your constructor, loop through this array (which is called through Date.months[index]) and compare the argument string to the values (you will have to use String.equalsIgnoreCase(otherString)).
Dec 14 '06 #6

P: 23
Are you saying make one like this
private static String[] months; // {"January", "February", "March", etc etc "December" }

or one like THIS

private static String[] months ={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
Dec 14 '06 #7

Ganon11
Expert 2.5K+
P: 3,652
Whichever initializes the array with the values, I haven't done Java in a while, so I forgot how to define static variables. Probably your second one is the right one.
Dec 14 '06 #8

Post your reply

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