Lecture 3 CSCI-260-W02
Class Agenda Last lecture review Homework practice New material Homework questions
1.1 Review Questions What are main models of s/w life cycle? –How do they differ? –What are specific advantages of one against another? –Any other models you know? What are the goals quality s/w must satisfy? –HINT: There are 4 of them
Review Questions What types of programming do you know? –How do they differ? What are 2 ways to express structural design? Describe in your own words the ideas behind object-oriented methodology –List the terms you know and explain them
Review Questions What does UML stand for? What is its purpose? What kind of UML diagrams do you know? What are the different access control mechanisms in Java? How are they shown on UML class diagrams?
Homework Example
Specific Class Methods Constructor –An operation creating a new instance (object) of a class Observer (or accessor or getter) –An operation allowing to observe the state of an object without changing it Transformer (or mutator or setter) –An operation that changes the internal state of an object
UML Class Diagram for Date Class
Source Code of Date Class public class Date { protected int year; protected int month; protected int day; public static final int MINYEAR = 1583; // Constructor public Date(int newMonth, int newDay, int newYear) { month = newMonth; day = newDay; year = newYear; } // Observers public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } public int lilian() { // Returns the Lilian Day Number // of this date. // Algorithm goes here. } public String toString() // Returns this date as a String. { return(month + "/" + day + "/" + year); }
About Lilian day The Julian day or Julian Day Number (JDN) is the integer number of days that have elapsed since an initial epoch defined as noon GMT January 1, 4713 BC in Julian calendar. This noon-to-noon day is counted as Julian day 0. –09/17/2014 2pm (ET), JDN is The Lilian day number (LDN) is similar to the Julian day number, except that LDN 1 started at midnight on the 1 st day of the Gregorian calendar, that is, October 15, 1582 –09/17/2014 2pm (ET), LDN is Conversion formula: –JDN – Other important day formats are in /** Returns the Lilian Day Number of this date. * Precondition: This is a valid date after 10/14/1582. * Computes # of days between 1/1/0 and this date as * if no calendar reforms took place, then subtracts * 578,100 so that October 15, 1582 is day 1. */ public int lilian() { /** number of calculated days from 1/1/0 to 10/14/1582*/ final int subDays = ; int numDays = 0; // Add days in years. numDays = year * 365; // Add days in the months. if (month <= 2) numDays = numDays + (month - 1) * 31; else numDays = numDays + ((month - 1) * 31) – ((4 * (month-1) + 27) / 10); // Add days in the days. numDays = numDays + day; // Take care of leap years. numDays = numDays + (year / 4) - (year / 100) + (year / 400); // Handle special case of leap year before leap day if (month < 3) { if ((year % 4) == 0) numDays = numDays - 1; if ((year % 100) == 0) numDays = numDays + 1; if ((year % 400) == 0) numDays = numDays - 1; } // Subtract extra days up to 10/14/1582. numDays = numDays - subDays; return numDays; }
Objects Date myDate = new Date(6, 24, 1951); Date yourDate = new Date(10, 11, 1953); Date ourDate = new Date(6, 15, 1985);
Applications An object-oriented application is a set of objects working together, by sending each other messages, to solve a problem. In object-oriented programming (OOP) a key step is identifying classes that can be used to help solve a problem. –An example: using our Date class to solve the problem of calculating the number of days between 2 dates (next 3 slides)
DaysBetween Design 1.Display instructions 2.Prompt for and read in info about the first date 3.Create the date1 object 4.Prompt for and read in info about the second date 5.Create the date2 object 6.IF dates entered are too early 6.1 Print an error message ELSE 6.2 Use the date.lilian() method to obtain the Lilian Day Numbers 6.3 Compute and 6.4 Print the number of days between the dates
// // DaysBetween.java by Dale/Joyce/Weems Chapter 1 // // Asks the user to enter two "modern" dates and then reports // the number of days between the two dates. // import java.util.Scanner; public class DaysBetween { public static void main(String[] args) { Scanner conIn = new Scanner(System.in); int day, month, year; System.out.println("Enter two 'modern' dates: month day year"); System.out.println("For example January 12, 1954 would be: "); System.out.println(); System.out.println("Modern dates occur after " + Date.MINYEAR + "."); System.out.println(); System.out.println("Enter the first date:"); month = conIn.nextInt(); day = conIn.nextInt(); year = conIn.nextInt(); Date date1 = new Date(month, day, year); Source Code of DaysBetween Class (Application)
System.out.println("Enter the second date:"); month = conIn.nextInt(); day = conIn.nextInt(); year = conIn.nextInt(); Date date2 = new Date(month, day, year); if ((date1.getYear() <= Date.MINYEAR) || (date2.getYear() <= Date.MINYEAR)) System.out.println("You entered a 'pre-modern' date."); else { System.out.println("The number of days between"); System.out.print(date1); System.out.print(" and "); System.out.print(date2); System.out.print(" is "); System.out.println(Math.abs(date1.lilian() - date2.lilian())); } Source Code of DaysBetween (Contd)
Homework Example
Todays Homework Exercise 29, 30 –Build on class modeling and inheritance TODO: –Recreate the classes as described –Add additional features from all listed exercises –Create the documentation as if you were writing these classes from scratch Build on existing textbook diagrams and descriptions –Create a test application that tests all functionality of both classes.