Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello. I have my entire class written and it works, but I can\'t figure out how

ID: 3534888 • Letter: H

Question

Hello.


I have my entire class written and it works, but I can't figure out how to print the output (using printf) to the the output I need. The sample session that I need is:

Enter a date in the form mm/dd ("q" to quit): 5/2
05/02
May 2
Enter a date in the form mm/dd ("q" to quit): 05/02
05/02
May 2
Enter a date in the form mm/dd ("q" to quit): 52
Invalid date format – 52
Enter a date in the form mm/dd ("q" to quit): 5.0/2
Invalid format - For input string: "5.0"
Enter a date in the form mm/dd ("q" to quit): 13/2
Invalid month – 13
Enter a date in the form mm/dd ("q" to quit): 2/x
Invalid format - For input string: "x"
Enter a date in the form mm/dd ("q" to quit): 2/30
Invalid day – 30
Enter a date in the form mm/dd ("q" to quit): 2/28
02/28
February 28
Enter a date in the form mm/dd ("q" to quit): q


However, my numerical dates are

5/2

Can you help me to get it to: 05/02?

Here's my code:


/*

* LastNameFirstNameDateDriver.java

*/

import java.util.Scanner;

public class WallaceRandyDateDriver {

public static void main(String[] args){

Date d;

while(true)

{

System.out.print("Enter a date in the form mm/dd (q to quit): ");

String dt=new Scanner(System.in).next();

if(dt.equals("q"))

System.exit(0);

d=new Date(dt);

if((d.getErrorMsg()) == null)

{

System.out.println(d.getMonthNumber()+"/"+ d.getDay());

System.out.println(d.getMonth()+" "+d.getDay());

}

else

System.out.println(d.getErrorMsg());

}

}

}

public class Date {

// Day, Month, and error variables

private int day, month;

private String error=null;

// Creates a new instance of Date

public Date(String dateStr){

//finds the position of the / character

int index=dateStr.indexOf("/");

if(index!=-1){

// gets the month & day from the given string

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(Exception e){

error="invalid format - For input string- "+dd;

}

}

catch(Exception e){

error="invalid format - For input string- "+mm;

}

} // end exceptions block

else

error="Invalid date format- "+dateStr; // prints if '/' is not found

} // end Date

//sets the month or determines month invalid

public void setMonth(int m){

if(m>0 && m<13)

month=m;

else

error="Invalid month- "+m;

} // end setMonth

//sets the day or determines day invalid

public void setDay(int d){

if(d<=0)

error="Invalid day- "+d;

else if(month==2 && d>28 )

error="Invalid day- "+d;

else if((month==1 || month==3 || month==5 || month==7 || month ==8 || month ==10 || month==12) && d >31)

error= "Inalid day- "+d;

else if((month ==4 || month==6 || month==9 ||month==11) && d>30)

error="Invlid day- "+d;

else

day=d;

}

//returns month in string format

public String getMonth(){

String mnthString="";

switch(this.month)

{

case 1: mnthString= "January"; break;

case 2: mnthString= "February"; break;

case 3: mnthString= "March"; break;

case 4: mnthString= "April"; break;

case 5: mnthString= "May"; break;

case 6: mnthString= "June"; break;

case 7: mnthString= "July"; break;

case 8: mnthString= "August"; break;

case 9: mnthString= "September"; break;

case 10: mnthString= "October"; break;

case 11: mnthString= "Novenber"; break;

case 12: mnthString= "December"; break;

}

return mnthString;

}

//gets the day

public int getDay(){

return day;

}

//gets month in integer format

public int getMonthNumber(){

return month;

}

//returns error message

public String getErrorMsg(){

return error;

} // end getErrorMsg method

} // end Date class

Explanation / Answer

Changed As per your requirement :


import java.util.Scanner;

public class WallaceRandyDateDriver {

public static void main(String[] args){

Date d;

while(true)

{

System.out.printf("Enter a date in the form mm/dd (q to quit): ");

String dt=new Scanner(System.in).next();

if(dt.equals("q"))

System.exit(0);

d=new MyDate(dt);

if((d.getErrorMsg()) == null)

{

System.out.printf(d.getMonthNumber()+"/"+ d.getDay());

System.out.printf(" "+d.getMonth()+" "+d.getDay());

}

else

System.out.printf(" "+d.getErrorMsg());

}

}

}