1 / 32

COMPUTER 2430 Object Oriented Programming and Data Structures I

COMPUTER 2430 Object Oriented Programming and Data Structures I. ADT. Abstract Data Type Data and operations on the data Specify the behavior of objects From users’ point of view No implementation details Barbara Liskov  and Stephen N. Zilles in 1974

patricksims
Download Presentation

COMPUTER 2430 Object Oriented Programming and Data Structures I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPUTER 2430Object Oriented Programming andData Structures I

  2. ADT • Abstract Data Type • Data and operations on the data • Specify the behavior of objects • From users’ point of view • No implementation details • Barbara Liskov and Stephen N. Zilles in 1974 • Java and other OOPL implement ADTs using classes

  3. Features of OOP • Abstraction (ADT) • Data Encapsulation • Data Hiding • (Inheritance) • (Polymorphism)

  4. ADT Example: Date Lab1 due date, Prog1 due date... Data: year, month, day • Operations: Print Tomorrow (NextDay) Yesterday (PreviousDay) Date + int Date – Date Relational operations Day of Week Day of year . . .

  5. Implementing ADT Using Class // Java public class Date { private int year, month, day; public void print(); . . . public Date tomorrow() . . . public Date addDays( int days ) . . . public int dateDiff( Date d ) . . . . . . } 5

  6. Procedural Programming // Using structure struct Date { int year, month, day; } // Functions prototypes void Print( Date d ); Date Tomorrow( Date d ); Date AddDays ( Date d, int t ); int DateDiff( Date d1, Date d2 ); . . . Data and operations are separated Have to pass parameters for the Date

  7. Java Class Date public class Date { private int year, month, day; // No need for passing parameters public void print() { System.out.println(month + "/" + day + "/" + year); } . . . } 7

  8. Java Class CS2430Sx public class Date { private int year, month, day; public void print() { System.out.println(month + "/" + day + "/" + year); } } public class CS2430Sx { public static void main( String args[] ) { Date prog1; prog1.print(); // Will it work? } } 8

  9. Must Create Class Instance! public class Date { private int year, month, day; public void print() { System.out.println(month + "/" + day + "/" + year); } } public class CS2430Sx { public static void main( String args[] ) { Date prog1; // Class variable, not instance prog1 = new Date(); // Create an Date instance! prog1.print(); } } 9

  10. No Class Instance (Object) prog1 Date prog1; prog1.print(); ?

  11. Object (Instance) and Reference prog1 Date prog1; prog1 = new Date(); prog1.print(); public class Date { // default value 0 private int year, month, day; public void print() . . . } 0, 0, 0

  12. Class Constructors A class must have one or more constructor to create instances (objects) If a class does not define any constructors, Java will provide a default constructor without parameters Lab1 and Prog1 Default values for the data fields If a class has one or more constructors, Java will not provide the default constructor. 12

  13. Default and Initial Values public class Date { // private int year, month, day; Default value: 0 private int year = 2018, month = 1, day = 1; public void print() { System.out.println(month + "/" + day + "/" + year); } } public class CS2430Sx { public static void main( String args[] ) { Date prog1 = new Date; // default constructor prog1.print(); // 1/1/2018 } } 13

  14. Default and Initial Values public class Date { // Data fields for “this” instance private int year = 2018, month = 1, day = 1; // Instance method public void print() { System.out.println(this.month + "/" + this.day + "/“ + this.year); } } public class CS2430Sx { public static void main( String args[] ) { Date prog1 = new Date; // default constructor prog1.print(); // “this” instance: prog1 } } 14

  15. Object and Reference prog1 Date prog1 = new Date; // default constructor prog1.print(); // 1/1/2018 2018, 1, 1

  16. public class ScoresList { public final int MAX_SIZE = 10; private int values[] = new int[MAX_SIZE]; private int numValues = 0; . . . } // No constructor defined public class Prog1 { public static void main( String [] args ) { // Display a message "Start of ScoresList Test:" System.out.println("Declare a variable . . ."); ScoresList list; System.out.println("Create an object..."); list = new ScoresList(); // default constructor // Display all values in list list.print(); . . . } }

  17. Constructor public class Date { private int year, month, day; /** Constructor to create a Date instance (object) */ public Date ( int inYear, int inMonth, int inDay ) { year = inYear; month = inMonth; day = inDay; } . . . } Syntax! 17

  18. Instance “this” public class Date { private int year, month, day; /** Constructor to create a Date instance (object) */ public Date ( int inYear, int inMonth, int inDay ) { this.year = inYear; this.month = inMonth; this.day = inDay; } . . . } 18

  19. Constructor public class Date { private int year, month, day; public Date (int inYear, int inMonth, int inDay) . . . } public class CS2430Sx { public static void main( String args[] ) { Date prog1 = new Date(2018, 9, 14); prog1.print(); // 9/14/2018 } } 19

  20. Constructor public class Date { private int year, month, day; public Date ( int inYear, int inMonth, int inDay ) . . . } public class CS2430Sx { public static void main( String args[] ) { Date prog1 = new Date(2018, 9, 14); prog1.print(); Date quiz1 = new Date(); // Will it work? } } 20

  21. Class Constructors A class must have one or more constructors to create objects If a class does not define any constructor, Java will provide a default constructor If a class has one or more constructors, Java will not provide the default constructor. 21

  22. Different 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 ) { month = inMonth; day = inDay; } public Date ( int inYear ) { year = inYear; } . . . } 22

  23. Different Constructors public class Date { private int year = 2018, month = 1, day = 1; public Date ( int inYear, int inMonth, int inDay ) public Date ( int inMonth, int inDay ) public Date ( int inYear ) . . . } public class CS2430Sx { public static void main( String [] args ) { Date d1, d2; d1 = new Date(2019); // Good! d1.print(); // 1/1/2019 d2 = new Date(); // Not good! . . . } } 23

  24. Date Class public class Date { private int year = 2018, month = 1, day = 1; public Date ( int inYear, int inMonth, int inDay ) public Date ( int inMonth, int inDay ) public Date ( int inYear ) public Date () // My own default constructor: 1/1/2018 // Public methods public print() public Date tomorrow() public Date addDays( int t ) public int dateDiff( Date d ) . . . // Private methods (helpers) . . . } 24

  25. Code Examples Date d1; d1 = new Date(); d1.print(); Date d2 = new Date(2006); d2.print(); d2 = d1.tomorrow(); d2.print(); Date quiz1, prog1; prog1 = new Date(2018, 9, 14); prog1.print(); quiz1 = prog1.addDays(3); quiz1.print();

  26. Object and Reference 2018, 1, 1 d1 d2 Date d1; d1 = new Date(); Date d2 = new Date(2006); d1 = new Date(2006); d1 = d2; d1 = new Date(2018, 9, 14); d2 = d1; d2 = new Date(2018, 9, 14); One variable can point to diff objects at diff times Multiple variables can point to the same object at the same time Garbage collection 2006, 1, 1 2018, 9, 14 2006, 1, 1 2018 , 9, 14

  27. 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) { month = inMonth; day = inDay; } public Date (int inYear) { year = inYear; } public Date () { // use the default values or write your own code } . . . } 27

  28. Class Constructors A class must have one or more constructors to create instances (objects). If a class does not define any constructor, Java will provide a default constructor. If a class has one or more constructors, Java will not provide the default constructor. To create “this” instance 28

  29. Instance Methods Most class methods are instance methods. Instance methods are invoked on class instances. Date d; d.print(); // Does not work! d = new Date(2019); d.print(); // Works on an instance! 29

  30. Prog1 • Requirements • Individual Assignment • Follow Instructions! Grand Rules! • Any Questions?

  31. Submitting Multiple Files Just select all files!

  32. Quiz 1: 10 Points Monday, September 17 Close book/notes No devises such as cellphones or calculators

More Related