38
Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehri

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

Embed Size (px)

Citation preview

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

Advanced Programming in Java

Peyman Dodangeh Sharif University of Technology Fall 2013

Lecture 1: Introduction to OOPSlides adapted from Steven Roehrig

Page 2: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Expected Background “A one-semester college course in

programming.” I assume you can write a program in some

language, understand variables, control structures, functions/subroutines.

If in doubt, let’s talk.

Page 3: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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

Page 4: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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

Page 5: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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

Page 6: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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

Page 7: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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!

Page 8: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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!

Page 9: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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.

This doesn’t help your job prospects.

Page 10: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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.

Page 11: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Course EtiquetteEtiquette is “conduct in polite society”No cell phonesNo random comings and goings If you are sleepy, go home If you want to read email or surf the Web,

please do it elsewhere

Page 12: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig
Page 13: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Object Oriented Programming

Problem Space the place where the problem exists such as a business

Solution Space the place where you’re implementing that solution such as a computer

The effort required to perform this mapping. E.g. think about a library, or a phonebook program

Page 14: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Object Oriented Approach

OOP lets the programmer represent problem space elements

The elements in the problem space

and their representations in the solution space

are referred to as “objects”

Page 15: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

OOP

The program is allowed to adapt itself to the lingo of the problem by adding new types of objects

when you read the code, you’re reading words that also express the problem.

15

Page 16: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

OOP (2)

OOP allows you to describe the problem in terms of the problem

Rather than in terms of the computer Objects in your code are similar to real

objects Recall the sample programs: phonebook and

library16

Page 17: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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.

Page 18: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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.

Page 19: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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.

Page 20: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Example Suppose I’ve defined this class in Java:

To make one, I typeLight lt = new Light();

If I want switch on My Lamp, I saylt.on();

Page 21: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

But Why Not Just…

This is legal, but just makes a “reference variable” named lt

This variable can refer to any Light object, but currently refers to nothing

The operator new actually causes an object to be created, so we tell it what kind we want

Light lt;

Page 22: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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

BottleOfBeer

-topOn:Boolean

+open():void+lift(height:int):void+tilt(angle:int):void

Page 23: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Object Oriented Languages

Smalltalk The first successful object-oriented language One of the languages upon which Java is based

Java C++ C##

Page 24: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

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.

Page 25: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Java History Java was created in 1991 by James Gosling in Sun Microsystems Initially called Oak

in honor of the tree outside Gosling's window Its name was changed to Java

because there was already a language called Oak. Sun Microsystems released the first public

implementation as Java 1.0 in 1995 Java syntax is similar to C and C++.

Page 26: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Java Motivation The need for platform independent language To be embedded in various consumer

electronic products like toasters and refrigerators

Platform independent?! Hardware Operating System

Page 27: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Java Motivation (2) At the same time, the World Wide Web and

the Internet were gaining popularity. Java could be used for internet programming. Why?

Platform independence Creation of Applets

Page 28: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

The Java technology is: A programming language

Java can create all kinds of applications A development environment

A compiler (javac) An interpreter (java) A documentation generator (javadoc) …

Compare it to C++

Page 29: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

High-Level Languages

Page 30: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Java Virtual Machine

Page 31: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Compile and Execution Stages

Compare to C++ and Assembly

.NET Framework

Sharif University of Technology

Page 32: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Java is Popular Some reports on programming languages

popularityAccording to Job advertisements Book sales Finding code on the web …

Page 33: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Characteristics of Java Java is simple Java is object-oriented Java is architecture-neutral Java is portable Java is interpreted Java is multithreaded Java is secure Java is robust

Page 34: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

First Example Create a file named First.java

Java class files have .java extension Note to naming convention

Copy this lines to the file Note: File name and class name should be the same.

Page 35: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

The Major Issues Editing

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

Page 36: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Assignment # 0 Download and install JDK

http://www.oracle.com/technetwork/java/javase/downloads/index.html JDK 7

Write a program that prints your name on the console

Compile and run the program

Page 37: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

Further Reading Read Java page on Wikipedia

http://en.wikipedia.org/wiki/Java_(programming_language)

Google this terms and phrases:

Page 38: Advanced Programming in Java Peyman Dodangeh Sharif University of Technology Fall 2013 Lecture 1: Introduction to OOP Slides adapted from Steven Roehrig

The End.