Displaying a calendar A worked example From Liang, Example 4.6, Pp136-141.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Method Abstraction Java Programming. Key to developing software is to apply the concept of abstraction. Method abstraction is defined as separating the.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
CS 201 Functions Debzani Deb.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Introduction to Methods
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/30/2012 and 10/31/2012 -Code PrintCalendar.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
Unit Testing Part 2: Drivers and Stubs
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
Common Mistakes with Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
Chapter 6 Functions.
JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER
JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Starting Out with Java: From Control Structures through Objects
MON TUE WED THU
Chapter 5 Function Basics
1   1.テキストの入れ替え テキストを自由に入れ替えることができます。 フチなし全面印刷がおすすめです。 印刷のポイント.
JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER
Logo Calendar – January 2012 TO DO LIST 01/04/2012 Example TO DO LIST
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
ANNUAL CALENDAR HOLIDAYS JANUARY FEBRUARY MARCH APRIL MAY JUNE
HOLIDAYS ANNUAL CALENDAR JANUARY FEBRUARY MARCH APRIL MAY JUNE
January Sun Mon Tue Wed Thu Fri Sat
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
HOLIDAYS ANNUAL CALENDAR JANUARY FEBRUARY MARCH APRIL MAY JUNE
2008 Calendar.
January MON TUE WED THU FRI SAT SUN
Sun Mon Tue Wed Thu Fri Sat
January MON TUE WED THU FRI SAT SUN
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
Calendar
Calendar.
January MON TUE WED THU FRI SAT SUN
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
Sun Mon Tue Wed Thu Fri Sat
Additional control structures
January MON TUE WED THU FRI SAT SUN
S M T W F S M T W F
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
1 January 2018 Sun Mon Tue Wed Thu Fri Sat
2008 Calendar.
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Displaying a calendar A worked example From Liang, Example 4.6, Pp

The goal Enter full year: 2000 Enter month as a number from 1..12: 6 June, Sun Mon Tue Wed Thu Fri Sat

Conventions used: January == 1,...December==12 Sunday==0,..., Saturday==6 Year >= 1800

How to proceed? Divide and conquer Don’t start coding yet Identify sub-problems...assign them to methods Solve problems one at a time

Initial decomposition printCalendar (main) readInputprintMonth

Overview of PrintCalendar public class PrintCalendar{ // main method public static void main(String[] args){ // gets the year and month for which calendar is needed String yearString = JOptionPane.showInputDialog(null, “enter year”, “Example 4.6 input”, JOpotionPane.QUESTION_MESSAGE); int year = Integer.parseInt(yearString); String monthString=JOptionPane.showInputDialog(null, “enter month”, “Example 4.6 input”, JOpotionPane.QUESTION_MESSAGE); int month = Integer.parseInt(monthString); // now display the calendar for that month: use a method!! printMonth(year, month); } // other method definitions (e.g. printMonth) go here }

Method “stub” Don’t try to solve all problems at once Before fully implementing a method, feel free to use a “stub” instead. This will allow you to get the overall structure of your program correct // just a stub for now: this is a method in our class public static void printMonth(int year, int month) { System.out.println(“year “ + year, “ month” + month); }

decomposing printMonth static void printMonth(int year, int month) { // which day of week is first day of this month? int startDay = getStartDay(year, month); // how many days in the month? int numOfDaysInMonth = getNumOfDaysInMonth(year,month); // print the headings printMonthTitle(year, month); // print the month body printMonthBody(startDay, numOfDaysInMonth); } We write other methods to do these things…

printMonthTitle (easy) static void printMonthTitle(int y, int m) { System.out.println(“ “ + getMonthName(m) + “, “ + y); System.out.println(“ ”); System.out.println(“Sun Mon Tue Wed Thu Fri Sat”); } Again, this uses another method getMonthName which takes in a month number and returns the name of that month. Notice that printMonthTitle is not public ! (We don’t need other programs to use this method – its internal to the calendar program).

getMonthName public static String getMonthName(int month) { String monthName = null; switch(month) { case 1: monthName=“January”; break; case 2: monthName=“February”; break;.... } return monthName; } This is a simple method, which just converts a number (1..12) into a month name. Notice that this returns a String, and is not public!

static int getNumOfDaysInMonth(int y, int m) { if(month==1 || month==3 || month== ) return 31; if(month==4 || month==6 || month== ) return 30; if(month==2) { if(isLeapYear(y)) { return 29; } else { return 28; } } return 0; // If month is incorrect! } This is a simple method which takes a year and a month number as arguments and returns the number of days in that month. Notice that this returns an int, and is not public! This uses another method isLeapYear(y) which we have to write and which will tell us if a year is a leap year or not.

A method that returns true or false static boolean isLeapYear(int year) { if((year%400 == 0) || ((year%4 == 0) && (year%100 !=0))) { return true; } return false; } This is a boolean method: it returns a boolean value, either true or false. It takes a year number as argument and returns true if that year is a leap year and false otherwise.

How do we find out what day the 1 st of a given month is? int startDay is going to hold the day number (from 0..6, where 0 means Sunday, 1 means Monday etc.) for the first day in the month we’ve been asked about. How do we figure out what this day number should be? One possible solution: work from a known start day in the past. Compute number of days since 1/1/1800 to our starting day: getTotalNumOfDays(year, month). Find out what day 1/1/1800 was (it was a Wednesday). Convert that day into a number ( startDay1800 = 3 ;) Add that num to the total, and take mod 7, to get the day number for the first day in the month we’ve been asked about: startDay = (total + startDay1800) % 7;

getStartDay (get the day that 1 st of the month falls on) static int getStartDay(int year, int month) { int startDay1800 = 3; // get total number of days since 1/1/1800 long totalNumOfDays = getTotalNumOfDays(year, month); // get the start day for the month we’ve been asked about int startDay = (totalNumOfDays + startDay1800) % 7; return startDay; } This method gets the day (Sun, Mon, Tue etc) that the month we’ve been asked about starts on, returning the day as a number (0..6). It works out the start day by getting the total number of days that have passed since 1/1/1800, for which the day is known.

getTotalNumberOfDays (since 1/1/1800) static long getTotalNumOfDays(int year, int month) { long total = 0; // for each year from last year add 365 or 366 for (int i = 1800; i< year; i++){ if ( isLeapYear(i) ) { total = total + 366; } else { total = total + 365; } // for this year, add days in all months to this month for (int i = 1; i< month; i++){ { total = total + getNumOfDaysInMonth(year, i); } return total; } This method gets the total number of days passed since 1/1/1800 and the month we’ve been asked to draw a calendar for.

Design Diagram: what methods call other methods

How to design a program Only do very simple things directly in the main method of a program. For anyting that is slightly complicated, write a method. For slightly complicated things inside that method, write another method, and so on. This is called top-down design. Write and test each method one at a time. Do not write the entire program at once. Test a method by calling it in the main body of the program. Once you are sure a method is working correctly, you can call it in other parts of the program. A program written as a collection of short methods is easier to understand than one where everything is “bunched” into the main part of the program. It is also easier to write, debug, and modify.