Need help with date output program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erekose666
    New Member
    • Oct 2006
    • 23

    Need help with date output program

    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.getda y(), DateClass.getye ar());
    }
    public void setDateClass(in t 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(NameNa me DateClass2)
    {
    this(DateClass2 .getname(),Date Class2.getday() ,DateClass2.get year());
    }
    public void setDateClass2(S tring 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.Strin gTokenizer;
    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.prin tln( s1.getmonth()+"/" +s1.getday()+"/" +s1.getyear() );
    }


    }

    Anyone know where I am screwing the pooch here?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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:

    Code:
    public class Test {
        public static void main(String[] args) {
            Date today = new Date("December", 14, 2006);
            System.out.println(today.toString());
        }
    }

    Comment

    • erekose666
      New Member
      • Oct 2006
      • 23

      #3
      So get rid of the entire Date.java after class NameName?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        I think so - after all, your problem specification says "Create class Date" not "Create two classes Date and whatever"

        Comment

        • erekose666
          New Member
          • Oct 2006
          • 23

          #5
          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,in t)
          this(string,int ,int)
          this(int,int)

          I have no idea how to get there from here though.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            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:

            Code:
            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.equalsIg noreCase(otherS tring)).

            Comment

            • erekose666
              New Member
              • Oct 2006
              • 23

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

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                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.

                Comment

                Working...