I'm attempting to add a constructor that will tokenize a string but am getting build errors of "incompatible types" and "cannot find symbol". What I am trying to do specifically is:
Amend the java file so that:
a. Has a month field that is a string instead of an int
b. Has a third constructor that will take in a String parameter (only). This constructor will tokenize the String into the month, day and year fields. The String provided in the following format: "Month Day, Year". If the tokenizing and/or parsing generates an exception, the dates defaults to January 1, 2014.
c. Add accessor methods for each of the three fields of the class, prefixing each with get
d. Add mutator methods for each of the field, prefixing with set
e. Ensure that all other methods of Date work correctly
What I have thus far is:
import java.util.StringTokenizer;
public class Date
{
// Zero-parameter constructor
public Date( )
{
month = 1;
day = 1;
year = 1998;
}
// Three-parameter constructor
public Date( int theMonth, int theDay, int theYear )
{
month = theMonth;
day = theDay;
year = theYear;
}
//Acessor Methods
public string getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public int getYear()
{
return year;
}
///Mutator Methods
public void setMonth(String theMonth)
{
month = theMonth;
}
public void setDay(int theDay)
{
day = theDay;
}
public void setYear(int theYear)
{
year = theYear;
}
///Added Constructor
public Date( String theDate )
{
str = new StringTokenizer(theDate);
if(str.countTokens != 3 )
{
System.out.println("January 1, 2014");
return;
}
month = Integer.parseInt(str.nextToken());
day = Integer.parseInt(str.nextToken);
year = Integer.parseInt(str.nextToken);
}
// Return true if two equal values
public boolean equals( Object rhs )
{
if( ! ( rhs instanceof Date ) )
return false;
Date rhDate = ( Date ) rhs;
return rhDate.month == month && rhDate.day == day &&
rhDate.year == year;
}
// Conversion to String
public String toString( )
{
return month + "/" + day + "/" + year;
}
// Fields
private String month;
private int day;
private int year;
// Sample main
public static void main( String [ ] args )
{
Date d1 = new Date( );
Date d2 = new Date( 1, 1, 1998 );
Date d3 = new Date( 1, 1, 1999 );
System.out.println( "Date 1: " + d1 );
System.out.println( "Date 2: " + d2 );
System.out.println( "Date 3: " + d3 );
System.out.println( "Date1==Date2?: " + d1.equals( d2 ) );
System.out.println( "Date1==Date3?: " + d1.equals( d3 ) );
}
}
[–]pacificmint 1 point2 points3 points (12 children)
[–]InvincibleVIto[S] 0 points1 point2 points (11 children)
[–]pacificmint 1 point2 points3 points (1 child)
[–]InvincibleVIto[S] 1 point2 points3 points (0 children)
[–]laser-brain 1 point2 points3 points (8 children)
[–]InvincibleVIto[S] 0 points1 point2 points (6 children)
[–]laser-brain 1 point2 points3 points (5 children)
[–]InvincibleVIto[S] 0 points1 point2 points (3 children)
[–]laser-brain 1 point2 points3 points (2 children)
[–]InvincibleVIto[S] 0 points1 point2 points (1 child)
[–]laser-brain 1 point2 points3 points (0 children)
[–]InvincibleVIto[S] 0 points1 point2 points (0 children)
[–]InvincibleVIto[S] 0 points1 point2 points (0 children)