The day number for a year is the number of days since December 31. For example,
ID: 3749099 • Letter: T
Question
The day number for a year is the number of days since December 31. For example, the day number for January 1 is 1, the day number for February 1 is 32 and the day number for December 31 is 365 1. Complete the table below by calculating the values for month and day for each of the day numbers While you are doing this, develop an algorithm. In other words, think about how you would get a computer to do it instead of you Month Day Day Number 32 57 60 79 91 157 213 214 277 365 12 31 2. Write a program called DayFunctions that includes the methods below. It should have a main that lets you call these two methods with values input from the userExplanation / Answer
1) Complete table
2)DayFunction java code:
private static int getDayNumber(int month,int day){
int ans=0;
for(int i=1;i<month;i++){
ans=ans+noOfDaysInMonths[i-1];
}
ans=ans+day;
return ans;
}
private static String getMonthDay(int dayNumber){
int month=0;
while(dayNumber>0){
dayNumber=dayNumber-noOfDaysInMonths[month];
month++;
}
int day = dayNumber+noOfDaysInMonths[month-1];
return String.valueOf(month)+"/"+String.valueOf(day);
}
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
private static int noOfDaysInMonths[] = {31,28,31,30,31,30,31,31,30,31,30,31};
public static void main(String[] args) {
//input
Scanner in = new Scanner(System.in);
System.out.println("Enter month:");
int month =in.nextInt();
System.out.println("Enter day:");
int day =in.nextInt();
int dayNumber = getDayNumber(month,day);
System.out.println("The Day number is "+String.valueOf(dayNumber));
System.out.println("Enter day number:");
int dayNum =in.nextInt();
String date = getMonthDay(dayNum);
System.out.println("The Day number is for date "+date);
}
private static int getDayNumber(int month,int day){
int ans=0;
for(int i=1;i<month;i++){
ans=ans+noOfDaysInMonths[i-1];
}
ans=ans+day;
return ans;
}
private static String getMonthDay(int dayNumber){
int month=0;
while(dayNumber>0){
dayNumber=dayNumber-noOfDaysInMonths[month];
month++;
}
int day = dayNumber+noOfDaysInMonths[month-1];
return String.valueOf(month)+"/"+String.valueOf(day);
}
}
3)Output from above program:
Month Day Day number 1 1 1 2 1 32 2 26 57 3 1 60 3 20 79 4 1 91 6 6 157 8 1 213 8 2 214 10 4 277 12 31 365Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.