Java Lab #4: Loops

  1. Write a program that accepts a date and a number of days and that computes the date the given number of days in the future. For example a typical run of the program might be

              Start: 2013-09-10
              Days : 2
    
              2013-09-10 + 2 days = 2013-09-12
            

    Your program should be able to handle any number of days (including zero) but negative days are an error. Your program should detect that error condition and display a message. For example

              Start: 2013-09-10
              Days : -1
    
              Error: Only non-negative day counts are allowed!
            

    For this lab you don't need to worry about validating the date input.

    Construct your program by combining code from several of your earlier programs. In particular use the code from Homework #2 to split the date string "2013-09-12" (or whatever the user enters) into separate year, month, and day values. Use the code from Lab #3 to compute a date one day ahead. Put the Lab #3 code inside a loop that repeatedly advances the day the required number of times. Notice that to print the desired output you'll need to keep a copy of the original date as well.

    Ideally your code will handle all month boundaries and leap years appropriately. Your Lab #3 code should already be doing this. However, the main focus of this lab is the loop so don't worry too much about the details until you have the loop working right.

    Here is the pseudo code for this program

              <Read and validate the input>
              <Copy the start date for later use>
              <Set the current date to the start date>
              FOR <each day> LOOP
                <Advance the current date by one day>
              END
              <Produce the appropriate output>
            

    Building a program by piecing together parts of other programs is a very common activity and there are important features in Java (and in all programming languages) to help make it easier. We will explore those features soon but I want you to experience this manual process first so that you can better appreciate the more powerful features when we cover them. Please pay attention to style! Good formatting, good variable names, and a clear layout will go a long way at keeping your program manageable.

    Question: What is the date 10,000 days from today? Use your program to find out.