COP 3330 ObjectOriented Programming Summer 2008 Methods In
COP 3330: Object-Oriented Programming Summer 2008 Methods In Java – Part 2 Instructor : Dr. Mark Llewellyn markl@cs. ucf. edu HEC 236, 407 -823 -2790 http: //www. cs. ucf. edu/courses/cop 3330/sum 2008 School of Electrical Engineering and Computer Science University of Central Florida COP 3330: Methods In Java – Part 2 Page 1 © Mark Llewellyn
Implementation Details • Continuing with our example from the previous set of notes that prints a monthly calendar based on the month and year supplied by the user, we now will focus on implementation issues. • Last time we completed the structural design of our solution and created a Java class that implemented stubs for each of the methods we defined in our solution. The desired output, structural design chart, and the stub program are repeated on the next four pages. COP 3330: Methods In Java – Part 2 Page 2 © Mark Llewellyn
Output from the calendar program COP 3330: Methods In Java – Part 2 Page 3 © Mark Llewellyn
Structure chart for our problem print. Calendar (main) read. Input print. Month. Title get. Month. Name print. Month. Body get. Start. Day get. Total. Number. Of. Days get. Number. Of. Days. In. Month is. Leap. Year COP 3330: Methods In Java – Part 2 Page 4 © Mark Llewellyn
Class implementation with stubs only import java. util. Scanner; public class print. Calendar { /** * Main method */ public static void main(String[] args) { // Create input scanner object Scanner input = new Scanner(System. in); //Prompt user to enter year System. out. print("Enter full year (e. g. 2008): "); int year = input. next. Int(); //Prompt user to enter month System. out. print("Enter month as a number between 1 and 12: "); int month = input. next. Int(); //Call method to print the calendar print. Month(year, month); } //end main method /** stub for print. Month */ public static void print. Month(int year, int month){ System. out. print(month + " " + year); }//end print. Month method /** stub for print. Month. Title */ public static void print. Month. Title(int year, int month) { }//end print. Month. Title method COP 3330: Methods In Java – Part 2 Page 5 © Mark Llewellyn
/** stub for print. Month. Body */ public static void print. Month. Body(int year, int month) { }//end print. Month. Body method /** stub for get. Month. Name */ public static String get. Month. Name(int month){ return("January"); //dummy value }//end get. Month. Name method /** stub for get. Start. Day */ public static int get. Start. Day(int year, int month) { return 1; //dummy value }//end get. Start. Day method /** stub for get. Number. Of. Day. In. Month */ public static int get. Number. Of. Days. In. Month(int year, int month){ return 31; //dummy value }//end get. Number. Of. Days. In. Month method /** stub for get. Total. Number. Of. Days */ public static int get. Total. Number. Of. Days(int year, int month) { return 10000; //dummy value }//end get. Total. Number. Of. Days /** stub for is. Leapyear */ public static boolean is. Leap. Year(int year) { return true; //dummy value }//end is. Leap. Year method }//end print. Calendar class COP 3330: Methods In Java – Part 2 Page 6 © Mark Llewellyn
Execution of stub version in Eclipse Shows execution of main method and stub for print. Month which simply prints its arguments at the moment. COP 3330: Methods In Java – Part 2 Page 7 © Mark Llewellyn
Implementation Details • As we develop the method bodies for the various methods as defined in our structure chart, we’ll have some specific implementation details that must be dealt with at the time. • However, our abstraction gives us the luxury of only needing to deal with those details when we are developing the code for that specific method. When dealing with other methods, those details specific to other methods are not important – we’ve abstracted away those details. • For example, when we need to develop the is. Leap. Year(int year) method, we’ll need to know that leap years are years that are evenly divisible by 4. Years that are evenly divisible by 100 are not leap years unless they are also evenly divisible by 400 (in the Gregorian calendar at least). So we could say: if (year modulo 4 is 0) and (year modulo 100 is not 0) or (year modulo 400 is 0) then leap else no_leap In Java we would express this condition as: return(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); COP 3330: Methods In Java – Part 2 Page 8 © Mark Llewellyn
Implementation Details • Similarly, to implement the method: get. Total. Number. Of. Days. In. Month(int year, int month), we’ll need to know that January, March, May, July, August, October, and December each have 31 days. April, June, September, and November each have 30 days. February has 28 days in a common year and 29 days in a leap year. So a regular year has 365 days and a leap year has 366 days. • To implement get. Total. Number. Of. Days(int year, int month), we’ll get sum the total number of days between January 1, 1800 and the first day of the month of the calendar. To do this we can calculate the total number of days from January 1, 1800 to January 1 of the year in question and then add the remaining days in the calendar year to the first of the month in question. COP 3330: Methods In Java – Part 2 Page 9 © Mark Llewellyn
Implementation Details Design method Implement method Create test program for the method Method fails tests Method tests OK Fix method implementation Go to next method COP 3330: Methods In Java – Part 2 Page 10 © Mark Llewellyn
Implementation Details • We’ll continue our development of this program using a top-down approach. This means that we would first implement the main method (the on the top of our structure chart). • The main method, based on our structure chart is based on two subproblems, read. Input and print. Month. • We’ll decide here, for the sake of readability that the statements that would comprise the read. Input method, which involve reading the year and month for which the user would like the calendar, will simply be coded as statements in the main method. The alternative is to create a method for this, however, in this case, it is relatively simple input and it would seem unnecessary to place this code in a method, so we’ll simply move those tasks to the main method. – Note that if we did implement a read. Input method that main would consist of only two lines of code, the first to invoke the read. Input method and the second would be to invoke the print. Month method. COP 3330: Methods In Java – Part 2 Page 11 © Mark Llewellyn
Modified structure chart for our problem print. Calendar (main) Read user input print. Month. Title get. Month. Name print. Month. Body get. Start. Day get. Total. Number. Of. Days get. Number. Of. Days. In. Month is. Leap. Year COP 3330: Methods In Java – Part 2 Page 12 © Mark Llewellyn
Implementation Details • Next, we’ll focus on the print. Month method. As you can see from the structure chart on the previous page, this sub-problem consists of two sub-problems: (1) print. Month. Title and (2) print. Month. Body. Thus, the implementation of this method will consist of two method invocations for the respective sub-problems. COP 3330: Methods In Java – Part 2 Page 13 © Mark Llewellyn
Implementation Details Stub version /** stub for print. Month */ public static void print. Month(int year, int month){ System. out. print(month + " " + year); }//end print. Month method Complete version /** Print the calendar for a month in a year */ static void print. Month(int year, int month) { // Print the headings of the calendar print. Month. Title(year, month); // Print the body of the calendar print. Month. Body(year, month); } COP 3330: Methods In Java – Part 2 Page 14 © Mark Llewellyn
Modified structure chart for our problem print. Calendar (main) Read user input print. Month. Title get. Month. Name print. Month. Body get. Start. Day get. Total. Number. Of. Days get. Number. Of. Days. In. Month is. Leap. Year COP 3330: Methods In Java – Part 2 Page 15 © Mark Llewellyn
Implementation Details • Using the top-down approach, we’ll work with the print. Month. Title sub-problem first. This task consists of building the header information for the calendar in question. So this method will need to know the year and month from the input, so we set both as arguments to the method. The rest is handled with print statements. COP 3330: Methods In Java – Part 2 Page 16 © Mark Llewellyn
Implementation Details Stub version /** stub for print. Month. Title */ public static void print. Month. Title(int year, int month) { }//end print. Month. Title method /** stub for print. Month. Body */ Complete version /** Print the month title, e. g. , June, 2008 */ static void print. Month. Title(int year, int month) { System. out. println(" " + get. Month. Name(month) + " " + year); System. out. println("---------------"); System. out. println(" Sun Mon Tue Wed Thu Fri Sat"); } COP 3330: Methods In Java – Part 2 Page 17 © Mark Llewellyn
Modified structure chart for our problem print. Calendar (main) Read user input print. Month. Title get. Month. Name print. Month. Body get. Start. Day get. Total. Number. Of. Days get. Number. Of. Days. In. Month is. Leap. Year COP 3330: Methods In Java – Part 2 Page 18 © Mark Llewellyn
Implementation Details Stub version /** stub for get. Month. Name */ public static String get. Month. Name(int month){ return("January"); //dummy value }//end get. Month. Name method COP 3330: Methods In Java – Part 2 Page 19 © Mark Llewellyn
Implementation Details Complete version /** Get the English name for the month */ static String get. Month. Name(int month) { String month. Name = null; switch (month) { case 1: month. Name = "January"; break; case 2: month. Name = "February"; break; case 3: month. Name = "March"; break; case 4: month. Name = "April"; break; case 5: month. Name = "May"; break; case 6: month. Name = "June"; break; case 7: month. Name = "July"; break; case 8: month. Name = "August"; break; case 9: month. Name = "September"; break; case 10: month. Name = "October"; break; case 11: month. Name = "November"; break; case 12: month. Name = "December"; } return month. Name; } COP 3330: Methods In Java – Part 2 Page 20 © Mark Llewellyn
Modified structure chart for our problem print. Calendar (main) Read user input print. Month. Title get. Month. Name print. Month. Body get. Start. Day get. Total. Number. Of. Days get. Number. Of. Days. In. Month is. Leap. Year COP 3330: Methods In Java – Part 2 Page 21 © Mark Llewellyn
Implementation Details Stub version public static void print. Month. Body(int year, int month) { }//end print. Month. Body method COP 3330: Methods In Java – Part 2 Page 22 © Mark Llewellyn
Implementation Details Complete version /** Print month body */ static void print. Month. Body(int year, int month) { // Get start day of the week for the first date in the month int start. Day = get. Start. Day(year, month); // Get number of days in the month int number. Of. Days. In. Month = get. Number. Of. Days. In. Month(year, month); // Pad space before the first day of the month int i = 0; for (i = 0; i < start. Day; i++) System. out. print(" "); for (i = 1; i <= number. Of. Days. In. Month; i++) { if (i < 10) System. out. print(" " + i); else System. out. print(" " + i); if ((i + start. Day) % 7 == 0) System. out. println(); } COP 3330: Methods In Java – Part 2 Page 23 © Mark Llewellyn
Modified structure chart for our problem print. Calendar (main) Read user input print. Month. Title get. Month. Name print. Month. Body get. Start. Day get. Total. Number. Of. Days get. Number. Of. Days. In. Month is. Leap. Year COP 3330: Methods In Java – Part 2 Page 24 © Mark Llewellyn
Implementation Details Stub version /** stub for get. Start. Day */ public static int get. Start. Day(int year, int month) { return 1; //dummy value }//end get. Start. Day method Complete version /** Get the start day of month/1/year */ static int get. Start. Day(int year, int month) { final int START_DAY_FOR_JAN_1_1800 = 3; // Get total number of days from 1/1/1800 to month/1/year int total. Number. Of. Days = get. Total. Number. Of. Days(year, month); // Return the start day for month/1/year return (total. Number. Of. Days + START_DAY_FOR_JAN_1_1800) % 7; } COP 3330: Methods In Java – Part 2 Page 25 © Mark Llewellyn
Modified structure chart for our problem print. Calendar (main) Read user input print. Month. Title get. Month. Name print. Month. Body get. Start. Day get. Total. Number. Of. Days get. Number. Of. Days. In. Month is. Leap. Year COP 3330: Methods In Java – Part 2 Page 26 © Mark Llewellyn
Implementation Details Stub version /** stub for get. Total. Number. Of. Days */ public static int get. Total. Number. Of. Days(int year, int month) { return 10000; //dummy value }//end get. Total. Number. Of. Days Complete version /** Get the total number of days since January 1, 1800 */ static int get. Total. Number. Of. Days(int year, int month) { int total = 0; // Get the total days from 1800 to 1/1/year for (int i = 1800; i < year; i++) if (is. Leap. Year(i)) total = total + 366; else total = total + 365; // Add days from Jan to the month prior to the calendar month for (int i = 1; i < month; i++) total = total + get. Number. Of. Days. In. Month(year, i); return total; } COP 3330: Methods In Java – Part 2 Page 27 © Mark Llewellyn
Modified structure chart for our problem print. Calendar (main) Read user input print. Month. Title get. Month. Name print. Month. Body get. Start. Day get. Total. Number. Of. Days get. Number. Of. Days. In. Month is. Leap. Year COP 3330: Methods In Java – Part 2 Page 28 © Mark Llewellyn
Implementation Details Stub version /** stub for is. Leapyear */ public static boolean is. Leap. Year(int year) { return true; //dummy value }//end is. Leap. Year method} Complete version /** Determine if it is a leap year */ static boolean is. Leap. Year(int year) { return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); } COP 3330: Methods In Java – Part 2 Page 29 © Mark Llewellyn
Modified structure chart for our problem print. Calendar (main) Read user input print. Month. Title get. Month. Name print. Month. Body get. Start. Day get. Total. Number. Of. Days get. Number. Of. Days. In. Month is. Leap. Year COP 3330: Methods In Java – Part 2 Page 30 © Mark Llewellyn
Implementation Details Stub version /** stub for get. Number. Of. Days. In. Month */ public static int get. Number. Of. Days. In. Month(int year, int month){ return 31; //dummy value }//end get. Number. Of. Days. In. Month method Complete version /** Get the number of days in a month */ static int get. Number. Of. Days. In. Month(int year, int month) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 31; if (month == 4 || month == 6 || month == 9 || month == 11) return 30; if (month == 2) return is. Leap. Year(year) ? 29 : 28; return 0; // If month is incorrect } COP 3330: Methods In Java – Part 2 Page 31 © Mark Llewellyn
Implementation Details • We’re done! • All of the subprograms have been implemented and tested as we went from top to bottom in our structure chart. • A complete program listing for this problem is available on the course website. COP 3330: Methods In Java – Part 2 Page 32 © Mark Llewellyn
- Slides: 32