COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
EC-241 Object-Oriented Programming
Advertisements

C++ Classes & Data Abstraction
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Object Oriented Programming: Java Edition By: Samuel Robinson.
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Objects & Classes Weiss ch. 3. So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API.
Lab 1 Logbook ADT. OVERVIEW A monthly logbook consists of a set of entries, one for each day of the month.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Homework 10 Due ( MT sections ) ( WTh sections ) at midnight Sun., 11/10 Mon., 11/11 Problems
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
COMPUTER 2430 Object Oriented Programming and Data Structures I
OOP: Encapsulation &Abstraction
Objects as a programming concept
Data Structures and Algorithms revision
COMPUTER 2430 Object Oriented Programming and Data Structures I
Examples of Classes & Objects
CMPE 135: Object-Oriented Analysis and Design September 14 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
COMPUTER 2430 Object Oriented Programming and Data Structures I
Introduction to Object-Oriented Programming (OOP)
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS Week 14 Jim Williams, PhD.
CS 302 Week 11 Jim Williams, PhD.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS Week 13 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
// simple Date // guarantee initialization with constructor // provide some notational convenience struct Date {           int y, m, d;                            //
Object Based Programming
Introduction to Computer Programming
Classes and Objects Encapsulation
COMPUTER 2430 Object Oriented Programming and Data Structures I
Simple Classes in C# CSCI 293 September 12, 2005.
CS 1430: Programming in C++ No time to cover HiC.
COMPUTER 2430 Object Oriented Programming and Data Structures I
תוכנה 1 בשפת Java שיעור מספר 5: "זרוק לו עצם"
Interfaces and Constructors
Classes & Objects: Examples
COMPUTER 2430 Object Oriented Programming and Data Structures I
Interfaces.
Introduction to Object-Oriented Programming (OOP)
CS2011 Introduction to Programming I Objects and Classes
COMPUTER 2430 Object Oriented Programming and Data Structures I
Simple Classes in Java CSCI 392 Classes – Part 1.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 2430 Object Oriented Programming and Data Structures I
Object-Oriented Programming Using C++
CIS 199 Final Review.
CS 200 Creating Classes Jim Williams, PhD.
Data Structures & Algorithms
Classes and Objects Reusing Classes with Composition
CS 200 Creating Classes Jim Williams, PhD.
Introduction to Object-Oriented Programming (OOP)
Classes and Objects Object Creation
CS 240 – Advanced Programming Concepts
Chapter 5 Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Any better way to implement the methods?

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

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

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

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)) }

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

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

Quiz 1 Monday 10 Points Note01 – Note05 Lab1 Prog1

Lab 2 Due Friday, September 21