Hello, so I am having trouble with establishing two classes for this particular
ID: 3817226 • Letter: H
Question
Hello, so I am having trouble with establishing two classes for this particular programming assignment which is in the C++ language. The two classes must be Application and Date. The program I use for assignments is blue jay.
Assignment:
Objective: To reinforce the understanding of switch statements and object-oriented programming.
Directions:Your program should allow the user to input a date by prompting for the month, day and year in that order. The date is then checked for validity according to the rules of the Gregorian calendar. A message concerning the validity of the date is then printed. Each month has a valid range of days given by the rhyme – Thirty days hath September, April, June, and November. All the rest have 31, Except for February all alone, It has 28 each year, but 29 each leap year. A leap year occurs when the year number is divisible by four, unless the year is divisible by 100 when the year is not a leap year. There is a further exception – if a year is divisible by 400 then it is a leap year.
Sample Output:
Enter day: 29
Enter month: 2
Enter year: 2011
The date 2/29/2011 is not valid.
Alternatively,
Enter day: 30
Enter month: 10
Enter year: 2015
The date 10/30/2015 is valid.
I've included the UML my instructor wants us to use.
Application main (arguments String) Date day: int month int year int Date day: int,month int,year nt) divides (a int,b int) boolean is Leap Year() boolean days In Month(): int isValid(): boolean toString StringExplanation / Answer
import java.util.*;
class Date
{
private int day;
private int month;
private int year;
public Date(int day,int month,int year) //constructor
{
this.day = day;
this.month = month;
this.year = year;
}
public boolean divides(int a,int b)
{
if(a%b == 0)
return true;
else
return false;
}
public boolean isLeapYear()
{
if(divides(year,4) && divides(year,400) && !divides(year,100))
return true ;
else
return false;
}
public int daysInMonth()
{
switch(month) //switch to validate days for months
{
case 1: day = 31;
break;
case 2: if(isLeapYear()) day = 28;
else day = 28;
break;
case 3: day = 31;
break;
case 4: day = 30;
break;
case 5: day = 31;
break;
case 6: day = 30;
break;
case 7: day = 31;
break;
case 8: day = 31;
break;
case 9: day = 30;
break;
case 10: day = 31;
break;
case 11: day = 30;
break;
case 12: day = 31;
break;
default : day = -1;
break;
}
return day;
}
public boolean isValid() //validity check
{
if(daysInMonth() == -1)
return false;
else if(day >= 1 && day <= daysInMonth() && month>=1 && month <=12 )
return true;
else
return false;
}
public String toString()
{
return " "+month+"/"+day+"/"+year;
}
}
class TestDate
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter day: ");
int day = scan.nextInt();
System.out.println(" Enter month: ");
int month = scan.nextInt();
System.out.println(" Enter year: ");
int year = scan.nextInt();
Date dt = new Date(day,month,year);
if(dt.isValid())
System.out.println("The date "+month+"/"+day+"/"+year +" is valid");
else
System.out.println("The date "+month+"/"+day+"/"+year +" is not valid");
}
}
Output:
Enter day: 29
Enter month: 2
Enter year: 2011
The date 2/29/2011 is not valid.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.