Object-Oriented Programming C

Slides:



Advertisements
Similar presentations
Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig.
Advertisements

Lab#1 (14/3/1431h) Introduction To java programming cs425
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
CMSC 132: Object-Oriented Programming II
CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
HST 952 Computing for Biomedical Scientists Lecture 2.
Honors 101, Fall 2006 Please do not sit in back of room! Lots of info on web page Join the mailing list Download Eclipse and start using it Read the text.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Programming Languages and Paradigms Object-Oriented Programming.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
JAVA Introduction ● One of the main JAVA design goal is reducing complexity for programmer – Development time is half or less comparing to equivalent C++
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
CSE 501N Fall ‘09 00: Introduction 27 August 2009 Nick Leidenfrost.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
Object Oriented Programming (OOP) Design Lecture 1 : Course Overview Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Object Oriented Programming (OOP) Design Lecture 1 : Course Overview Bong-Soo Sohn Associate Professor School of Computer Science and Engineering Chung-Ang.
1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Object-Oriented Programming MISM/MSIT Carnegie Mellon University Lecture 1: Introduction to OOP.
Chapter 2: Everything is an Object ● C++ has many non object oriented features inherited from C. It is a hybrid language meaning that it support different.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
OOP (Java): Prelim/ OOP (using Java) Objective – –to give some background on the course Semester 2, Who we are: Andrew Davison.
1 Software Systems Development CEN Spring 2011 TR 12:30 PM – 1:45 PM ENB 116 Instructor:Dr. Rollins Turner Dept. of Computer Science and Engineering.
POS 406 Java Technology And Beginning Java Code
Objected Oriented Programming & Design JAVA Shishir Gupta (704) (704)
CSCI 51 Introduction to Computer Science Dr. Joshua Stough January 20, 2009.
The Joy of Programming (also known as) Introduction to Object-Oriented Programming.
Salman Marvasti Sharif University of Technology Fall 2014.
Introduction to Databases Computer Science 557 September 2007 Instructor: Joe Bockhorst University of Wisconsin - Milwaukee.
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CS 340 DATA STRUCTURES Lecture: Everything is an Object.
Introduction to Data Structures
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
+ Introduction to Class IST210 Class Lecture. + Course Objectives Understand the importance of data, databases, and database management Design and implement.
Everything Is an Object Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
Lecture.1: Getting Started With Java Jiang (Jen) ZHENG May 9 th, 2005.
Introduction to ECE 2401 Data Structure Fall 2005 Chapter 0 Chen, Chang-Sheng
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
10/8: Software What is software? –Types of software System software: Operating systems Applications Creating software –Evolution of software development.
CSE S. Tanimoto Java Introduction 1 Java A Programming Language for Web-based Computing with Graphics.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Salman Marvasti Sharif University of Technology Winter 2015.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
Object Oriented Software Development 4. C# data types, objects and references.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Peyman Dodangeh Sharif University of Technology Spring 2014.
C Programming Lecture 1 : Introduction Bong-Soo Sohn Associate Professor School of Computer Science and Engineering Chung-Ang University.
Course Info Instructor U.T. Nguyen Office: CSEB Office hours: Tuesday, 14:30-15:30 Thursday, 12:00-12:45 By.
Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.
OOP Basics Classes & Methods (c) IDMS/SQL News
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Presentation transcript:

Object-Oriented Programming 95-712 C Sakir YUCEL MISM/MSIT Carnegie Mellon University Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Expected Background “A one-semester college course in programming.” (MISM admissions policy) I assume you can write a program in some language, understand variables, control structures, functions/subroutines. If in doubt, let’s talk.

Course Outline Week 1: Background, basics of O-O, first Java program, programming environments Week 2: Raw materials: types, variables, operators, program control Week 3: Classes: declarations, constructors, cleanup & garbage collection Week 4: Packages, access specifiers, finals, class loading

Course Outline (cont.) Week 5: Polymorphism, abstract classes, design patterns Week 6: Interfaces & extends, inner classes, callbacks via inner classes Week 7: Applets, Applications, Swing Week 8: Graphics and Multimedia Week 9: Arrays, container classes, iterators

Course Outline (cont.) Week 10: Exception handling, threads Week 11: Java I/O, networking Week 12: JDBC and object persistency

Administrative Details Professor: Sakir Yucel Office: Hamburg Hall, 3021 Office Hours: Wed 4:45pm-5:15pm, 8:30-9:00, or by appointment Email: syucel@andrew.cmu.edu Course Web sites www.andrew.cmu.edu/~syucel/Java www.cmu.edu/blackboard Blackboard points to the real Web site. Blackboard used for grades, discussion board.

Administrative Details (cont.) Our teaching assistant is Karen Chen She will be setting up times for office hours shortly

Administrative Details (cont.) Required Text: Horstmann & Cornell, “Core Java, Volume 1 - Fundamentals,” Sun Microsystems Press Additional Online Texts: The Java Tutorial http://java.sun.com/docs/books/tutorial/ Thinking in Java by Bruce Eckel http://mindview.net/Books/TIJ/

Administrative Details (cont.) 14 weeks Two consecutive 80-minute lectures per week, a break in between the lectures. Course notes in PowerPoint or HTML. Course notes available (usually) Tuesday before class. Send course questions to Blackboard’s Discussion Group, personal questions to me.

Administrative Details (cont.) Midterm and final exams Homework: 50% Midterm exam 20% Final exam: 30% I will give one A+.

Administrative Details (cont.) Everything is attached to the syllabus. Don’t look for assignments, etc. on Blackboard. Look at the syllabus! Homework usually weekly. Submission instructions with each assignment, usually printed listings and a zip file. Printing slides? Three to a page, at least. Save a tree! Remove the PowerPoint background before printing. Save toner!

Administrative Details (cont.) Attendance is not required, but… …you are responsible for everything said in class. I encourage you to ask questions in class. Don’t guess, ask a question!

My Policy on Cheating Cheating means “submitting, without proper attribution, any computer code that is directly traceable to the computer code written by another person.” I give students a failing course grade for any cheating. Expulsion is also possible. This doesn’t help your job prospects.

My Policy on Cheating You may discuss homework problems with classmates, after you have made a serious effort in trying the homework on your own. You can use ideas from the literature (with proper citation). You can use anything from the textbook/notes. The code you submit must be written completely by you.

Even More On Cheating “In addition to any penalties imposed by the instructor, including failing the course, all cheating and plagiarism infractions will be reported in writing to the Associate Dean for the program, the Associate Dean of Faculty, the Dean of Student Affairs, and the Dean. They will review and determine if expulsion should be recommended. The report will become part of the student’s permanent record.”

Course Etiquette Etiquette is “conduct in polite society” No cell phones No random comings and goings If you are sleepy, go home If you want to read email or surf the Web, please do it elsewhere

Programming Language Evolution Machine language Assembler “3rd generation” (COBOL, FORTRAN, C) Specialized (Lisp, Prolog, APL) “4th generation” (SQL, spreadsheets, Mathematica) “5th generation” (example, anyone?)

Why So Many Languages? Bring the language “closer” to the problem. But 4GLs are typically focused on specialized domains (e.g., relational databases). We want a language that is general purpose, yet can easily be “tailored” to any domain.

Object-Oriented Languages Smalltalk, C++, Java, etc… You can make any kind of objects you want How different from procedural languages? No different at all: Every (reasonable) language is “Turing complete” Very different: Make expression easier, less error-prone

O-O Languages (Alan Kay) Everything is an object. A program is a bunch of objects telling each other what to do, by sending messages. Each object has its own memory, and is made up of other objects. Every object has a type (class). All objects of the same type can receive the same messages.

Making Java Work It's “easy as pie” to write procedural code in Java. It takes some discipline (an attitude) to think O-O. It's worthwhile to do it.

Objects An object has an interface, determined by its class. A class is an abstract data type, or user-defined type. Designing a class means defining its interface.

Built-In Types Think of an int… What is its interface? How do you “send it messages”? How do you make one? Where does it go when you’re done with it? In solving a computational problem, the goal is to Dream up useful classes, and Endow them with appropriate characteristics.

Example Suppose I’ve defined this class in Java: To make one, I type BottleOfBeer myBeer = new BottleOfBeer(); If I want myBeer opened, I say myBeer.open();

But Why Not Just… BottleOfBeer myBeer; This is legal, but just makes a “reference variable” named myBeer This variable can refer to any BottleOfBeer object, but currently refers to nothing The operator new actually causes an object to be created, so we tell it what kind we want

Designers Design, Users Use The interface is the critical part, but the details (implementation) are important too. Users use the interface (the “public part”); the implementation is hidden by “access control”.

Objects vs. Procedural Libraries C libraries are like this, sort of: The library designer invents a useful struct. Then she provides some useful functions for it. The user creates an instance of the struct, then applies library functions to it. One big difference is that anyone can change any part of the struct. Booo, hsss! Another difference is in initialization.

Two Ways of Reusing Classes Composition: One class has another as a part (indicated by the diamond “aggregation” symbol).

Two Ways of Reusing Classes Inheritance: One class is a specialized version of another (indicated by the triangle “inheritance” symbol).

Polymorphism Different subclasses respond to the same message, possibly with different actions.

Some Java Code Patron p1 = new Patron(); Patron p2 = new YankPatron(); Patron p3 = new BritPatron(); Patron p4 = new GermanPatron(); p1.BeerPlease() // polite request p2. BeerPlease() // rude request p3.BeerPlease() // polite request p4.BeerPlease() // request in German (but polite) This is a bit of a trick: it requires late binding of the function call.

Creating Objects We usually assume this is free; with built-in types like int or char, we just say int i; char c; With user-defined types (the ones we make), we need to be explicit about what we want: constructor function This is a very important issue!

Destroying Objects If an object goes “out of scope,” it can no longer be used (its name is no longer known). In C++, we might need to write an explicit function to free memory allocated to an object. Java uses references and “garbage collection”.

Example of Object Scope public String getTitle(int lectureNumber) { LectureNotes lect; lect = syllabus.getLecture(lectureNumber); String s = lect.getLine(1); return s; } What happens to lect? The LectureNotes object still exists, but the reference lect disappears (it’s out of scope after return). Eventually, the garbage collector removes the actual LectureNotes object.

A Simple Model of Memory The reference variable lect holds a memory location The LectureNotes object is at that location 0x1234 memory 22 C++ Versus Java\n… When you “speak of” the variable lect, you are “referring to” the actual LectureNotes object. When lect goes out of scope it is automatically destroyed. The LectureNotes object lives on, but nobody can use it…unless there are other references to it

Java’s Use of Memory Stack Heap Static variables Constants Non-RAM storage

Java’s Primitive Types Size Wrapper type boolean - Boolean char 16-bit Character byte 8-bit Byte short Short int 32-bit Integer long 64-bit Long float Float double Double void Void

Wrapper Types Variables of primitive types are “automatic”, i.e., they are stored on the stack. They are automatically deleted when they go out of scope. What if you want an object holding a primitive type? Example: char c = ‘x’; Character C = new Character(‘x’);

Really Big Numbers BigInteger, BigDecimal These are arbitrary precision, as big as they need to be. You can’t use the usual operators (+-*/) since they are objects. But there are methods (functions) to do these things.

Creating New Types Now it’s legal to say class MyNewType { // definition here } Now it’s legal to say MyNewType m = new MyNewType();

Class Members Fields (a.k.a. member variables, data members) Methods (a.k.a. member functions) class MyClass { int a; YourClass b; float memberFunction(int x, float f) { return 0; }

Let’s Write Something // Our first program. File: HelloDate.java // Note that the file name is exactly the same // as the class name, including capitalization. import java.util.*; public class HelloDate { public static void main(String[] args) { System.out.println(“Hello, it is ”); System.out.println(new Date()); }

The Major Issues Editing Compiling Running Use any text editor you like (not a word processor!); save as HelloDate.java Compiling From a DOS or UNIX command line, type > javac HelloDate.java This should produce the file HelloDate.class Running Again from the command prompt, type > java HelloDate

Getting the Java Documentation Point your browser to http://java.sun.com/docs/index.html Download version 1.4.2 of the J2SE API Bookmark the file so you can always get to it easily Or, just bookmark the index page on Sun’s Java Web site