In: Computer Science
Java program
Create class DateClass with the following capabilities:
Output the date in multiple formats, such as
MM/DD/YYYY June 14, 1992 DDD YYYY
Use overloaded constructors to create DateClass objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day number in the year.
If someone enters the day as the 14 and the month is june, the program should be able to convert that to the day out of 365 for the third format. No null values. The program should convert whatever the user enters and print out all three date formats correctly.
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Below java classes are created.
DateClass.java :
//Java class
public class DateClass {
//constructor
public DateClass(int mm,int dd,int yyyy)
{
//print date in the format
MM/DD/YYYY
System.out.println(mm+"/"+dd+"/"+yyyy);
}
//overloaded constructor
public DateClass(String mm,int dd,int yyyy)
{
//print date in the format June 14,
1992
System.out.println(mm+"
"+dd+","+yyyy);
}
public DateClass(int dd,int yyyy)
{
//print date in the format DDD
YYYY
System.out.println(dd+"
"+yyyy);
}
}
**********************************
DateClassTest.java :
import java.util.*;//import package
import java.time.LocalDate;
//Java class
public class DateClassTest {
// entry point of program , main() method
public static void main(String[] args) {
// creating object of scanner
class
Scanner sc = new
Scanner(System.in);
// asking user to enter day
System.out.print("Enter Day :
");
int day = sc.nextInt();// reading
day
// asking user to enter month
System.out.print("Enter Month :
");
String month = sc.next();// reading
month
// asking user to enter year
System.out.print("Enter Year :
");
int year = sc.nextInt();// reading
year
// declaring variable to store
month number
int monthNumber = 0;
// array to store month
number
String[] months = { "january",
"february", "march", "april", "may", "june", "july", "august",
"september",
"october", "november", "december" };
// finding month number using for
loop
for (int i = 0; i <
months.length; i++) {
if
(month.toLowerCase().equals(months[i])) {
// if month is found in the month array
monthNumber = i;// set i as month number
break;// break the loop
}
}
// creating object of
DateClass
DateClass date2 = new
DateClass(monthNumber + 1, day, year);// object 1
DateClass date1 = new
DateClass(month, day, year);// object 2
// getting day number from the
date
int dayNumber = LocalDate.of(year,
monthNumber + 1, day).getDayOfYear();
DateClass date3 = new
DateClass(dayNumber, year);
}
}
======================================================
Output : Compile and Run DateClassTest,java to get the screen as shown below
Screen 1 :DateClassTest.java
Screen 2 :
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.