NOTE This task is a direct continuation of the previous task

NOTE: This task is a direct continuation of the previous task. 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.

Previously done Task:

import java.util.Scanner;

public class MonthDays
{

public static int yearDays(int year)
{
      int result = 0;
      for (int i = 0; i < year; i++)
      {
          if (year > 0)
          {
          if (year % 100 == 0)
              {
                  if (year % 400 == 0)
                  {
                      result = 366;
                      return result;
                  }
                  else
                  {
                      result = 365;
                      return result;
                  }
              }
          else
          {
              if (year % 4 == 0)
              {
                  result = 366;
                  return result;
              }
              else
              {
                  result = 365;
                  return result;
              }
          }
          }
      }
      return result;
}
//done^
public static int monthDays(int year, int month)
{
      int result2 = 0;
    
          if (year > 0 )
          {
              if (month > 1 && month < 12)
              {
                      if (month == 2 )
                    // ^ when year has 366 days return results2
                      {
                          if (yearDays(year) == 366)
                          {
                              result2 = 29;
                          }
                          else
                          {
                              result2 = 28;
                        
                          }
                      }
                  }
                  else if (month == 1 || month == 3 || month == 5 || month == 7 ||
                          month == 8 || month == 10 || month == 12)
                  {
                      result2 = 31;
                    
                  }
                  else
                  {
                      result2 = 30;
                    
                  }
            
              }
         
      return result2;
}


public static int userInteger(String message)
{
    Scanner in = new Scanner(System.in);
    int result;
    int result2;
    while (true)
    {
      System.out.printf(message);
      String s = in.next();
    
      if (s.equals(\"q\"))
      {
        System.out.printf(\"Exiting...\ \");
        System.exit(0);
      }
    
      try
      {
        result2 = Integer.parseInt(s);
        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);
    }
}
}

File DayOfWeek.java:

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 called daysPassed, 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 yearDays function 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.

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

This is an example run of the complete program:

Solution

Hello Dude,The code you provided inside monthDays() method of MonthDays.java is having some logical mistakes.But anyway I cleared that to get the exact output according to your requirement.Thank you

_______________________________

DayOfWeek.java

import java.util.Scanner;

public class DayOfWeek
{
  
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 >= 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 days have passed from 12/31/999 to %d/%d/%d.\ \",
result, month, day, year);
  
String day_name = dayOfWeek(year, month, day);
System.out.printf(\"%d/%d/%d is a %s.\ \ \", month, day, year, day_name);
}
}


private static String dayOfWeek(int year, int month, int day) {
   int result=daysPassed(year,month,day);
   int dayNo=result%7;
   if(dayNo==0)
       return \"Tuesday\";
   else if(dayNo==1)
       return \"Wednesday\";
   else if(dayNo==2)
       return \"Thursday\";
   else if(dayNo==3)
       return \"Friday\";
   else if(dayNo==4)
       return \"Saturday\";
   else if(dayNo==5)
       return \"Sunday\";
   else if(dayNo==7)
       return \"monday\";
   return null;
}

public static int yearDays(int year)
{
int result = 0;
for (int i = 0; i < year; i++)
{
if (year > 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
result = 366;
return result;
}
else
{
result = 365;
return result;
}
}
else
{
if (year % 4 == 0)
{
result = 366;
return result;
}
else
{
result = 365;
return result;
}
}
}
}
return result;
}

public static int monthDays(int year, int month)
{
int result2 = 0;
  
if (year > 0 )
{
if (month >= 1 && month <= 12)
{
if (month == 2 )
// ^ when year has 366 days return results2
{
if (yearDays(year) == 366)
{
result2 = 29;
}
else
{
result2 = 28;
  
}
}
else if (month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
{
result2 = 31;
  
}
else
{
result2 = 30;
  
}
}
  
  
}

return result2;
}

private static int daysPassed(int year, int month, int day) {
   int total_years_days=0,total_months_days=0;
   for(int i=1000;i<year;i++)
   {
       total_years_days+=yearDays(i);
   }
   for(int i=1;i<month;i++)
   {
  
       total_months_days+=monthDays(year,i);
   }
  
return total_months_days+total_years_days+day;
}
}

_______________________________________

Output:

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

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

Enter a year (must be >= 1000): 1776
Enter a month(must be between 1 and 12): 7
Enter a day: 4
283614 days have passed from 12/31/999 to 7/4/1776.
7/4/1776 is a Thursday.

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

_________________Thank You

NOTE: This task is a direct continuation of the previous task. It is strongly recommended that you reuse the solution from your previous task here, by copy-past
NOTE: This task is a direct continuation of the previous task. It is strongly recommended that you reuse the solution from your previous task here, by copy-past
NOTE: This task is a direct continuation of the previous task. It is strongly recommended that you reuse the solution from your previous task here, by copy-past
NOTE: This task is a direct continuation of the previous task. It is strongly recommended that you reuse the solution from your previous task here, by copy-past
NOTE: This task is a direct continuation of the previous task. It is strongly recommended that you reuse the solution from your previous task here, by copy-past
NOTE: This task is a direct continuation of the previous task. It is strongly recommended that you reuse the solution from your previous task here, by copy-past
NOTE: This task is a direct continuation of the previous task. It is strongly recommended that you reuse the solution from your previous task here, by copy-past

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site