Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Controller for date class

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Controller for date class

    Does anyone know how to write a controller class? I've been asked to write one for my date class to make sure that it works ok:

    public class Date


    // private variables
    {

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

    // constructors

    public Date ()
    {
    day = 0;
    month = 0;
    year = 0;
    }

    public Date (int dd,int mm,int yyyy)
    {
    day = dd;
    month = mm;
    year = yyyy;
    }

    public Date (Date other)
    {
    day = other.day;
    month = other.month;
    year = other.year;
    }

    // accessors

    public int getDay()
    {
    return day;
    }

    public int getMonth()
    {
    return month;
    }

    public int getYear()
    {
    return year;
    }

    public String monthAsString()
    {
    switch (month)
    {
    case 1 : return "January";
    case 2 : return "February";
    case 3 : return "March";
    case 4 : return "April";
    case 5 : return "May";
    case 6 : return "June";
    case 7 : return "July";
    case 8 : return "August";
    case 9 : return "September";
    case 10 : return "October";
    case 11 : return "November";
    case 12: return "December";
    default : return "error - this month does not exist";
    }
    }

    public boolean equals(Date other)
    {
    if (year == other.year && month == other.month && day == other.day)
    return true;
    else return false;
    }

    public boolean earlierThan(Date other)
    {
    if (year < other.year)
    return true;
    else if (year == other.year)

    {
    if (month < other.month)
    return true;
    else if (month == other.month)

    {
    if (day < other.day)
    return true;
    else return false;
    }
    else return false;
    }
    else return false;
    }

    public String toString()
    {
    return day + " " + monthAsString() + " " + year;
    }

    public void copy (Date other)
    {
    day = other.day;
    month = other.month;
    year = other.year;
    }
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Controller for date class

    Quote Originally Posted by Ecosse View Post
    Does anyone know how to write a controller class? I've been asked to write one for my date class to make sure that it works ok:...
    To test the Date class, make another class in another file. Maybe you want to call your "controller" class TestDate.

    If you are compiling from a command line, then create a file named TestDate.java in the same directory as your Date.java.

    If you are using some kind of Integrated Development Environment, create the new file and add it to your project in the "normal" way.

    Anyhow, TestDate.java can start out looking something like this:
    public class TestDate {
        public static void main(String [] args) {
            // A constructor for 2 November, 2012
            Date date1 = new Date(2, 11, 2012);
            // Now do stuff with the date1 object
            System.out.println("The first date is in the month of " + date1.monthAsString());
            // Do more stuff with date1 (show month, day, year)
     
            Date date2 = new Date( /* some other day, month, year */);
            // Now do stuff with date1 and date2
            // Maybe use the Date method that checks to
            // see whether they are equal.
            // Maybe use the Date method that checks to
            // see whether date1 is earlier than date2
            //
            // Etc.
            //
            // Write code that verifies correct functionality
            // of each and every one of the Date methods
            // and constructors.
        }
    }


    Cheers!

    Z

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Controller for date class

    Thank you. You're a lifesaver!

    Also, how would I code it so that it asks me for the day, then the month and then the year and the output is the full date??

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Controller for date class

    Quote Originally Posted by Ecosse View Post
    ... how would I code it so that it asks me...
    There are a few examples in the Java Documentation for the Scanner Class

    The drill can go something like this:

    • Create a Scanner object with System.in as the parameter.

    • Prompt the user, maybe with with System.out.print("Prompt for whatever: ");

    • Use the Scanner nextInt() method for as many integer variable values you want to read.


    After reading day, month, year from the user (using three nextInt() calls), use these variables as the arguments to create a Date object. Then use that object's getDay() getMonth(), getYear() methods to retrieve the integer values and print them out.

    Or some such thing.


    Cheers!

    Z

Similar Threads

  1. Building your own Date class.
    By ShaunB in forum Java SE API Tutorials
    Replies: 5
    Last Post: October 25th, 2011, 05:37 PM
  2. Building your own Date class.
    By ShaunB in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 21st, 2011, 06:11 PM
  3. Date Of Birth Revision , Class please help
    By youssef22 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 6th, 2010, 02:41 AM
  4. I'm having trouble with date and calendar class
    By kiph in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 02:56 AM
  5. Replies: 3
    Last Post: May 8th, 2009, 01:27 PM