Due Date: Wednesday, October 5, 2011 by 4pm Note: Assignments received after 4 and before 9:30pm will lose 1 mark per 15 minute interval. Assignments are graded out of 20 marks. Assignments submitted between 4 and 4:30 pm will be marked as late, but no marks will be deducted. After 4:30pm late assignments will lose 1 mark for every 15 minutes of lateness. For example, an assignment submitted at 4:38pm would lose 1 mark for lateness. An assignment submitted at 7:50 would lose 14 marks. No assignments will be accepted after 9:31pm, since they would already have lost all 20 marks. Material Covered input using JOptionPane and Scanner output using JOptionPane and System.out.println Strings parseInt and parseDouble Integer and double arithmetic Math methods
Notes: Name your files as follows: <LastName><FirstName>A1Q1.java and <LastName><FirstName>A1Q2.java. For example, LiJaneA1Q1.java is a valid name. If you wish to add a version number to your file, you may add it to the end of the file name. For example, SmithRobA2Q2V2.java is a valid name. Follow the programming standards to avoid losing marks. You must submit a Blanket Honesty Declaration by Wednesday, October 6. This honesty declaration applies to all assignments in COMP 1010. To submit the assignment you will upload your .java files using the course website. You will also copy and paste your output into a text box in the online assignment submission form. Do NOT manipulate your output in any way. We will demonstrate the assignment hand-in procedure in lecture. Capturi ng your output f or assi gnments i n COMP 1010 For the COMP 1010 programming questions, you will write a program for each question. Youll upload your .java file. Then you will also be asked to run your programs (often using values we provide for you) and then copy and paste the output of your program into a text box. How do you copy and paste this text? Just copy and paste the text from the DrJavas interactions panel!
COMP 1010 Fall 2011 Assignment 1 Page 2 of 6 Quick! What day of the week is birthday on this year? What day of the week does Christmas fall on? You can use John Horton Conways Doomsday rule to impress your friends and mentally calculate the day of the week a particular date falls on. The Doomsday rule is based on the observation that each month has an easy-to- remember date that falls on the same day of the week. In 2011, that day, or Doomsday, is Monday. That means that in 2011 the following days each month fall on a Monday: April 4 (4/4), June 6 (6/6), August 8 (8/8), October 10 (10/10) and December 12 (12/12) all fall on Doomsday. The last day of February (either 2/28 or 2/29) falls on Doomsday. Since the last day of February is Doomsday, March 7 is Doomsday. I work 9 5 at the 7-11: September 5 (9/5) and July 11 (7/11) are Doomsdays; e May 9 (5/9) and November 7 (11/7) are also Doomsdays. Boxing Day and Halloween also fall on Doomsday. Question 1Zellers Congruence Zellers Congruence is an algorithm that can be used to find the day of the week for any Gregorian calendar date. On the last page of this assignment is the code for finding a date using Zellers congruence. First, re-write this code using 1010 coding standards. Add comments to show you understand each line of the code. Second, revise this code so it outputs a date where this code outputs a date where: Sunday is 0 Monday is 1 Tuesday is 2 Wednesday is 3 Thursday is 4 Friday is 5 Saturday is 6. (Note: In the given code, Saturday is 0.) Hand-in: Save your file using the naming conventions described in the beginning of the assignment. Upload your file using the web-based hand-in tool. Also, submit one set of data using the following input: October 31, 2011 one date of your choosing.
Question 2Doomsday Rule
Doomsday is the day of the week that easy-to-remember dates fall on. In 2011, Doomsday is Monday. Knowing that Doomsday for 2011 is Monday makes it quite easy to figure out that this year Christmas (12/25) falls on a Sunday. Why? Because if you know that 12/12 is a Monday, then its possible to figure out that Dec 25 is 13 days later, which means it is two weeks less one day, which means it falls on a Sunday. Or you might recall that Boxing Day is always on Doomsday. Using Doomsday, can you figure out what day your birthday falls on this year? Write a program that will ask the user for a year in the 21 st century (2000 2099) and then calculate and output Doomsday for that year. COMP 1010 Fall 2011 Assignment 1 Page 3 of 6 Hint! Conways rule requires you to think of days of the weeks as numbers. Sunday is 0, Monday is 1, Tuesday is 2, Wednesday is 3, Thursday is 4, Friday is 5, Saturday is 6. To calculate Doomsday, you need to know the anchor day for each century. From 2000 2099 the anchor date is Tuesday (2). Since Tuesday is the anchor day this century, the formula to calculate Doomsday for 2000-2099 dates, using the Gregorian calendar is:
Where y is the last two digits of the century. Eg: In 2011, y is 11. In other words, find the first part of the equation and count that many days past the anchor day. For example, in 2011, (13) % 7 + anchor = Doomsday 6 + Tuesday (2) = Doomsday Doomsday = Tuesday + six days = Monday Heres an example run of the program.
For what year do you want Doomsday? 2011 Doomsday for 2011: 1 Where: Day 0 is Sunday + Day 1 is Monday Day 2 is Tuesday Day 3 is Wednesday Day 4 is Thursday Day 5 is Friday Day 6 is Saturday Programmed by 1010 instructors ***End of Programming*** > Input: Use JOptionPane.showInputDialog to prompt the user to input the year. As shown in the example above, echo back all input using System.out.println. Calculate: Now, use the formula given to calculate Doomsday.
Bonus: Once you have Doomsday working for the 21 st century, expand your program so the user can enter any date from 1900 2099. Hint: use an if statement. Output: Display the results of your calculation using both JOptionPane.showMessageDialog and System.out.print. Model your output on the example program above. Hand-in: Save your file using the naming conventions described in the beginning of the assignment. Upload your file using the web-based hand-in tool. Also, submit two sets of output using the following input: Year: 2015 Year: another year in the 21 st century (or in the 20 th century if you are going for a bonus mark.) Problem? How will you get the computer to count forward six days from Tuesday? Hint: Consider using the % operator.
COMP 1010 Fall 2011 Assignment 1 Page 4 of 6 Question 3Week Daze Game Write a little game to teach someone how to figure out dates using Conways Doomsday rule. Input: Ask the user for a year, then a month, then a day. Calculate: Calculate 1) Doomsday; then 2) the day of the week the date falls on. Re-use your your code from questions 1 and 2. Output: Output Doomsday. Offer some tips on using Doomsday to find a particular date. Second Input: Now, ask the user to use Doomsday to figure out the day of the week the date entered falls on. Output: Check to see if the user entered the correct date. Output a suitable message. Use a boolean expression to answer the question Correct? with either true or false Here is a sample run of a game. Your own game will obviously have different wording and layouts.
Let's play a game! Input year (eg: 2011): 2011 Input month (where Jan is 1, Feb is 2, March is 3...December is 12): 9 Input day of month: 8 In 2011 Doomsday is 1 Where: 0 is Sunday 1 is Monday 2 is Tuesday 3 is Wednesday 4 is Thursday 5 is Friday 6 is Saturday Now that you know Doomsday is 1 in 2011 can you figure out what day of the week 9-8 falls on? Enter: 0 for Sunday 1 for Monday 2 for Tuesday is 3 for Wednesday 4 for Thursday 5 for Friday 6 for Saturday. Hint: Remember 4/4, 6/6, 8/8, 10/10 and 12/12 all fall on Doomsday. Date: 8-9-2011 Your guess: 4 Actual day: 4 Correct? true Programmed by COMP 1010 Instructors **End of Programming**
Hand-in: Save your file using the naming conventions described in the beginning of the assignment. Upload your file using the web-based hand-in tool. Also, submit one set of data using the following input: July 1, 2012 input of your choosing COMP 1010 Fall 2011 Assignment 1 Page 5 of 6
Question 4Programming and People: Answer these questions in two sentences or less. Record your answers in the text boxes of the online assignment submission form. 1) Programming Politics a. What is open source code? How does it differ from other types of code? b. Give an example of open source code. Give an example of code that is not open source. 2) Therac-25 a. Summarize the Therac-25 disaster in a sentence or two. Give the url or published reference where you found this information. b. List one of the problems that contributed to the disaster. Give the url or published reference where you found this information. COMP 1010 Fall 2011 Assignment 1 Page 6 of 6
import javax.swing.*; public class A1_Zellers { public static void main (String [] args){ int day; int month; int year; String input; int yearOfCentury; //last two digits of four digit year (k in formula given) int century; //first two digits of four digit year (j in formula given) int weekDay; //day of week, output as single digit (h in formula given) input = JOptionPane.showInputDialog("Input year (eg: 2009)"); year =Integer.parseInt(input); System.out.print(input +"-"); input = JOptionPane.showInputDialog("Input month (where Jan is 1, Feb is 2, March is 3...December is 12."); month =Integer.parseInt(input); System.out.print(input +"-"); input = JOptionPane.showInputDialog("Input day of month"); day =Integer.parseInt(input); System.out.print(input);
//convert month if month is January or February if (month<3){ month = month + 12; year = year-1; } yearOfCentury = year%100; century = year/100; //use Zeller's congruence to find day look up Zellers congruence in Wikipedia if you want to learn more about this algorithm weekDay = (day + ((month+1)*26/10) + yearOfCentury + (yearOfCentury/4) +(century/4) + (5*century))%7;
System.out.println (" falls on day " + weekDay); System.out.println ("Where:\n0 is Saturday \n1 is Sunday\n2 is Monday\n3 is" + " Tuesday\n4 is Wednesday \n5 is Thursday\n6 is Friday"); System.out.println("Programmed by COMP 1010Instructors and [insert your name]"); System.out.println ("**End of Programming**");