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 Features of OOP Abstraction Data Encapsulation (Data Hiding)
(Inheritance) (Polymorphism)

3 Abstraction Abstract Data Type (ADT)
Mathematical model 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

4 ADT Example: 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 Traditional Approach // C++ 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;
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 prog0; prog0.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 prog0; Date prog0 = new Date(); // Create an Date instance! prog0.print(); 9

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

11 Object and Reference Date prog0 = new Date(); prog0.print(); prog0
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 Lab0 and Prog0 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 = 2000, month = 1, day = 1; public void print() System.out.println(month + "/" + day + "/" + year); } public class CS2430Sx public static void main(String args[]) Date prog0 = new Date; // default constructor prog0.print(); // 1/1/2000 13

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

15 Object and Reference Date prog0 = new Date; prog0.print(); prog0
2000, 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 Prog0 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 prog0 = new Date(2017, 9, 15); prog0.print(); // 9/15/2017 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 prog0 = new Date(2017, 9, 15); prog0.print(); Date quiz1 = new Date(); quiz1.print(); // 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 = 2000, 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) . . . 22

23 Different Constructors
public class Date { private int year = 2000, 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(2011); // Good! d2 = new Date(); // Not good! 23

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

25 Code Examples Date d1; d1 = new Date(); d1.print(); Date d2 = new Date(2006); d2.print(); d2 = d1.tomorrow(); Date quiz1, prog0; prog0 = new Date(2017, 9, 15); prog0.print(); quiz1 = prog0.addDays(3); quiz1.print();

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

27 Class Constructors public class Date {
private int year = 2000, 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 . . . 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 Individual Assignment
Prog0 Requirements Individual Assignment Follow Instructions! Any Questions?

30 Submitting Multiple Files
Just select all files!

31 Lab Pals Monday – Thursday 6 – 9 pm Lab 009

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


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

Similar presentations


Ads by Google