Task 7 20 pts NOTE This task is a direct continuation of the

Task 7 (20 pts.)

NOTE: This task is a direct continuation of the previous task (Previous task solution is shown below the instruction)

It is strongly recommended that you reuse the solution from your previous task here, by copy-pasting the functions that you defined there, and calling those functions as indicated by the hints below. This task is an exercise in reusing functions that you wrote before. If you do not reuse those functions, you will end up writing significantly more code.

File DayOfWeek.java contains an incomplete program, that takes as input from the user a date and outputs two things: the number of days that have passed from 12/31/999 up to the specified date, and the day of the week for that specified date. Complete that program, by defining two functions. The first function is calleddaysPassed, and should satisfy the following specs:

Function daysPassed takes three arguments, called year, month, day. You can assume that year > 0, that month is between 1 and 12, and that dayis between 1 and the number of days for that month of that year. We will only test your code with such cases.

The function returns the number of days that have passed from 12/31/999 to the specified date.

Hint: to compute that, you may find it useful to use your solutions from the previous task. Otherwise you will need to write significantly more lines of code. In more detail:

Do a for-loop from 1000 up to (and not including) the specified year, adding up the days of each year in the loop. You may find it useful to call your yearDaysfunction from the previous task (to be able to call your yearDays function, you need to copy-paste that function from your MonthDays.java file to your DayOfWeek.java file).

Then, do another for-loop, from 1 up to (and not including) the specified month, adding up the days of each month in the loop. You may find it useful to call your monthDays function from Task 6 (to be able to call your monthDays function, you need to copy-paste that function from your MonthDays.java file to your DayOfWeek.java file).

Finally, add to your total the specified day of the month.

The second function is called dayOfWeek, and should satisfy the following specs:

Function dayOfWeek takes three arguments, called year, month, day. For these arguments, you can make the same assumptions that are stated for the arguments of the days_passed function.

The function returns the day of the week (e.g, Sunday, Tuesday, and so on) that corresponds to the specified date.

Hint: the answer depends only on the remainder of dividing by 7 the result of daysPassed(year, month, day). For example, if the remainder is 0 the function should return \"Tuesday\", if the remainder is 1 the function should return \"Wednesday\", and so on.

import java.util.Scanner;

public class MonthDays
{
public static int yearDays(int year)
{
if ((year % 4 == 0) && (year % 100 != 0))
{
int result = 366;
return result;
}
  
if (year % 400 == 0)
{
int result = 366;
return result;
}
  
else
{
int result = 365;
return result;
}
}
public static int monthDays(int year, int month)
{
  
if ((month == 1) ||(month == 3) || (month == 5)|| (month == 7) || (month == 8) || (month == 10) || (month == 12))
{
int result2 = 31;
return result2;
}
  
if ((month == 4) ||(month == 6) || (month == 9)|| (month == 11))
{
int result2 = 30;
return result2;
}
if ((month == 2) || (year == 365))
{
int result2 = 28;
return result2;
}
else
{
int result2 = 29;
return result2;
}
}
public static int userInteger(String message)
{
Scanner in = new Scanner(System.in);
int result;
while (true)
{
System.out.printf(message);
String s = in.next();
if (s.equals(\"q\"))
{
System.out.printf(\"Exiting...\ \");
System.exit(0);
}
  
try
{
result = Integer.parseInt(s);
}
catch (Exception e)
{
System.out.printf(\"%s is not a valid number, try again.\ \ \", s);
continue;
}
  
if (result <= 0)
{
System.out.printf(\"%s is <= 0, try again.\ \ \", s);
continue;
}
return result;
}
}
  
  
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
  
while (true)
{
int year = userInteger(\"Enter a year (must be > 0): \");
int month = userInteger(\"Enter a month (must be between 1 and 12): \");
if (month > 12)
{
System.out.printf(\"Invalid month.\ \ \");
continue;
}

int result = yearDays(year);
int result2 = monthDays(year, month);

System.out.printf(\"Year %d has %d days.\ \", year, result);
System.out.printf(\"Month %d, %d has %d days.\ \ \", month, year, result2);
}
}
}
  

IMPORTANT: You are NOT allowed to modify in any way the main function..

This is an example run of the complete program:

Solution

Please follow the code and comments for description :

CODE :

import java.util.Scanner; // required imports

public class DayOfWeek { // class to run the code

    public static int userInteger(String message) { // method to get the date entered by the user
        Scanner in = new Scanner(System.in);
        int result;
        while (true) { // checking for the date entered
            System.out.printf(message); // printing the message
            String s = in.next();
            if (s.equals(\"q\")) {
                System.out.printf(\"Exiting...\ \");
                System.exit(0); // exiting the code
            }

            try {
                result = Integer.parseInt(s); // getting the result
            } catch (Exception e) {
                System.out.printf(\"%s is not a valid number, try again.\ \ \", s);
                continue;
            }

            if (result <= 0) {
                System.out.printf(\"%s is <= 0, try again.\ \ \", s);
                continue;
            }
            return result; // returning the result
        }
    }

    public static int yearDays(int year) { // getting the year days for a given year
        int result = 0;
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {             // checking for the leap year condition
            result = 366;          
        } else {
            result = 365;          
        }
        return result;
    }

    public static int monthDays(int year, int month) { // returning the days for a given month

        if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) {// checking for the mont count
            int result2 = 31;
            return result2;
        }

        if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
            int result2 = 30;
            return result2;
        }
        if ((month == 2) && (!((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))) {
            int result2 = 28;
            return result2;
        } else {
            int result2 = 29;
            return result2;
        }
    }

    public static int daysPassed(int year, int month, int day) { // calculating the days that have passed from a given date
        int totalYrDays = 0, totalDays = 0; // required initialisations
        for (int i = 1000; i < year; i++) { // iterating over the loop from 100 to entered year
            int yrDays = yearDays(i);
            totalYrDays = totalYrDays + yrDays; // calculating the totalDays
        }
      
        int totalMnthDays = 0;
        for (int j = 1; j < month; j++) { // iterating over the month to get the days
            totalMnthDays = totalMnthDays + monthDays(year, j);       // calculating the totalDays
        }
      
        totalDays = totalYrDays + totalMnthDays + day; // summing the total
        return totalDays; // returning the total days
    }

    public static String dayOfWeek(int year, int month, int day) { // method to get the day of the date
        String dayOfTheWeek = null; // required initialisations
        int totalDiffDays = daysPassed(year, month, day); // getting the total difference days
        int rem = totalDiffDays % 7; // getting the remainder
      
        if (rem == 0) { // checking based on the remainder
            dayOfTheWeek = \"Tuesday\";
        } else if (rem == 1) {
            dayOfTheWeek = \"Wednesday\";
        } else if (rem == 2) {
            dayOfTheWeek = \"Thursday\";
        } else if (rem == 3) {
            dayOfTheWeek = \"Friday\";
        } else if (rem == 4) {
            dayOfTheWeek = \"Saturday\";
        } else if (rem == 5) {
            dayOfTheWeek = \"Sunday\";
        } else {
            dayOfTheWeek = \"Monday\";
        }
        return dayOfTheWeek; // return the day of the week
    }

    public static void main(String[] args) { // driver method
        Scanner in = new Scanner(System.in);

        while (true) { // checking for the condition
            int year = userInteger(\"Enter a year (must be >= 1000): \");
            if (year < 1000) {
                System.out.printf(\"Invalid year.\ \ \");
                continue;
            }

            int month = userInteger(\"Enter a month(must be between 1 and 12): \");
            if (month > 12) {
                System.out.printf(\"Invalid month.\ \ \");
                continue;
            }

            int day = userInteger(\"Enter a day: \");

            int result = daysPassed(year, month, day);
            System.out.printf(\"%d day(s) have passed from 12/31/999 to %d/%d/%d.\ \",
                    result, month, day, year); // print the result

            String day_name = dayOfWeek(year, month, day);
            System.out.printf(\"%d/%d/%d is a %s.\ \ \", month, day, year, day_name); // print the result
        }
    }
}


OUTPUT :

Enter a year (must be >= 1000): 2015
Enter a month(must be between 1 and 12): 8
Enter a day: 4
370937 day(s) have passed from 12/31/999 to 8/4/2015.
8/4/2015 is a Tuesday.

Enter a year (must be >= 1000): 1000
Enter a month(must be between 1 and 12): 1
Enter a day: 1
1 day(s) have passed from 12/31/999 to 1/1/1000.
1/1/1000 is a Wednesday.

Enter a year (must be >= 1000): q
Exiting...


Hope this is helpful

Task 7 (20 pts.) NOTE: This task is a direct continuation of the previous task (Previous task solution is shown below the instruction) It is strongly recommende
Task 7 (20 pts.) NOTE: This task is a direct continuation of the previous task (Previous task solution is shown below the instruction) It is strongly recommende
Task 7 (20 pts.) NOTE: This task is a direct continuation of the previous task (Previous task solution is shown below the instruction) It is strongly recommende
Task 7 (20 pts.) NOTE: This task is a direct continuation of the previous task (Previous task solution is shown below the instruction) It is strongly recommende
Task 7 (20 pts.) NOTE: This task is a direct continuation of the previous task (Previous task solution is shown below the instruction) It is strongly recommende

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site