### CS171, Homework 1

• Preparation

• To do this project, you must obtain a copy of the prepared files with these commands:

 ``` mkdir ~/cs171/hw1 cp /home/cs171000/Handouts/hw1/*.java ~/cs171/hw1 ```

• Assignment

• Write a Java method called:

 ``` public static int findDayInDigit( int month, int day, int year ) ```

that receives as inputs a date that is after Jan 1, 2000 encoded using the parameter variables:

 month = the month (1 = January, 2 = February, etc) day = the day (1, 2, 3, etc) year = the year (2000, 2001, 2002, etc)

and returns the output value:

 0 if the day is a Sunday 1 if the day is a Monday 2 if the day is a Tuesday 3 if the day is a Wednesday            4 if the day is a Thursday 5 if the day is a Friday 6 if the day is a Saturday

• Information that you will need:

 You are given that 1/1/2000 is a Saturday

• Sketch of the Algorithm

• Here are the steps that will solve the problem:

 ``` 1. compute n = the number of days between the input date and 1/1/2000 a. first: add the number of days between input year and year 2000 b. then: add the number of days between input month and January of the input year c. finally: add the number of days between input day and the first day of the month. 2. n = n + 6 (because 1/1/2000 is a Saturday, and 6 represents Saturday) 3. return: n%7 ```

Examples:

• First, look at a simple case: for 1/3/2000, we get:

 ``` 1. n = 2; // # days between 1/3/2000 and 1/1/2000 = 1 2. n = n + 6; = 8 3. return: 8%7 = 1 ---> Monday ! ```

• For a general case, like 4/12/2005, the calculation goes a follows:

 ``` 1. Count #day in the years between 2000 and 2005: 2000: 366 (leap !) 2001: 365 2002: 365 2003: 365 2004: 366 (leap !) ---------------------------- 1827 days 2. Count #days in the months till month 4 (not including) in 2005 Jan: 31 Feb: 28 (Feb in 2005 is not leap) Mar: 31 ---------------------------- 90 3. Count #days until day 1 and day 12 in month 4: 11 (Because there are 0 days between 4/1 and 4/1 !!) 4. n = 1827+90+11 = 1928; // # days between 4/12/2005 and 1/1/2000 5. n = n + 6; // 6 = Saturday = 1934 6. return: 1934%7 = 2 ---> Tuesday ! ```

• Leap years

• Fact:

 Normal years have 365 days Leap years has an extra day (namely: Feb 29)

This is how you can tell if a year is leap:

 A year that is divisible by 400 is always a leap year E.g.: 2000 is a leap year A year that is divisible by 100 but not divisible by 400 is not a leap year E.g.: 1900 is not a leap year A year that is divisible by 4 but not divisible by 100 is a leap year E.g.: 2004 is a leap year All other years are not leap years.

• How to do your project

• The copy command that you issued to prepare this project would have copied a skeltal Java program file into you cs170/hw1 project directory. You change this program to do the assignment:

Complete the findDayInDigit( int month, int day, int year ) method in the skeletal program file.

How to run the program:

 To compile:   javac hw1.java To run:          java hw1 DATE

Sample outputs:

 ``` >> java hw1 1/1/2000 1/1/2000 is a Saturday >> java hw1 1/2/2000 1/2/2000 is a Sunday >> java hw1 8/1/2013 8/1/2013 is a Thursday >> java hw1 1/1/3000 1/1/3000 is a Wednesday ```

There is also the command cal on UNIX that shows the calendar of any year. For example:

 ``` >> cal 2000 >> cal 2010 ```

• Turnin

• After you have successfully completed and tested your program, you must turn your file in for grading.

Use the following command (from inside your cs171/hw1 project directory) to turn in your program:

 ``` cd ~/cs171/hw1 /home/cs171000/turnin hw1.java hw1 ```

Note:

 If you work on your laptop, you must first upload your program to to your Emory account and then use the above commands to turn in Read the next section to find out how to upload file to your Emory account

• How to upload a program file from your laptop to turn in

• Before you upload any file to Emory, go login to your Emory account and make a hw1 directory inside your cs171 directory:

 ``` mkdir ~/cs171/hw1 ```

(If you want to do this from your laptop, you must read this webpage first:

 How to tele-commute from home --- work on the MathCS lab machine from your dorm: click here

• How to upload a file to Emory:

• If you use a Mac, run this command in the directory that you have the file to copy the file name "file" to your cs171/hw1 directory on Emory:

 ``` scp file yourEmoryID@lab0z.mathcs.emory.edu:cs171/hw1 ```

 WinSCP: right click and save

Run the program after installing and connect lab0z.mathcs.emory.edu

In the left window, navigate to the directory on your PC that contains your file

In the right window, navigate to the directory on your Emory account that should contains your file (that would be /home/yourID/cs171/hw1 in this project)

Drag the file from the left window to the right window to upload.