Question

Julian day numbers (JDN) are used to provide a number for a day of the year...

Julian day numbers (JDN) are used to provide a number for a day of the year so that calculations can be performed relatively easily to determine the difference between two dates. The JDN for a day is the number of days that have elapsed between that day and a base date of January 1, 4713 BC. Use the following calculation: JDN = (1461 × (Y + 4800 + (M − 14) / 12)) / 4 + (367 × (M − 2 − 12 × ((M − 14) / 12))) / 12 − (3 × ((Y + 4900 + (M - 14) / 12) / 100)) / 4 + D – 32075 Once the above is calculated, input two dates, convert them to their JDNs, and take the difference of the JDNs. Write a C# program to find the difference between two dates using the method above. Error check dates- where day is an integer from 1 to 31, month goes from 1 to 12, and year goes from 1600 to 2100. Finally, ask the user to run the program again. A user-defined function must be used with the calculation. Do not use the C# DateTime object in this problem. Refer to the sample output below. Sample Run: Enter the first date (MM/DD/YYYY): 9 7 1980 Enter the second date (MM/DD/YYYY): 6 11 2012 Days between dates 09/07/1980 and 06/11/2012 is: 11600 Run Again (Y/N)? n

0 0
Add a comment Improve this question Transcribed image text
Answer #1

using System;
                  
public class Program
{
   public static void Main()
   {
           String runAgainFlag;
           do{
           // reads first date
Console.Write("Enter the first date (MM/DD/YYYY): ");
           String firstDateString;
           firstDateString = Console.ReadLine();
           // convert date into Julian Date Number using user defined method getJulinaDateNumber(String dateString)
           int jdnOfFirstDate = getJulianDateNumber(firstDateString);
  
           // reads second date
           Console.Write("Enter the second date (MM/DD/YYYY): ");
           String secondDateString;
           secondDateString = Console.ReadLine();
           // convert date into Julian Date Number using user defined method getJulinaDateNumber(String dateString)
           int jdnOfSecondDate = getJulianDateNumber(secondDateString);
  
           // print difference between the two dates
           int dateDifference;
           if(jdnOfSecondDate > jdnOfFirstDate){
               dateDifference = jdnOfSecondDate - jdnOfFirstDate;
           }else
               dateDifference = jdnOfFirstDate - jdnOfSecondDate;
              
           // print difference between the dates entered
Console.Write("Days between dates " + firstDateString + " and " + secondDateString + " is: " + dateDifference);
          
           Console.Write("Run Again (Y/N)");
runAgainFlag = Console.ReadLine();
       }while(runAgainFlag.Contains("Y") || runAgainFlag.Contains("y"));
   }
   public static int getJulianDateNumber(String dateString){
       String[] dateAttributes = new String[3];
      
       //split the date into months, days and years based on / or space
       //If date does not contains / or space then it can't be splitted and the variable dateAttributes remains empty
           if(dateString.Contains("/")){
               dateAttributes = dateString.Split('/');
           }else if (dateString.Contains(" ")){
               dateAttributes = dateString.Split(' ');
           }
           if(dateAttributes.Length != 3){
               Console.Write("Date is not in proper format");
               System.Environment.Exit(0);
           }
           int M = 0;
           int D = 0;
           int Y = 0;
      
       //If dateAttributes is empty the program throws IndexOutOfRangeException which is handled using try catch
       //In the catch block program prints a string and stops the execution
       try{
           M = int.Parse(dateAttributes[0]);
           D = int.Parse(dateAttributes[1]);
           Y = int.Parse(dateAttributes[2]);
       }catch(Exception){
           Console.Write("Date is not in proper format");
           System.Environment.Exit(0);
       }
      
       //check if all the variables month,day and year are in range specified using user defined method checkForRange(int value, int lowerLimit, int upperLimit)
       if(checkForRange(M,1,12) == false){
           Console.Write("Month should be in range 1 and 12");
           System.Environment.Exit(0);
       }
       if(checkForRange(D,1,31) == false){
           Console.Write("Day should be in range 1 and 31");
           System.Environment.Exit(0);
       }
       if(checkForRange(Y,1600,2100) == false){
           Console.Write("Year should be in range 1600 and 2100");
           System.Environment.Exit(0);
       }
      
       //formula to convert date into Julian date number is written directly in return statement
       //The formula is calculated and returned to the point where getJulianDateNumber is being called
       return (1461 * (Y + 4800 + (M - 14) / 12))/4 + (367 * (M - 2 - 12 * ((M - 14) / 12))) / 12 - (3 * ((Y + 4900 + (M - 14) / 12) / 100)) / 4 + D - 32075;
   }
  
   public static bool checkForRange(int value, int lowerLimit, int upperLimit){
       if(value >= lowerLimit && value <= upperLimit){
           return true;
       }else
           return false;
   }
  
   /*
   * The method getJulianDateNumber(String dateString) takes date string in the format MM/DD/YYYY or MM DD YYYY and converts the date string into Julian day number
   * If the string is not in the format MM/DD/YYYY or MM DD YYYY, the program prints an error and stops execution
   * The variables M,D and Y denote month, day and year respectively
   * If month is not in range 1 and 12, the program prints an error and stops execution
   * If day is not in range 1 and 31, the program prints an error and stops execution
   * If year is not in range 1600 and 2100, the program prints an error and stops execution
   */
  
   /*
   * The method checkForRange(int value, int lowerLimit, int upperLimit) checks if value is in range between lowerLimit and upperLimit
   * If the value is in range the method returns true or else it returns false
   */
}

Add a comment
Know the answer?
Add Answer to:
Julian day numbers (JDN) are used to provide a number for a day of the year...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi any help is appreciated! I am using C++. The Julian Day Number (JDN) is a...

    Hi any help is appreciated! I am using C++. The Julian Day Number (JDN) is a sequential count of days since the beginning of the Julian Period. Day number zero corresponds to 1 January 4713 BCE. As a result of calendar reform in the 16th Century, we will only be computing day numbers since 15 October 1582 (which was the first day of the modern Gregorian calendar, having day number 2299161). 1 January 2001 was day number 2451911. To compute...

  • I need this in Java, please! The ISO 8601 Standard date format for Information Interchange indicates...

    I need this in Java, please! The ISO 8601 Standard date format for Information Interchange indicates that a date be written as such: yyyy-MM-dd (eg. 2012-07-02, 1999-12-05, 1998 -01-27 ) where yyyy represents the four digit year MM represents a two digit numerical month dd represents a two digit numerical day Chinese date format is specified as: yyyy-M-d Macedonean date format is specified as: d.M.yyyy where yyyy represents the four digit year M represents a one or two digit numerical...

  • CODING IN JAVA Dates are printed in several common formats. Two of the more common formats...

    CODING IN JAVA Dates are printed in several common formats. Two of the more common formats are: 07/21/55 and July 21, 1955 Write a program that reads a date in the first format and prints that date in the second format. Prompt the user and accept user input for the date in MM/DD/YYYY format. Retrieve the month portion of the date entered and convert it to an integer. Do the same thing for day and year Create a string named...

  • C Programming Quesition (Structs in C): Write a C program that prompts the user for a...

    C Programming Quesition (Structs in C): Write a C program that prompts the user for a date (mm/dd/yyyy). The program should then take that date and use the formula on page 190 (see problem 2 in the textbook) to convert the date entered into a very large number representing a particular date. Here is the formula from Problem 2 in the textbook: A formula can be used to calculate the number of days between two dates. This is affected by...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    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 )...

  • Need help writing beginner C# program, I will make sure to provide feedback to whoever can...

    Need help writing beginner C# program, I will make sure to provide feedback to whoever can help me figure it out! No public or global variables should be used. You need to consider passing arguments between the methods according to the ways described in the lecture. i.e. all variables should be declared inside the methods and passed to other methods by value/ref/out as needed Description: We want to design a Date class to represent a date using three integer numbers...

  • An ordinal date consists of a year and a day within it, both of which are...

    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...

  • Please create a C++ class called myDate. It should have the following operations: myDate() – default...

    Please create a C++ class called myDate. It should have the following operations: myDate() – default constructor. This will set the date to May 10, 1959. myDate(int M, int D, int Y) – overloaded constructor. This will set the date to the values passed in through the parameter list represented by Month, Day and Year. void display() – display the date in the following format (May 11, 1959) Do NOT print a linefeed after the date. void incrDate(int N) –...

  • In Java please Project 14-1: Reservation Calculator Create an application that gets arrival and departure dates...

    In Java please Project 14-1: Reservation Calculator Create an application that gets arrival and departure dates for a reservation and calculates the total amount for the stay. Console Reservation Calculator Enter the arrival month (1-12): 5 Enter the arrival day (1-31): 16 Enter the arrival year: 2018 Enter the departure month (1-12): 5 Enter the departure day (1-31) : 18 Enter the departure year: 2018 Arrival Date: May 16, 2018 Departure Date: May 18, 2018 Price: $145.00 per night Total...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT