Anniversary Date Tester Create a java GUI application it takes input of “Today\'
ID: 3661560 • Letter: A
Question
Anniversary Date Tester
Create a java GUI application it takes input of “Today's date” and “Your Special date” using Swing JoptionPane as follows: After taking above inputs, it shows the output as Required Output Case #1: Today's Date: 28 December 2015 Special Date: 14 May 2003 Anniversary: 14 May 2015 Sorry, I missed your anniversary! Required Output Case #2: Today's Date : 28 December 2015 Special Date : 31 December 2008 Anniversary : 31 December 2015 Will you have a party? For this project we need to implement the following class files: a) CalendarDay.java: It contains a class “CalendarDay” with required constructors and getters/setter methods. We required to throw exception, if input of day is not between 1 to 31 b) CalenderMonth.java: It contains a class “CalendarMonth” with required constructors and getters/setter methods. Getters must return name of the month and number of days of a month. Throw the errors, if month number is invalid c) CalendarYear.java: It contains a class “CalendarYear” with required constructors and getter/setter methods. A method to check whether year is leap or not. Another method to get number of days of year number d) CalendarDate.java: It is class with above 03-classes as aggregation. It contains a enum with 03constants (STANDARD- Eg: 3 August 2012, US_FORMAT – Eg: Aug 3, 2012 and ISO_8601 – 2012-08-03). Define required constructors and getters/setters. Override toString() method to return string of date in required format. Define a method “twoDigits()” to get a string of digits with leading 0 for single digit values. Another method “compareTo()” to compare two dates and returns 0 or -1 or 1. Finally define a method “anniversary()” , this returns current year calendar date of your special date e) DateTest.java: A final program with main() and other functions to test all above classes
Explanation / Answer
import java.util.*;
import javax.swing.*;
public class DateTester
{
public static String main(String tdate,String special)
{
CalendarDate todaysDate = inputDate(tdate);
CalendarDate specialDate = inputDate(special);
CalendarDate anniversary = specialDate.anniversary(new CalendarYear(2000));
System.out.println("Today's Date : " + todaysDate);
System.out.println("Special Date : " + specialDate);
System.out.println("Anniversary : " + anniversary);
System.out.println(anniversaryMessage(todaysDate, anniversary));
String send=anniversaryMessage(todaysDate, anniversary);
return send;
}
private static CalendarDate inputDate(String prompt)
{
return new CalendarDate(1, 1, 2016);
}
private static String anniversaryMessage(CalendarDate today, CalendarDate bigDay)
{
if (today.compareTo(bigDay) == 0)
{
return "Happy Anniversary!";
}
if (today.compareTo(bigDay) == -1)
{
return "Will you have a party?";
}
else
{
return "Sorry, I missed your anniversary!";
}
}
}
//CALENDAR DATE CLASS:
//An instance of this class represents a Calendar calendar date
// 1582 or later
class CalendarDate
{
public enum DateFormat
{
STANDARD, //3 August 2012
US_FORMAT, //Aug 3, 2012
ISO_8601; //2012-08-03
}
//Instance (State) Variables
CalendarYear year; //A Calendar year
private CalendarMonth month; //A Calendar month
private CalendarDay day; //1 .. 28/29/30/31
private DateFormat format; //Display format
//Constructor
public CalendarDate(int day, int month, int year)
{
this.year = new CalendarYear (year);
this.month = new CalendarMonth (month);
this.day = new CalendarDay(day);
this.format = DateFormat.STANDARD;
if (day > this.month.numberOfDays(true))
throw new RuntimeException("Invalid Day # " + day);
if (day == 29 && month == 2 && this.year.isLeapYear() == false)
throw new RuntimeException("Invalid Day # " + day);
}
//Accessors
public CalendarDay getDay()
{
return this.day;
}
public CalendarMonth getMonth()
{
return this.month;
}
public CalendarYear getYear()
{
return this.year;
}
//Mutator
public void setFormat(CalendarDate.DateFormat format)
{
this.format = format;
}
//Override
//The format of the returned image of this CalendarDate
//is determined by the value of instance variable format
@Override
public String toString()
{
switch ( this.format )
{
case STANDARD : return twoDigits(this.day.getNumber()) + " " +
this.month.name() + " " +
this.year.getNumber();
case US_FORMAT :
case ISO_8601 :
default : return twoDigits(this.month.getNumber()) + " " +
twoDigits(this.day.getNumber()) + " " +
this.year.getNumber();
}
}
//Helper
private static String twoDigits(int number)
{
return (number < 10 ? "0" : "") + number;
}
//Compare this CalendarDate with an other CalendarDate
// Returns 0 if the dates are identical
// any negative number if this precedes other
// any positive number if this succeeds other
public int compareTo(CalendarDate other)
{
if (this.day.getNumber() == other.day.getNumber() &&
this.month.getNumber() == other.month.getNumber() &&
this.year.getNumber() == other.year.getNumber())
{
return 0;
}
else if (this.day.getNumber() < other.day.getNumber() &&
this.month.getNumber() <= other.month.getNumber() &&
this.year.getNumber() <= other.year.getNumber())
{
return -1;
}
else
{
return 1;
}
}
//Return the anniversary date in another year of this CalendarDate
// Example, the 2015 anniversary date of 05/18/1995 is 05/18/2015
// @param year: the anniversary year
public CalendarDate anniversary(CalendarYear year)
{
return this;
}
}
//CALENDAR DAY CLASS:
//An instance of this class represents a day of the Calendar calendar
//
class CalendarDay
{
//Class Constants
public static final int MIN_DAY_NUMBER = 1;
public static final int MAX_DAY_NUMBER = 31;
//Instance (State) Variable
private int number; //1 .. 31
//Constructor
public CalendarDay(int number)
{
if ( number < 1 || number > 31 ) //Test for an invalid Day number
throw new RuntimeException("Invalid Day # " + number);
this.number = number;
}
//Accessor
public int getNumber()
{
return this.number;
}
}
//CALENDAR MONTH CLASS:
//An instance of this class represents a month of the Calendar calendar
//
class CalendarMonth
{
//Class Constants
public static final int MIN_MONTH_NUMBER = 1;
public static final int MAX_MONTH_NUMBER = 12;
//Instance (State) Variable
private int number; // 1 .. 12
//Constructor
public CalendarMonth(int number)
{
if ( number < 1 || number > 12 )
throw new RuntimeException("Invalid Calendar Month Number " + number);
this.number = number;
}
//Accessor
public int getNumber()
{
return this.number;
}
//Return the name of this CalendarMonth
public String name()
{
switch ( this.number )
{
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default: throw new RuntimeException("Invalid Month Number");
}
}
//Return the number of days of this CalendarMonth
public int numberOfDays(boolean isLeapYear)
{
switch ( this.number )
{
case 1: return 31;
case 2: return 29;
case 3: return 31;
case 4: return 30;
case 5: return 31;
case 6: return 30;
case 7: return 31;
case 8: return 31;
case 9: return 30;
case 10: return 31;
case 11: return 30;
case 12: return 31;
default: return 31;
}
}
}
//CALENDAR YEAR CLASS:
//An instance of this class represents a Calendar year, 1582 -
class CalendarYear
{
//Class Constant: Initial year of the Calendar calendar
public static final int MIN_YEAR_NUMBER = 1582;
//Instance (State) Variable
private int number;
//Constructor
public CalendarYear(int number)
{
if (number < 1582)
throw new RuntimeException("Pre-Calendar Year " + number);
this.number = number;
}
//Accessor
public int getNumber()
{
return this.number;
}
//Return true if this CalendarYear is a leap-year, false otherwise
public boolean isLeapYear()
{
if (this.number % 400 == 0 && this.number > MIN_YEAR_NUMBER)
return true;
else if (this.number % 4 == 0 && this.number > MIN_YEAR_NUMBER)
return true;
return false;
}
//Return the number of days in this CalendarYear, 365 or 366 (Leap)
public int numberOfDays()
{
if (isLeapYear() == false)
return 365;
return 366;
}
}
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Label;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Anniversery {
private JFrame frame;
private JTextField textField;
private JTextField textField1;
JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Anniversery window = new Anniversery();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Anniversery() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Label label = new Label("Enter Anniversery Date");
label.setBounds(25, 81, 120, 22);
frame.getContentPane().add(label);
textField = new JTextField();
textField.setBounds(154, 83, 135, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField1 = new JTextField();
textField1.setBounds(159, 93, 145, 40);
frame.getContentPane().add(textField1);
textField1.setColumns(10);
Label label_1 = new Label("Anniversery Date Calculatir");
label_1.setBounds(110, 21, 237, 39);
frame.getContentPane().add(label_1);
textArea = new JTextArea();
textArea.setBounds(38, 138, 350, 64);
frame.getContentPane().add(textArea);
Button button = new Button("submit");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textArea.setText("");
String date=textField.getText();
String sdate=textField1.getText();
String msg=DateTester.main(date,sdate);
textArea.setText(msg);
}
});
button.setBounds(306, 81, 70, 22);
frame.getContentPane().add(button);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.