Answer to Question #77297 in Java | JSP | JSF for Renier Markram

Question #77297
Write a program that converts dates from numerical month-day format to alphabetic month-day format. For example, input of 1/31 or 01/31 would produce January 31 as output. The dialogue with the user should be similar to that shown in Programming Project 2. You should define two exception classes, one called MonthException and another called DayException. If the user enters anything other than a legal month number (integers from 1 to 12), your program should throw and catch a MonthException. Similarly, if the user enters anything other than a valid day number (integers from 1 to either 29, 30, or 31, depending on the month), your program should throw and catch a DayException. To keep things simple, assume that February always has 28 days.
1
Expert's answer
2018-05-16T05:45:08-0400
cat DateConverter.java :

import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;

public class DateConverter {

public static void main(String[] args) {
System.out.println("Please Enter date in Pattern MM/DD and press Enter:");
Scanner scan = new Scanner(System.in);
String enteredDate = scan.nextLine();
int month = Integer.valueOf(enteredDate.substring(0, 2));
int day = Integer.valueOf(enteredDate.substring(3, enteredDate.length()));
Map<Integer, String> year = new TreeMap<Integer, String>();
year.put(1, "January");
year.put(2, "February");
year.put(3, "March");
year.put(4, "April");
year.put(5, "May");
year.put(6, "June");
year.put(7, "July");
year.put(8, "August");
year.put(9, "September");
year.put(10, "October");
year.put(11, "November");
year.put(12, "December");
try {
MonthException.genException(month);
} catch (Exception e) {
e.printStackTrace();
}
try {
DayException.genException(month,day);
} catch (Exception e) {
e.printStackTrace();
}
if(month<=12||month>1) {
String lookingForMonth="";
for(Entry<Integer, String> entr:year.entrySet()) {
int monthNumber=entr.getKey();
String nameOfMonth=entr.getValue();
if(month==monthNumber)lookingForMonth=nameOfMonth;
}
System.out.println(lookingForMonth+" "+day);
}
scan.close();
}

}


DayException.java :
public class DayException {
public static void genException(int month, int day) {
if (month == 2 && (day < 1 || day > 29)) {
System.out.println("Wrong format of day!");
}else if((month==1||month==3||month==5||month==6||month==8||month==10||month==12)&& (day < 1 || day > 31)) {
System.out.println("Wrong format of day!");
}else if((month==4||month==7||month==9||month==11)&& (day < 1 || day > 30)) {
System.out.println("Wrong format of day!");
}
}


MonthException.java :
public class MonthException {
public static void genException(int month) {
if(month<1||month>12) {
System.out.println("Wrong format of month!");
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS