Reputation: 399
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.
Upvotes: 0
Views: 240
Reputation: 5533
public static final char INSET_HYPHEN = '-';
instead of public static final char INSET_HYPHEN = -;
- Characters should always be defined between apostrophes.getFullDate()
is wrong, and it is anyway much better to let String.format
do the dirty work for you. Also, getFullDate()
returns year + month + day
, which is an integer, instead of returning fullDate
.This code should work:
public class Date {
private static final int DEFAULT_YEAR = 2013;
private static final int DEFAULT_MONTH = 1;
private static final int DEFAULT_DAY = 1;
private static final char SEPARATOR = '-';
private int year;
private int month;
private int day;
/**
* Default constructor
*/
public Date() {
this(DEFAULT_YEAR, DEFAULT_MONTH, DEFAULT_DAY);
}
/**
*
*/
public Date (int year, int month, int day) {
setYear(year);
setMonth(month);
setDay(day);
}
/**
*@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() {
return String.format("%d%c%02d%c%02d", year, SEPARATOR, month, SEPARATOR, day);
}
/**
*
*/
public void setYear (int year)
{
this.year = ((year >= 1990) && (year <= DEFAULT_YEAR)) ? year : DEFAULT_YEAR;
}
/**
*
*/
public void setMonth (int month)
{
this.month = ((month >= 1) && (month <= 12)) ? month : DEFAULT_MONTH;
}
/**
*
*/
public void setDay (int day)
{
this.day = ((day >= 1) && (day <= 31)) ? day : DEFAULT_DAY;
}
}
Upvotes: 0
Reputation: 2390
you can not add a string literal, and if you want to use something as above : then try this
public static final CHARACTER INSET_HYPHEN = new CHARACTER('-');
or
public static final char INSET_HYPHEN = '-';
Upvotes: 0
Reputation: 46428
apart from the syntax error of declaring an charcter incorrectly you are trying to access local variables declared in your constructor in a getFullDate() method
public Date (int whatIsYear, int whatIsMonth, int whatIsDay)
public String getFullDate()
{
String fullDate;
if (whatIsMonth < 10); // the year, month, and day all give me incompatible
//rest of the code
}
whatIsMonth , whatIsDay are local variables defined in your constructor, you can't use these variables outside the constructor.
your getFullDate() method should rather be using your instance variables year
, month
and 'day'. note that int whatIsYear, int whatIsMonth, int whatIsDay
are only constructor arguments and are confined to your constructor only. you can't access them outside the constructor.
Upvotes: 0
Reputation: 1125
char
type only holds data enclosed in single quotation mark. If you want to use double quotation mark then your datatype should be string
Upvotes: 0
Reputation: 1690
You should use single quotes:
public static final char INSET_HYPHEN = '-';
Upvotes: 6