Lab 04, Esc101, 2004-05 Semester-II

Solve the following problems


Problem 1

Write a program which takes date like 17-9-1921 as String format and finds day of the week for this date. For example, output of your program on the above input should be: The date 17 9 1921 is a Saturday.

The basic idea of this program is to remember a standard date day pair (for example: Jan 1, 2003 was a Wednesday), and compute the number of days, between the standard date and the given date, modulo 7. For instance if the date is later than Jan 1, 2003 and the above diff mod 7 is 2 then the given date is Friday. If the date is before Jan 1, 2003 and the above diff mod 7 is 2 then the given date is Monday.

Also remember that a year is a leap year if it is a multiple of 4. An exception to the above is when the year is also a multiple of 100, in that case the year is a leap year iff it is a multiple of 400. For example, 1900 was not a leap year but 2000 was a leap year.

Hints:

First write a method which will calculate the difference between two dates which are in the same month, in the same year. Then write a method which will calculate the difference between two dates which are in the same year, but not necessarily in the same month. Then write a method which will calculate the difference between two dates which may be in different years. Of course, each method can use the previous method(s).


Problem 2

a) Write a method starsInLine(int n) which will take an integer 'n' as argument and prints 'n' stars in a single line:
For instance: starsInLine(5) should print

*****

b) Write a second method called pattern(int m) which will take an integer 'm' as argument, and print the following pattern:

*
**
***
...
***...** (m stars)

You may use "starsInLine" while writing "pattern".

c) Bonus: can you write "pattern" without using "starsInLine" ?


Problem 3

Given a decimal number as integer convert it into binary number. For e.g. given 27 should print 11011.


Bhaskaran Raman
Last modified: Sat Jan 22 17:14:08 IST 2005