COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
C++ Classes & Data Abstraction
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object-oriented analysis (OOA) techniques are used to (1) study existing objects to see if they can be reused or adapted for new uses, and (2) define new.
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 4 Objects and Classes.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Learning about Programming Languages By: Mike and Sean.
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.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
OOP Basics Classes & Methods (c) IDMS/SQL News
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Web Design & Development Lecture 9
Classes (Part 1) Lecture 3
Concepts of Object Oriented Programming
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMPE 135: Object-Oriented Analysis and Design September 14 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
Module 5: Common Type System
Class Definitions and Writing Methods
COMPUTER 2430 Object Oriented Programming and Data Structures I
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Object Based Programming
CSC 113 Tutorial QUIZ I.
Interface.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Simple Classes in C# CSCI 293 September 12, 2005.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Building Java Programs
COMPUTER 2430 Object Oriented Programming and Data Structures I
Learning Objectives Classes Constructors Principles of OOP
Object Oriented Programming
COMPUTER 2430 Object Oriented Programming and Data Structures I
Java Inheritance.
Java Classes and Objects
CMSC 202 Classes and Objects.
CS 2430 Object Oriented Programming and Data Structures I
Introduction to Object-Oriented Programming
COMPUTER 2430 Object Oriented Programming and Data Structures I
OO Programming Concepts
Building Java Programs
Data Structures and ADTs
Chapter 11 Inheritance and Encapsulation and Polymorphism
CSG2H3 Object Oriented Programming
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

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

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

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

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 . . .

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

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

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

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

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

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

Object and Reference Date prog0 = new Date(); prog0.print(); prog0 0, 0, 0

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

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

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

Object and Reference Date prog0 = new Date; prog0.print(); prog0 2000, 1, 1

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

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

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

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

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

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

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

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

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

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();

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

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

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

Individual Assignment Prog0 Requirements Individual Assignment Follow Instructions! Any Questions?

Submitting Multiple Files Just select all files!

Lab Pals Monday – Thursday 6 – 9 pm Lab 009

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