1. Date
by CodeChum Admin
Create a new file called Date.java and construct a Date class that models a calendar date with day, month and year. It contains the following members:
more info here.
https://pastebin.com/cGAumGnt
import java.util.*;
enum Day {
MON, TUE, WED, THU, FRI, SAT, SUN;
}
class Date {
// private integer instance variables: day, month (1 - 12), and year (4-digit)
private int day, month, year;
/**
* a default constructor which will set the date to 01/01/1972. When called,
* this also prints the string "Default Constructor"
*/
public Date() {
setDate(1, 1, 1972);
System.out.println("Default Constructor");
}
/**
* an overloaded constructor that takes in the day, month, and year as
* arguments. When called, this also prints the string "Overloaded Constructor"
*
* @param day
* @param month
* @param year
*/
public Date(int day, int month, int year) {
setDate(day, month, year);
System.out.println("Overloaded Constructor");
}
/**
* @return the day
*/
public int getDay() {
return day;
}
/**
* @param day the day to set
*/
public void setDay(int day) {
this.day = day;
}
/**
* @return the month
*/
public int getMonth() {
return month;
}
/**
* @param month the month to set
*/
public void setMonth(int month) {
this.month = month;
}
/**
* @return the year
*/
public int getYear() {
return year;
}
/**
* @param year the year to set
*/
public void setYear(int year) {
this.year = year;
}
/**
* sets the day, month, and year based on the arguments
*
* @param day
* @param month
* @param year
*/
public void setDate(int day, int month, int year) {
this.setDay(day);
this.setMonth(month);
this.setYear(year);
}
/**
* returns the day of the week (MON, TUE, WED, THU, FRI, SAT, SUN) for the date.
* Create an enum Day to handle this. Follow the formula below for this.
*
* @return
*/
public String dayOfTheWeek() {
// If the month is January or February, subtract 1 from the year to get a new Y
// and add 12 to the month to get a new M.
int Y = year;
int M = month;
int D = day;
if (month == 1 || month == 2) {
Y = year - 1;
M = month + 12;
}
double A = Y / 100;
double B = A / 4;
double C = 2 - A + B;
double E = 365.25 * (Y + 4716);
double F = 30.6001 * (M + 1);
double JD = C + D + E + F - 1524.5;
int dayOfWeek = (int) Math.round((JD % 7));
return "" + Day.values()[dayOfWeek - 1];
}
/***
* returns "DD/MM/YYYY", with leading zero for DD and MM if applicable (note:
* return the string and do not display it from the method itself)
*/
public String toString() {
return String.format("%02d/%02d/%d", day, month, year);
}
}
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int mode = sc.nextInt();
Date date = null;
if (mode == 1) {
date = new Date();
} else if (mode == 2) {
int day = sc.nextInt();
int month = sc.nextInt();
int year = sc.nextInt();
date = new Date(day, month, year);
}
int m = sc.nextInt();
for (int ctr = 0; ctr < m; ctr++) {
int operation = sc.nextInt();
if (operation == 1) {
int day = sc.nextInt();
date.setDay(day);
} else if (operation == 2) {
int month = sc.nextInt();
date.setMonth(month);
} else if (operation == 3) {
int year = sc.nextInt();
date.setYear(year);
} else if (operation == 4) {
int day = sc.nextInt();
int month = sc.nextInt();
int year = sc.nextInt();
date.setDate(day, month, year);
} else if (operation == 5) {
System.out.println(date.getDay());
} else if (operation == 6) {
System.out.println(date.getMonth());
} else if (operation == 7) {
System.out.println(date.getYear());
} else if (operation == 8) {
System.out.println(date.toString());
} else if (operation == 9) {
System.out.println(date.dayOfTheWeek());
}
}
}
}
Comments
Thank you so much
Leave a comment