An ordinal date consists of a year and a day within it, both of which are integers. The year can be any year in the Gregorian calendar while the day within the year ranges from one, which represents January 1, through to 365 (or 366 if the year is a leap year) which represents December 31.
Ordinal dates are convenient when computing differences between dates that are a specific number of days (rather than months). For example, ordinal dates can be used to easily determine whether a customer is within a 90 day return period, the sell-by date for a food-product based on its production date, and the due date for a baby.
Write a function named ordinalDate that takes three integers as parameters. These parameters will be a day, month and year respectively. The function should return the day within the year for that date as its only result.
Outside of your functions, write code to read a day, month and year from the user and displays the day within the year for that date. You should not use any other class material beyond what we covered to date.
The java code is as follows:-
import java.util.*;
public class Main
{
static int days
[]={31,28,31,30,31,30,31,31,30,31,30,31};
static int ordinalDate(int d, int m, int y)
{
if(m>2 && y%4==0
&& (y%100!=0 || y%400==0))
++d;
while(--m>0)
d=d+days[m-1];
return d;
}
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int day=0,month,year,invalid=0;
int days
[]={31,28,31,30,31,30,31,31,30,31,30,31};
while(day!=-1)
{
System.out.print("Enter day(-1 to
exit): ");
day=sc.nextInt();
if(day==-1)
break;
System.out.print("Enter month:
");
month=sc.nextInt();
System.out.print("Enter year:
");
year=sc.nextInt();
if(month>12) //invalid
month
{
System.out.println("Invalid month
entered!\n");
invalid=1;
}
if((year%4==0 &&
day>29)||(year%4!=0 && day>days[month-1])) //invalid
day
{
System.out.println("Invalid day
entered!\n");
invalid=1;
}
if(invalid==0) //if date is
valid
System.out.println(ordinalDate(day,month,year)+"\n");
invalid=0;
}
}
}
The output of the above code is as follows:-
Enter day(-1 to exit): 15
Enter month: 2
Enter year: 2020
46
Enter day(-1 to exit): 16
Enter month: 13
Enter year: 2020
Invalid month entered!
Enter day(-1 to exit): 32
Enter month: 3
Enter year: 2020
Invalid day entered!
Enter day(-1 to exit): -1
The screenshot of the above code is as follows:-
An ordinal date consists of a year and a day within it, both of which are...
A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are: 1) The year must be divisible by 4 2) If the year is a century year...
In Python 3 please 3.28 LAB: Leap year A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are: 1) The year must be divisible by 4...
3. Using no date-specific Java libraries, write a program that computes the number of days between any two dates (inclusive). You must take into account leap years. The rule for computing a leap year is as follows: If the year is divisible by 4 it is a Leap Year ... Unless the year is divisible by 100, then it is _not_ a Leap Year ... Unless the year is divisible by 400, then it _is_ a Leap Year. During a...
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 )...
Extend this date validation program, so that it checks for valid leap years (in which the month February has 29, instead of 28, days). A leap year is any year that is divisible by 4 but not divisible by 100, unless it is also divisible by 400. Do not allow February to have 29 days on years that are not leap years. **Please view both photos for the entire code and post new code with the leap year modification. Thank...
A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are: 1) The year must be divisible by 4 2) If the year is a century year...
the instructions are: A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are:1) The year must be divisible by 42) If the year is a century year...
please follow format of example output, do in python A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are: 1) The year must be divisible by...
PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program that reads a values for hour and minute from the input test file timeData.txt . The data read for valid hour and valid minute entries. Assume you are working with a 24 hour time format. Your program should use functions called validateHour and validateMinutes. Below is a sample of the format to use for your output file timeValidation.txt : ...
JAVA programing Question 1 (Date Class): 5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...