The Artima Developer Community
Sponsored Link

Java Answers Forum
Is this task hard for a beginner to accomplish in 1 day?

3 replies on 1 page. Most recent reply: Mar 13, 2006 3:24 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Julien Barrett

Posts: 2
Nickname: julian15
Registered: Mar, 2006

Is this task hard for a beginner to accomplish in 1 day? Posted: Mar 13, 2006 1:26 AM
Reply to this message Reply
Advertisement
The Task
Write a Java program called DaysApart, which will ask for two dates from the user, and print out the number of days between the two dates. The range of dates that the program must accept are from 1900/01/01 to 2099/12/31.

If you attempt the basic version of the assignment, then your program only needs to calculate the number of days between two dates in the same year. If you attempt the advanced version of the assignment, then your program needs to calculate the number of days between any two dates in the range 1900/01/01 to 2099/12/31 inclusive.

Specific Subtasks
Your program must contain a method called isLeap(), which takes a year integer parameter, and returns a boolean result which is true if the year is a leap year, or returns false if the year is not a leap year.

Your program must contain a method called isValidDate(), which takes three integer parameters representing the date (year, month, day), and returns true if the date is a valid date, or false if the date is not valid.

A date is an invalid date if it does not represent a date within the range 1900/01/01 to 2099/12/31 inclusive, or if it does not represent a valid date within the given year (e.g. 2006/45/-1 or 2006/2/30).

Your program must contain a method called dayNumber(), which takes three integer parameters representing the date (year, month, day), and returns the number of that day within the given year. For example, the 1st of January is always day 1, and the 31st of December is day number 365 in normal years, or day number 366 in leap years. If the input date is invalid, the method must return the number -1 to indicate that the date is invalid.

Your program must contain a method called daysApart(). This method takes six integer parameters representing two dates, and returns the number of days between the two dates as a positive number. If either of the input dates are invalid, the method must return the number -1 to indicate that one of the dates is invalid.

If you are only attempting the basic version of this assignment, then the method must also return -1 if the dates are from different years.

Your main() method is the only method which is allowed to read input from the user, and the only method which is allowed to print information out to the user.
Each time the main() method reads in a date from the user, it must check the date. If the input date is invalid, the main() method must print out the message "That date is invalid!" and stop.

---------------------------------------

How hard is it for a begginer to do all of this? Im trying my best to get bonus marks in my computer science class at high school.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Is this task hard for a beginner to accomplish in 1 day? Posted: Mar 13, 2006 2:24 AM
Reply to this message Reply
It's not really complicated - if you know how to create methods in Java and set parameters and return types.

The text describes more or less every step you have to do inside the methods.

One hint: Crate an int-Array with 12 fields, every field contains the number of days of each month. This will easy a lot of the operations.
Note: When accessing this array, use monthNr-1 as index.

Julien Barrett

Posts: 2
Nickname: julian15
Registered: Mar, 2006

Re: Is this task hard for a beginner to accomplish in 1 day? Posted: Mar 13, 2006 2:42 AM
Reply to this message Reply
I guess its time to learn Arrays for the next hours, If I get this done in 20 hours from now, god knows how this will mean to me.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Is this task hard for a beginner to accomplish in 1 day? Posted: Mar 13, 2006 3:24 AM
Reply to this message Reply
Create your array this way:
int[] nDays = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


Then you can access it like this:

int test = nDays[5];

this would return 30, the number of days of June.

You can use this for the date validation method and to generate "day nr" of a given date.

Flat View: This topic has 3 replies on 1 page
Topic: Exist in Java? Previous Topic   Next Topic Topic: dice roller

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use