Advanced Programming in Java Peyman Dodangeh n Sharif

  • Slides: 38
Download presentation
Advanced Programming in Java Peyman Dodangeh n Sharif University of Technology n Fall 2013

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

Expected Background o o o “A one-semester college course in programming. ” I assume

Expected Background o o o “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.

Course Outline o o Week 1: Background, basics of O-O, first Java program, programming

Course Outline o o 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. ) o o o Week 5: Polymorphism, abstract classes, design patterns

Course Outline (cont. ) o o o 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. ) o o o Week 10: Exception handling, threads Week 11:

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

Administrative Details (cont. ) o o o Midterm and final exams Homework + Quiz

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

Administrative Details (cont. ) o o Everything is attached to the syllabus. Don’t look

Administrative Details (cont. ) o o 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 Power. Point background before printing. Save toner!

Administrative Details (cont. ) o o o Attendance is not required, but… …you are

Administrative Details (cont. ) o o o 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 o o o Cheating means “submitting, without proper attribution, any

My Policy on Cheating o o o 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.

My Policy on Cheating o o You may discuss homework problems with classmates, after

My Policy on Cheating o o 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.

Course Etiquette o Etiquette is “conduct in polite society” o No cell phones o

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

Object Oriented Programming o Problem Space n n o Solution Space n n o

Object Oriented Programming o Problem Space n n o Solution Space n n o the place where the problem exists such as a business the place where you’re implementing that solution such as a computer The effort required to perform this mapping. n E. g. think about a library, or a phonebook program

Object Oriented Approach o OOP lets the programmer represent problem space elements o The

Object Oriented Approach o OOP lets the programmer represent problem space elements o The elements in the problem space and their representations in the solution space are referred to as “objects”

OOP o The program is allowed to adapt itself to the lingo of the

OOP o The program is allowed to adapt itself to the lingo of the problem n o by adding new types of objects when you read the code, you’re reading words that also express the problem. 15

OOP (2) o o OOP allows you to describe the problem in terms of

OOP (2) o o 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 library 16

O-O Languages (Alan Kay) o o o Everything is an object. A program is

O-O Languages (Alan Kay) o o o 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.

Objects o o o An object has an interface, determined by its class. A

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

Built-In Types o Think of an int… n n o What is its interface?

Built-In Types o Think of an int… n n o 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 n n Dream up useful classes, and Endow them with appropriate characteristics.

Example o Suppose I’ve defined this class in Java: o To make one, I

Example o Suppose I’ve defined this class in Java: o To make one, I type Light lt = new Light(); o If I want switch on My Lamp, I say lt. on();

But Why Not Just… Light lt; o o o This is legal, but just

But Why Not Just… Light lt; o o o 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

Designers Design, Users Use o The interface is the critical part, but the details

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

Object Oriented Languages o Smalltalk n n o o o The first successful object-oriented

Object Oriented Languages o Smalltalk n n o o o The first successful object-oriented language One of the languages upon which Java is based Java C++ C##

Why So Many Languages? o o o Bring the language “closer” to the problem.

Why So Many Languages? o o o Bring the language “closer” to the problem. But 4 GLs 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.

Java History o o o Java was created in 1991 by James Gosling in

Java History o o o Java was created in 1991 by James Gosling in Sun Microsystems Initially called Oak n o Its name was changed to Java n o o in honor of the tree outside Gosling's window 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++.

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

Java Motivation o o The need for platform independent language To be embedded in various consumer electronic products n o like toasters and refrigerators Platform independent? ! n n Hardware Operating System

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

Java Motivation (2) o o o At the same time, the World Wide Web and the Internet were gaining popularity. Java could be used for internet programming. Why? n o Platform independence Creation of Applets

The Java technology is: o A programming language n o A development environment n

The Java technology is: o A programming language n o A development environment n n o Java can create all kinds of applications A compiler (javac) An interpreter (java) A documentation generator (javadoc) … Compare it to C++

High-Level Languages

High-Level Languages

Java Virtual Machine

Java Virtual Machine

Compile and Execution Stages o Compare to C++ and Assembly o . NET Framework

Compile and Execution Stages o Compare to C++ and Assembly o . NET Framework Sharif University of Technology

Java is Popular o Some reports on programming languages popularity n According to n

Java is Popular o Some reports on programming languages popularity n According to n Job advertisements n Book sales n Finding code on the web n …

Characteristics of Java o o o o Java is simple Java is object-oriented Java

Characteristics of Java o o o o 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

First Example o Create a file named First. java n n o Java class

First Example o Create a file named First. java n n o Java class files have. java extension Note to naming convention Copy this lines to the file n Note: File name and class name should be the same.

The Major Issues o Editing n o Compiling n n o Use any text

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

Assignment # 0 o Download and install JDK n n o o http: //www.

Assignment # 0 o Download and install JDK n n o o http: //www. oracle. com/technetwork/javase/downloads/index. html JDK 7 Write a program that prints your name on the console Compile and run the program

Further Reading o Read Java page on Wikipedia http: //en. wikipedia. org/wiki/Java_(programming_language) o Google

Further Reading o Read Java page on Wikipedia http: //en. wikipedia. org/wiki/Java_(programming_language) o Google this terms and phrases: �Java Mobile �JVM �Byte code �Java Sun �Java and C++ �Java and C#

o The End.

o The End.