Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER 2430 Object Oriented Programming and Data Structures I

Similar presentations


Presentation on theme: "COMPUTER 2430 Object Oriented Programming and Data Structures I"— Presentation transcript:

1 COMPUTER 2430 Object Oriented Programming and Data Structures I
Day1 on Thursday, before lab1

2 Class Constructors public class Date {
private int year = 2018, month = 1, day = 1; public Date (int inYear, int inMonth, int inDay) year = inYear; month = inMonth; day = inDay; } public Date (int inMonth, int inDay) public Date (int inYear) public Date () // use the default values . . . 2

3 Class Instances (Objects)
// Each instance has its own // year, month and day. // Method print() uses the // values of the instances Date d1 = new Date(); Date d2 = new Date(2018, 9, 14); d1.print(); // 2018/1/1 d2.print(); // 2018/9/14 d1 2018, 1, 1 d2 2018, 9, 14

4 Instance Data Members public class Date {
// Instance data members: “this” instance private int year = 2018, month = 1, day = 1; // Constructors create instances and assign the initial // values to the instance data members. public Date (int inYear, int inMonth, int inDay) public Date (int inMonth, int inDay) public Date (int inYear) public Date () . . . } 4

5 Instance Methods public class Date { . . .
// Instance methods: “this” instance public Date tomorrow() public Date addDays(int t) public int dateDiff(Date d) . . public static void main( String [] args ) Date d1 = new Date(); Date d2 = new Date(2018, 9, 14); Date d3 = d1.tomorrow(); d3 = d2.addDays(7); int days = d3.dateDiff(d2); System.out.println(“The difference: ” + d2.dateDiff(d1)); } Which one is “this” instance? 5

6 Instances (Objects) Assignment
Date d1; d1 = new Date(2018, 9, 14); Date d2 = d1; // Two variables, one instance. How can we create an instance by copying the instance data members? d1 2018, 9, 14 d2

7 Copy Constructor public class Date { // Instance fields
private int year = 2018, month = 1, day = 1; // Constructors: to create “this” instance public Date ( int inYear, int inMonth, int inDay ) public Date ( int inMonth, int inDay ) public Date ( int inYear ) public Date () /** Makes an instance that has the same values as aDate. aDate is a parameter of Date, which is not “this” instance. */ public Date ( Date aDate ) this.year = aDate.year; this.month = aDate.month; this.day = aDate.day; } . . . 7

8 Copy Constructor public class Date { // Instance fields
private int year = 2018, month = 1, day = 1; // Constructors: to create “this” instance public Date ( int inYear, int inMonth, int inDay ) public Date ( int inMonth, int inDay ) public Date ( int inYear ) public Date () /** Makes an instance that has the same values as aDate. aDate is a parameter of Date, which is not “this” instance. */ public Date ( Date aDate ) year = aDate.year; // year: this.year month = aDate.month; // month: this.month day = aDate.day; // day: this.day } . . . 8

9 Instances (Objects) Assignment
// Each instance has its own // year, month and day. Date d1 = new Date(2018, 9, 14); Date d2 = new Date(d1); // Two variables, two instances. // Same date d1 2018, 9, 14 d2 2018, 9, 14

10 Can a class have non-instance members?
public class Date { // Instance data members private int year = 2018, month = 1, day = 1; // Constructors: to create “this” instance public Date ( int inYear, int inMonth, int inDay ) public Date ( int inMonth, int inDay ) public Date ( int inYear ) public Date () public Date ( Date aDate ) // Instance methods public print() public Date tomorrow() public Date addDays( int t ) public int dateDiff( Date d ) . . . } Can a class have non-instance members? 10

11 Static Members public class Date {
// Static data field: one value for the class private static int datesCount = 0; // Instance data members private int year = 2018, month = 1, day = 1; // Constructors: to create “this” instance public Date ( int inYear, int inMonth, int inDay ) public Date ( int inMonth, int inDay ) public Date ( int inYear ) public Date () public Date ( Date aDate ) // Instance methods public print() public Date tomorrow() public Date addDays( int t ) public int dateDiff( Date d ) . . . } 11

12 The datesCount must be updated when a Date is created/destroyed!
Static Data Members public class Date { // Static data field: one value for the class private static int datesCount = 0; // Instance data members private int year = 2018, month = 1, day = 1; // Each constructor must increment datesCount public Date ( int inYear, int inMonth, int inDay ) . . . datesCount ++; } public Date ( int inMonth, int inDay ) public Date ( int inYear ) public Date () public Date ( Date aDate ) The datesCount must be updated when a Date is created/destroyed! 12

13 Static Methods public class Date { // Static data field
private static int datesCount = 0; // Instance data members private int year = 2018, month = 1, day = 1; // Each constructor must increment datesCount public Date ( int inYear, int inMonth, int inDay ) . . . datesCount ++; } public static int getCount() return datesCount; 13

14 Static Methods public class Date { // Static data field
private static int datesCount = 0; // Instance data members private int year = 2018, month = 1, day = 1; // Static method public static int getCount() return datesCount; } . . . public static void main( String [] args ) // Calling static methods System.out.println("Count: " + Date.getCount()); 14

15 Static Methods public class Date { private static int datesCount = 0;
public static int getCount() private int year = 2018, month = 1, day = 1; public void print() public static void main( String [] args ) System.out.println("Count : " + Date.getCount()); // Count: 0 Date d1 = new Date(); Date d2 = new Date(2016, 2, 28); // Count: 2 Date d3 = d2; d3 = new Date(d2); // Count: 3 } 15

16 Static Methods public class Date { private static int datesCount = 0;
public static int getCount() private int year = 2018, month = 1, day = 1; public void print() public static void main( String [] args ) System.out.println("Count : " + Date.getCount()); // Count: 0 Date d1 = new Date(); Date d2 = new Date(2018, 9, 14); // Count: 2 d1.print(); // Works? Date.print(); // Works? } 16

17 Static and Instance Members
public class Date { // Static data field private static int datesCount = 0; // Instance data members private int year = 2018, month = 1, day = 1; // Static method: Can it access instance data? // NO! public static int getCount() System.out.println(month + "/" + day + "/" + year); return datesCount; } . . . Static methods CANNOT access instance data, because there could be zero or multiple instances. 17

18 Static and Instance Members
public class Date { // Static data field private static int datesCount = 0; // Instance data members private int year = 2018, month = 1, day = 1; // Instance methods: Can it access static data? // Yes! public void print() System.out.println(month + "/" + day + "/" + year); System.out.println(“Count: ” + datesCount); } . . . Instance methods CAN access static data, because static data fields are available to all instances. 18

19 Implementing Other Methods
public class Date { // Private data private int year = 2018, month = 1, day = 1; // Public methods public void print() System.out.println(this.month + "/" + this.day + "/" + this.year); } /** Code example: Date d2 = d1.tomorrow(); The method should not change d1 Assignment could change d1: d1 = d1.tomorrow() */ public Date tomorrow() day ++; // Correct? Must return a new Date object Does not change “this” object 19

20 Method tomorrow public class Date {
private int year = 2018, month = 1, day = 1; /** Code example: Date d2 = d1.tomorrow(); Returns a new Date instance Must not change “this” instance */ public Date tomorrow() Date d = new Date(this); d.day ++; return d; } Correct? Last day of the month? 20

21 Method tomorrow public class Date {
private int year = 2018, month = 1, day = 1; public Date tomorrow() Date d = new Date(this); if (d.day < d.lastDayOfMonth()) d.day ++; else d.day = 1; d.month ++; if (d.month == 13) // magic number! d.year ++; d.month = 1; } return d; // returns the last day of the month of “this” date object private int lastDayOfMonth() 21

22 Private Method lastDayOfMonth
public class Date { . . . public Date tomorrow() // returns the last day of the month of “this” date object private int lastDayOfMonth() int lastDay; switch (month) case 1: case 3: case 5: case 7: case 8: case 10: case 12: lastDay = 31; break; case 4: case 6: case 9: case 11: lastDay = 30; default: if (isLeapYear()) lastDay = 29; else lastDay = 28; } return lastDay; 22

23 Private Method isLeapYear
public class Date { . . . public Date tomorrow() private int lastDayOfMonth() private boolean isLeapYear() boolean leapYear; if (year % 4 != 0) leapYear = false; else if (year % 100 != 0) leapYear = true; else if (year % 400 != 0) else return leapYear; } 23

24 Any better way to implement the methods?

25 ADT: Date Data 3 ints: year, month, day
String: “9/9/2002” 3 ints: year, month, day 1 int: # of days since 12/31/1899 2 ints: days of year, year 1 string and 2 ints: “May”, 2, 2006 ... Operations Creator / Initializer Tomorrow, yesterday Date + int: Date (# days from date) Date – Date: int (# of days between) Relational operations Conversion to a String Day of Week, Day of year, is it leap year Check for validity ... Most commercial implementations # of days since 12/31/1899 Our implmentation month, day, year

26 How to Implement Date Class?
public class Date { // number of days since 12/31/1899 private int numDays; // Constructors public Date ( int inYear, int inMonth, int inDay ) public Date ( int inMonth, int inDay ) public Date ( int inYear ) public Date () // Public methods void print() public Date tomorrow() public Date addDays( int t ) public int dateDiff( Date d ) // Private methods . . . } 26

27 Implementers and Users
public class Date { . . . } public class CS2430Sx public static void main( String [] args ) Date d1 = new Date(); Date d2 = new Date(2018, 9, 14); Date d3 = d1.tomorrow(); d3 = d2.addDays(7); int days = d3.dateDiff(d2); System.out.println(“The difference: ” + d2.dateDiff(d1)); The application code remains the same! Which one is “this” instance? 27

28 Prog1 public class ScoresList { private final int MAX_SIZE = 10; private int scores[] = new int[MAX_SIZE]; private int numScores = 0; public boolean appendScore ( int score ) // How to check if the array is full? if (numScores < MAX_SIZE) } public boolean delete ( int score ) // which one to remove? // call find! // should be private private int find ( int score ) public class Prog1 { public static void main( String [] args ) ScoresList theList = new ScoresList(); . . . // Do we know the size of the array and // number of scores in the array? if (theList.appendScore(theValue)) // Do we know if theValue in theList? // Do we call find? if (theList.delete(theValue)) }

29 Prog 1 You must follow the Software Development Grand Rules for CS You may lose up to five (5) points on style.

30 Features of OOP Abstraction Data Encapsulation Data Hiding
(Inheritance) (Polymorphism)

31 Quiz 1 Monday 10 Points Note01 – Note05 Lab1 Prog1

32 Lab 2 Due Friday, September 21


Download ppt "COMPUTER 2430 Object Oriented Programming and Data Structures I"

Similar presentations


Ads by Google