The files must be named as follows: <LastNameFirstNameDateDriver.java>. Date.jav
ID: 655317 • Letter: T
Question
The files must be named as follows: <LastNameFirstNameDateDriver.java>.
Date.java (Date class file)
Ensure you include ALL files required to make your program compile and run.
I would like to see your .java files only.
Proper coding conventions required the first letter of the class start with a capital
letter and the first letter of each additional word start with a capital letter.
5%
Overall Requirements
Create a class named Date that stores date values and prints out the date in either a
pure numeric format or a name and number format (see sample session for format
details).
Date.java class file
Explanation / Answer
Driver Code:
import java.util.Scanner;
public class SilvaLuisDateDriver {
public static void main(String args[])
{
Date d;
while(true)
{
Scanner stdInput = new Scanner(System.in);
System.out.print("Enter a date in the form mm/dd (q to quit): ");
String quit = stdInput.next();
if(quit.equalsIgnoreCase("q")){
System.exit(0);
}
else
d = new Date(quit);
if((d.getErrorMsg()) == null)
{
System.out.println(d.getMonthNumber()+"/"+d.getDay());
System.out.println(d.getMonth()+" "+d.getDay());
}
else
System.out.println(d.getErrorMsg());
}
}
}
//Date .java
public class Date{
private int day;
private int month;
private String error = null;
public Date(String dateStr)
{
int index = dateStr.indexOf('/');
if(index!=-1)
{
String mm = dateStr.substring(0,index);
String dd = dateStr.substring(index+1,dateStr.length());
try
{
setMonth(Integer.parseInt(mm));
try
{
setDay(Integer.parseInt(dd));
}
catch(InputMismatchException e)
{
error = "Invalid format - For input string- " + dd;
}
}
catch(Exception e)
{
error = "Invalid format - For input string- " + mm;
}
}
else
error = "Invalid date format- " + dateStr;
}
public void setMonth(int month){
if(month>0 && month<13)
this.month=month;
else
error = "Invalid month- " + month;
}
public void setDay(int d){
if(d<=0)
error = "Invalid day- " + d;
else if(month==2 && d>29 )
error = "Invalid day- " + d;
else if((month==1 || month==3 || month==5 || month==7 || month ==8 || month ==10 || month==12) && d >31)
error = "Invalid day- " + d;
else if((month ==4 || month==6 || month==9 ||month==11) && d>30)
error = "Invalid day- " + d;
else
day = d;
}
public String getMonth()
{
String monthString = "";
switch(this.month)
{
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "Novenber";
break;
case 12: monthString = "December";
break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.