Need help figuring out what would be the all 12 days output
public class Date {
private int day;
private int month;
private int year;
private static int tracker;
public Date()
{
this(1, 1, 2000);
System.out.println("4");
}
public Date(int year)
{
this(1, 1, year);
System.out.println("3");
}
public Date(int month, int year)
{
this(1, month, year);
System.out.println("2");
}
public Date(int day, int month, int year)
{
setDate ( day, month, year);
tracker ++;
}
public Date(Date r)
{
this(r.day, r.month, r.year);
System.out.println("1");
}
public void setDate (int day, int month, int
year)
{
setMonth(month);
setDay(day);
setYear(year);
}
public void setDay(int day)
{
int[] daysPerMonth = {0, 31, 28,
31, 30, 31, 30, 31, 31, 30, 31};
this.day = ((day > 0 &&
day < daysPerMonth[month]) ? day : 1);
}
public void setMonth(int month)
{
this.month = ((month> 0
&& month <= 12) ? month : 1);
}
public void setYear(int year)
{
this.year = year;
}
public static int getTracker ()
{
return tracker;
}
public String toString()
{
return
String.format("%02d-%02d=%02d", month, day, year !=0 ? year :
2000);
}
}
-------------------------
public class Q1
{
public static void main (String [] args)
{
System.out.printf("%d\n",
Date.getTracker());
Date[] dateList = new
Date[12];
dateList[0] = new Date();
dateList[1] = new Date(2020);
dateList[2] = new Date(3,
2020);
dateList[3] = new Date(25, 3,
2020);
dateList[4] = new Date(35, 35,
2050);
dateList[5] = new
Date(dateList[3]);
System.out.printf("%d\n",
dateList[0].getTracker());
for (int i = 0; i<
dateList.length; i++)
System.out.printf("%d\n", dateList[i]);
}
}
Let’s go through each line in Q1 class to figure out the output.
System.out.printf("%d\n", Date.getTracker());
This will initially print the number of Date objects created. Initially no objects are created, so this will be 0.
Date[] dateList = new Date[12];
This will create an array of 12 Date objects. All elements will initially be null.
dateList[0] = new Date();
This will initialize Date using default constructor, which itself calls the constructor taking day, month and year passing values 1, 1 and 2000 respectively, and then the default constructor will print 4. (System.out.println("4");) The date object will be stored at index 0 in dateList array.
dateList[1] = new Date(2020);
This will initialize Date using constructor taking year value, which itself calls the constructor taking day, month and year passing values 1, 1 and year respectively, and then the constructor will print 3. The date object will be stored at index 1 in dateList array.
dateList[2] = new Date(3, 2020);
This will initialize Date using constructor taking month and year values, which itself calls the constructor taking day, month and year passing values 1, month and year respectively, and then the constructor will print 2. The date object will be stored at index 2 in dateList array.
dateList[3] = new Date(25, 3, 2020);
This will initialize Date using constructor taking day, month and year values, and nothing will be printed. The date object will be stored at index 3 in dateList array.
dateList[4] = new Date(35, 35, 2050);
This will initialize Date using constructor taking day, month and year values, and nothing will be printed.
Note: Since the day and month values are invalid (35 is not a valid day or month), the day and month values will be initialized to 1.
The date object will be stored at index 4 in dateList array.
dateList[5] = new Date(dateList[3]);
This will initialize Date using constructor taking another Date object, which itself calls the constructor taking day, month and year passing values day, month and year values of the passed Date object respectively (here day=25, month=3 and year=2020), and then the constructor will print 1. The date object will be stored at index 5 in dateList array.
System.out.printf("%d\n", dateList[0].getTracker());
This will print 6 as we have created 6 Date objects and assigned to array indices 0 to 5. The rest of the array elements will be null.
for (int i = 0; i < dateList.length; i++)
System.out.printf("%d\n", dateList[i]);
This will go through each element on the dateList array, and try to print each object as an integer (%d is for denoting integer). Since Date objects are not integers, IllegalFormatConversionException exception will be thrown. If we need to print the Date objects as Strings (invoking toString()), we need to use %s instead of %d. So assuming we use %s instead. Then the loop will print:
01-01=2000
01-01=2020
03-01=2020
03-25=2020
01-01=2050
03-25=2020
null
null
null
null
null
null
Here 01-01=2000 is in the format month-day=year as defined by the toString method of Date class.
So the system will output this if we don’t change %d to %s:
0
4
3
2
1
6
Exception in thread "main" java.util.IllegalFormatConversionException: d !=Date
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Q1.main(Q1.java:19)
And the system will output this if we change %d to %s:
0
4
3
2
1
6
01-01=2000
01-01=2020
03-01=2020
03-25=2020
01-01=2050
03-25=2020
null
null
null
null
null
null
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Need help figuring out what would be the all 12 days output public class Date {...
public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...
I'm having trouble with the daysSince() Method in bold at the end of the program public class Date { static final int [] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; private int day, month, year; public Date() { day = 1; month = 1; year = 2000; } public Date(int d, int m, int y) { day = d; month = m; year = y; } public Date (String s) { setDate(s);...
I need to create a Java class file and a client file that does the following: Ask user to enter Today’s date in the form (month day year): 2 28 2018 Ask user to enter Birthday in the form (month day year): 11 30 1990 Output the following: The date they were born in the form (year/month/day). The day of the week they were born (Sunday – Saturday). The number of days between their birthday and today’s...
JAVA LANGUAGE create an empty Java file called Date build it to find any syntax errors and eliminate them identify each member of this class and write your answer in a comment There are four different types of class member: constant, instance variable, constructor, and method identify a method as class method if it is static /* * Java program: Date.java * * Define the class Date */ import java.time.LocalDateTime; public class Date { private static final int daysEachYear =...
My values are not storing into my object and I keep returning zero. please explain my mistake thanks public class CalanderDate { int year = 0 ; int day = 0; int month= 0; public static void main(String[] args) { CalanderDate date; date = new CalanderDate( 1, 10 ,1999); System.out.print(date); } public CalanderDate(int month, int day , int year ) { if (month < 1){ month = 1; } if (month> 12){ month = 12; }...
Problem Description: Expand on 2 by implementing Insertion Sort and displaying the dates sorted after adding one to all dates. Run your code using the attached input files outputlong.txtoutputlab3.txt. Please note that your the dates that are sorted will be the ones after 1 day has been added to them. Sample Output: (green is user input) Run #2 using sample file outputlab3.txt Enter name of file to import or the word null to bypass: outputlab3.txt How many assessments in this...
Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2019. Derive the class extDateType so that the date can be printed in either form. Add a member variable to the class extDateType so that the month can also be stored in string form. Add a member function to output the month in the string format, followed...
this needs to be in Java:
use a comparator class which
is passed into the sort method that is available on the
ArrayList.
import java.util.Scanner;
public class Assign1{
public static
void main(String[] args){
Scanner reader = new Scanner (System.in);
MyDate todayDate = new MyDate();
int choice = 0;
Library library = new Library();
while (choice != 6){
displayMainMenu();
if (reader.hasNextInt()){
choice = reader.nextInt();
switch(choice){
case 1:
library.inputResource(reader, todayDate);
break;
case 2:
System.out.println(library.resourcesOverDue(todayDate));
break;
case 3:
System.out.println(library.toString());
break;
case 4:
library.deleteResource(reader,...
(IN JAVA ) Given the class Date that can prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2005. Please do the following: Derive the class ExtDate so that the date can be printed either form. Add a data member to the class ExtDate so that the month can also be stored in string form. Add a method to output the month in the string format followed...
C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep track of a date, but it has very limited operations. Redefine the class dateType so that it can perform the following operations on a date, in addition to the operations already defined: Set the month. Set the day. Set the year. Return the month. Return the day. Return the year. Test whether the year is a leap year. Return the number of days in...