Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why won't this program work?

    I have been asked to code a program (class) based on some certain instructions. I feel like I ALMOST got it down, but am doing something stupid. I cannot figure out how to add a hyphen into a symbolic constant so that when I type INSERT_HYPHEN it will insert a "-" into a accessors method. It says incompatible types> Also when I try to insert the local variable "fullDate" into the 'getFullDate' accessor method, and then put "fullDate = year + month + day" it indicates 'incompatible types! Perhaps it is because the accesor method is a string, and I am trying to add 'ints' inside it. I cannot find a way around it. Here is my code.

    public class Date
    {
    public static final int INSERT_ZERO = 0;
    public static final char INSET_HYPHEN = -; //ERROR incompatible types

    // instance variables - replace the example below with your own
    private int year;
    private int month;
    private int day;

    /**
    * Default constructor
    */
    public Date()
    {
    setYear (2013);
    setMonth (01);
    setDay (01);
    }

    /**
    *
    */
    public Date (int whatIsYear, int whatIsMonth, int whatIsDay)
    {
    setYear (whatIsYear);
    setMonth (whatIsMonth);
    setDay (whatIsDay);
    }

    /**
    *@return year
    */
    public int getYear()
    {
    return year;
    }

    /**
    *@return month
    */
    public int getMonth()
    {
    return month;
    }

    /**
    *@return day
    */
    public int getDay()
    {
    return day;
    }

    /**
    *@return
    */
    public String getFullDate()
    {
    String fullDate;
    if (whatIsMonth < 10); // the year, month, and day all give me incompatible types
    {
    fullDate = year + INSERT_HYPHEN + INSERT_ZERO + month + INSERT_HYPHEN + day;
    }
    if (whatIsDay < 10);
    {
    fullDate = year + INSERT_HYPHEN + month + INSERT_HYPHEN + INSERT_ZERO + day;
    }
    else
    {
    fullDate = year + INSERT_HYPHEN + month + INSERT_HYPHEN + day;
    }
    return year + month + day;
    }

    /**
    *
    */
    public void setYear (int whatIsYear)
    {
    if ((whatIsYear >= 1990) && (whatIsYear <= 2013))
    {
    year = whatIsYear;
    }
    else
    {
    year = 2013;
    }
    }

    /**
    *
    */
    public void setMonth (int whatIsMonth)
    {
    if ((whatIsMonth >= 1) && (whatIsMonth <= 12))
    {
    month = whatIsMonth;
    }
    else
    {
    month = 01;
    }
    }

    /**
    *
    */
    public void setDay (int whatIsDay)
    {
    if ((whatIsDay >= 1) && (whatIsDay <= 31))
    {
    day = whatIsDay;
    }
    else
    {
    day = 01;
    }
    }


    }




    Just for some more background. This class that I am constructing has three fields, to hold year, month and day. Years can be between 1900 and the current year, inclusive. Months can be between 1 and 12
    inclusive. Days can be between 1 and 31 inclusive. I have to use symbolic constants instead of “magic” numbers in the code, e.g. public static final int FIRST_MONTH = 1;

    The default constructor sets year to the current year, month to the first month and day to the first day. The non-default constructor tests each parameter. If the year parameter is outside the acceptable range, it sets the field to the current year. If the month parameter is outside the acceptable range, it sets the field to the first month. If the day parameter is outside the acceptable range, it sets the field to the first day.

    Each field has an accessor method and a mutator method. All three mutator methods check their parameter for validity, and if not valid set the corresponding field in the same way as the nondefault
    constructor.

    This is the part that I am having trouble with. I have to include a method called "public String getFullDate() which returns a string with the date in this format: YYYY-MM-DD e.g. 2012-01-01. Month and day with a single digit are padded with a leading zero."


    Any help whatsoever would be appreciated, even if just an idea Thanks.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    I suspect that your trouble is that you are trying to compare string values with numbers.

    BTW, when posting here please help us to help you by making it easier to copy, test and debug your scripts by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.
    Last edited by Philip M; 02-11-2013 at 12:05 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    58
    Thanks
    4
    Thanked 3 Times in 3 Posts
    As Philip said, this is the wrong language, but i think....

    Code:
    public static final char INSET_HYPHEN = -; //ERROR incompatible types
    should be

    Code:
    public static final char INSET_HYPHEN = '-';


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •