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?
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?
Comment