Read the Java online API documentation for the split method

Read the Java online API documentation for the split method in the String class and the In- teger.parselnt method in the Integer class. They may be handy when dealing with inputs. Additional Requirements All methods must be public and all instance variables private. Do not use if statements to deter- mine the name of the weekday or month: declare two arrays of strings, one with the names of the months and the other with the days of the week, and use the appropriate indexes to obtain them when needed. Avoid any other unnecessary us ofif statements. Exhaustively, testyour program to ensure that it works. (Y may find the calendar generator at http:INwww.dayoftheweek.org ou helpful in verifying the correctness of your program.) Do not test your program using an invalid date or a date before 1583. Here are two sample program interactions: Listing 1: Sample Run 1. 1 Enter the first day of class 8/22/2016 3 First day of class: Monday, August 22, 2016 4 Last day of class: Monday. August 22, 2016 6 Enter the last day of class 12/8/2016 8 First day of class. Monday, August 22, 2016 9 Last day of class: Thursday, December 8, 2016 Listing 2 Sample Run 2. 1 Enter the first day of class 2/7/1900 3 First day of class Wednesday, February 7, 1900 4 Last day of class Wednesday, February 7. 1900 6 Enter the last day of class 1/19/1901 8 First day of class: Wednesday, February 7, 1900 9 Last day of class: Saturday. January 19. 1901

Solution

import java.util.*;
public class Date {

private int day; // days
private int month; //months 1 to 12
private int year; // year

// week day names
private static final String[] DAYNAMES = {\"Sunday\", \"Monday\", \"Tuesday\",
\"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"};

// year month names
private static final String[] MONTHNAMES = {\"\", \"January\", \"February\",
\"March\", \"April\", \"May\", \"June\", \"July\", \"August\",
\"September\", \"October\", \"November\", \"December\"};

// month day dates
private static final int[] MONTHDAYS = {0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
// supporting date upto 1582 year calender
private static final int FIRST_MONTH = 10;
private static final int FIRST_DAY = 15;
private static final int FIRST_YEAR = 1582;
private static final Date FIRST_DATE = new Date(FIRST_MONTH, FIRST_DAY, FIRST_YEAR);

private static final int START_DAY_INDEX = 5;

public Date (Date date){
this.month = date.month;
this.day = date.day;
this.year = date.year;
}
public Date(int month, int day, int year) {
String s = month + \"/\" + day + \"/\" + year;

// check for correct/wrong month
if (month > 12 || month < 1) {
throw new DateException(s + \"\ Illegal month: \" + month
+ \" (Month must be between 1 and 12)\");
}

// check for year correct or not
if ((year < 1582)
|| (year == 1582 && month < 10)
|| (year == 1582 && month == 10 && day < 15)) {
throw new DateException(s
+ \" (Sorry we are not supporting this year as of now.)\");
}

// check for leap year
if (month == 2 && day == 29 && !isLeapYear(year)) {
throw new DateException(s + \" (\" + year + \" is not a leap year)\");
}

// check given month is legal or not
if ((day <= 0)
|| (month == 2 && isLeapYear(year) && day > 29)
|| (month == 2 && !isLeapYear(year) && day > 28)
|| (month != 2 && day > MONTHDAYS[month])) {
throw new DateException(s + \"\ Illegal day: \" + day + \"\ For \"
+ (month == 2 ? \"February \" + year : MONTHNAMES[month])
+ \", must be between 1 and \"
+ (isLeapYear(year) && month == 2 ? 29
: MONTHDAYS[month]));
}

// here if month, day, and year are all good
this.month = month;
this.day = day;
this.year = year;
}
  
public void setDate(int month, int day, int year){
this.month = month;
this.day = day;
this.year = year;
}
// getter day method
public int getDay() {
return day;
}

// getter month method
public int getMonth() {
return month;
}

// getter int method
public int getYear() {
return year;
}

// getter month method
public String getMonthName() {
return MONTHNAMES[month];
}

// return day week
public String getDayOfWeek() {

// create sample temp date
Date tempDate = new Date(FIRST_MONTH, FIRST_DAY, FIRST_YEAR);

int dayNameIndex = START_DAY_INDEX;

// check until match found
while (!tempDate.equals(this)) {
tempDate.next();
dayNameIndex = (dayNameIndex + 1) % 7;
}
return DAYNAMES[dayNameIndex];
}

// get short date
public String getShortDate() {
String out = \"\";
if (month <= 9) // add leading zero if single digit month
{
out += \"0\";
}
out += month + \"/\";
if (day <= 9) // add leading zero if single digit day
{
out += \"0\";
}
return out + day + \"/\" + year;
}

// get simple dare
public String getMediumDate() {
return MONTHNAMES[month] + \" \" + day + \", \" + year;
}

// det date in string format
public String getLongDate() {
String out = this.getDayOfWeek() + \", \" + MONTHNAMES[month]
+ \" \" + day;
switch (day) {
case 1:
out += \"st, \";
break;
case 2:
out += \"nd, \";
break;
case 3:
out += \"rd, \";
break;
case 21:
out += \"st, \";
break;
case 22:
out += \"nd, \";
break;
case 23:
out += \"rd, \";
break;
case 31:
out += \"st, \";
break;
default:
out += \"th, \";
}
return out + year;
}

// check leapyear
private boolean isLeapYear() {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

// check specific year is leap year or not
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
   public static void main(String args[]){
//create scanner object
Scanner sc = new Scanner(System.in);
System.out.println(\"Enter first day of class --> \");
String firstDay = sc.nextLine();

System.out.println(\"Enter last day of class --> \");
String lastDay = sc.nextLine();
  
// psrase string
String[] firstDayArr = firstDay.split(\"/\");
String[] lastDayArr = lastDay.split(\"/\");
//create date objects
Date date1 = new Date(Integer.parseInt(firstDayArr[0]),Integer.parseInt(firstDayArr[1]),Integer.parseInt(firstDayArr[2]));
Date date2 = new Date(Integer.parseInt(lastDayArr[0]),Integer.parseInt(lastDayArr[1]),Integer.parseInt(lastDayArr[2]));
// printing results
System.out.println(\"First day of class: \"+date1.getLongDate());
System.out.println(\"Last day of class: \"+date2.getLongDate());
}
}

 Read the Java online API documentation for the split method in the String class and the In- teger.parselnt method in the Integer class. They may be handy when
 Read the Java online API documentation for the split method in the String class and the In- teger.parselnt method in the Integer class. They may be handy when
 Read the Java online API documentation for the split method in the String class and the In- teger.parselnt method in the Integer class. They may be handy when
 Read the Java online API documentation for the split method in the String class and the In- teger.parselnt method in the Integer class. They may be handy when

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site