3 Replies - 497 Views - Last Post: 26 March 2012 - 11:04 AM Rate Topic: -----

#1 breemanson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-March 12

Do not know how to complete code. Almosted finished it.

Posted 26 March 2012 - 10:24 AM

import java.util.Scanner;

/**
 * Days determines the number of days between two dates.
 * @author X
 */
public class Days {
    public static void main(String[] args) {
        String start = "02/23/2011"; // replace with input
        String end = "04/15/2011";   // replace with input
        System.out.printf("Days between %s and %s are %d.\n", start, end,
                          daysBetweenDates(start, end));
    }

    /**
     * isLeapYear determines whether a year is a leap year or not
     * @param year (int) Years since recognized beginning of C.E.
     * @return (boolean) true if year divisible by 4 (but not 100)
     */
    public static boolean isLeapYear(int year) {
        System.out.println("in isLeapYear");
        return false;
    }

    /**
     * daysInMonth looks up the number of days in the specified month
     * @param month (int) a number between 1 and 12 indicating the desired month
     * @param year (int) the year is only meaningful for February (2)
     * @return (int) the last day of month/year (or 0 if invalid month provided)
     */
    public static int daysInMonth(int month, int year) {
        System.out.println("calculating days in a month");
        return 30;
    }

    /**
     * daysIntoYear translates a date (month, day, year) into a single number
     * representing the days since 12/31/(year-1).
     * @param month (int) 1-12, where 1 is January and 12 is December
     * @param day (int) 1-31, or actually up to daysInMonth
     * @param year (int) years since start of C.E.
     * @return (int) number of days between 12/31/(year-1) and (month)/(day)/(year)
     */
    public static int daysIntoYear(int month, int day, int year) {
        System.out.println("calculating days into year");
        return 1;
    }

    /**
     * daysBetweenDates calculates number of days between two dates.
     * @param start (String) starting date
     * @param end (String) ending date, presumably later than start
     * @return (int) days between start date and end date
     */
    public static int daysBetweenDates(String start, String end) {
        int days = 0;

        System.out.println("calculating daysBetweenDates");
        return days;
    }
}



Here is what the program is supposed to do:
1.The input data we need to get from user: two dates, both in numeric month/day/year format. We will use either Scanner or JOptionPane to get each date from a user and store them into two variables, say start and end.

2.We need to calculate the number of days between these two dates. Assuming the years are the same, this can be calculated by transforming each date into the number of days into that year (1/16/2012 → 16, 2/16/2012 → 47, etc.) and subtracting (47 – 16 = 31 days apart). Otherwise, calculate the number of days left to get to the end of the start year, the number of days in each year in between, and the number of days to get to the end date from the start of the end year.
a.We presumably have the dates as strings; we need to break each date into three numbers: month, day and year. One method is to build a Scanner around the String and change its delimiter from whitespace to “/”:Scanner scanDate1 = new Scanner(start);scanDate1.useDelimiter(“/”);startMonth = scanDate1.nextInt();startDay = scanDate1.nextInt();startYear = scanDate1.nextInt();

b.Assume you have methods for calculating daysIntoYear(month, day, year) and daysInYear(year), days between dates becomesif startYear and endYear are the same, daysIntoYear(endMonth, endDay, endYear) – daysIntoYear(startMonth, startDay, startYear)otherwise
i.days left in start year = daysInYear(startYear) – daysIntoYear(startMonth, startDay, startYear)

ii.for each year from startYear + 1 to endYear – 1, add daysInYear(year)

iii.add daysIntoYear(endMonth, endDay, endYear)



3.We need to be able to calculate daysIntoYear
a.for each earlier month, add the days in that month

b.add the day of the month


4.We need to be able to calculate daysInYearif leap year, then it is 366;otherwise, it is 365.

5.We need to know how many days are in each month. Let us use another int variable month to store the value of month. 1 means “January”, 2 means “February”…, 12 means “December”.1, 3, 5, 7, 8, 10, 12: 31 days4, 6, 9, 11: 30 days2: 28 or 29 days, depending on whether this February is in a leap year or not.

6.We need to know whether a year is a leap year or not.A year is a leap year if it is divisible by 4 but not divisible by 100.A year is also a leap year if it is divisible by 400.Any other year is not a leap year.For example, the year 2011 is not a leap year since it is not divisible by 4. The year 2100 is not a

Is This A Good Question/Topic? 0
  • +

Replies To: Do not know how to complete code. Almosted finished it.

#2 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: Do not know how to complete code. Almosted finished it.

Posted 26 March 2012 - 10:34 AM

We can't just hand out code. Please tell us exactly what's wrong and we will be able to help.
Was This Post Helpful? 0
  • +
  • -

#3 breemanson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-March 12

Re: Do not know how to complete code. Almosted finished it.

Posted 26 March 2012 - 10:38 AM

View PostThe_Programmer-, on 26 March 2012 - 10:34 AM, said:

We can't just hand out code. Please tell us exactly what's wrong and we will be able to help.

It does not tell the number of days between dates.
I do not know how to get the user's input of dates and calculated the number of days from the input.
Was This Post Helpful? 0
  • +
  • -

#4 eikkaj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 119
  • Joined: 20-December 08

Re: Do not know how to complete code. Almosted finished it.

Posted 26 March 2012 - 11:04 AM

Look into using the Scanner class. This post is rather helpful

http://forums.hotjoe...s/list/128.page
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1