95-712 object oriented programming java

34
95-712 Lecture 1: Introdu ction 1 95-712 Object Oriented Programming Java Lecture 1: Introduction

Upload: alair

Post on 21-Jan-2016

71 views

Category:

Documents


0 download

DESCRIPTION

95-712 Object Oriented Programming Java. Lecture 1: Introduction. Structure of the Course. Lectures / class participation Homework (pencil and paper and programming) Quizzes Midterm and Final examinations Use Blackboard for queries on course material - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 1

95-712 Object Oriented Programming Java

Lecture 1: Introduction

Page 2: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 2

Structure of the Course

• Lectures / class participation• Homework (pencil and paper and

programming) • Quizzes• Midterm and Final examinations• Use Blackboard for queries on course

material• Send email to me regarding personal issues

Page 3: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 3

Readings

• Readings from the required text are assigned for each lecture -- read them in advance. The first reading is due for next week.

• Readings from the web also assigned• Look for updates to reading list on

schedule

Page 4: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 4

Grading

• Programming (5-7) 40%

• Three quizzes (low score dropped) 15%

• Midterm 20%

• Final Exam 25%

• I will assign exactly one “A+” per section.

Page 5: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 5

Teaching Assistant: Yubao

Office hours will beannounced soon.

Page 6: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 6

Important Web Sites

• http://www.andrew.cmu.edu/~mm6 • http://www.javasoft.com • http://java.sun.com/docs/books/tutorial/• http://www.eclipse.org/• http://www-128.ibm.com/developerworks/rational/library/769.html

Page 7: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 7

Prerequisites to OOP Logic

Logical And Logical OrP Q P^Q

T T T

T F F

F T F

F F F

P Q PVQ

T T T

T F T

F T T

F F F

Page 8: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 8

Prerequisites to OOP Logic

Examples of Logical Negation P Q -----

P^Q

T T F

T F T

F T T

F F T

P Q _ _PVQ

T T F

T F T

F T T

F F T

Page 9: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 9

Prerequisites to OOP Sets

Important Sets:

Denotes the empty set Denotes the set of integers {…,-2, -

1,0,1,2,…}R Denotes the set of real numbers

{…,-2.9, 4.5, 5.2, …}

Page 10: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 10

Prerequisites to OOPTypes

Expressions have types as well as

values:

Expression Type Value

4+2 integer 6

4<2 boolean false

4>2 boolean true

4.0+2.0 real 6.0

Page 11: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 11

Prerequisites to OOP Algorithm

Definition:

Informally, an algorithm is a process or set of steps to be followed in calculations or other problem-solving operations, esp. by a computer1.1. Oxford American Dictionary

Page 12: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 12

Prerequisites to OOPProtocol

Definition:

Informally, a protocol is a set of rules governing the exchange or transmission of data electronically between devices2.

2. Oxford American Dictionary

Page 13: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 13

Prerequisites to OOPASCII

Definition:

ASCII is an abbreviation for the American Standard Code for Information Interchange, a set of digital codes representing letters, numerals, and other symbols, widely used as a standard format in the transfer of text between computers3. ASCII is a seven bit code.

3. Oxford American Dictionary

Page 14: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 14

Prerequisites to OOP Unicode

Definition:

The Unicode Standard is a character coding system designed to support the worldwide interchange, processing, and display of the written texts of the diverse languages and technical disciplines of the modern world. In addition, it supports classical and historical texts of many written languages.4 Unicode characters may vary in size.4. Unicode.org

Page 15: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 15

Prerequisites to OOPBinary Data

The coding schemes mentioned above (ASCII and Unicode)amount to agreements about how characters in various alphabets may be represented as integers. The character “A”, for example, is represented as 6510.

But how do we represent the integer 65 in a computer?We usually use the Binary Numbering System. 6510=010000012.

So, suppose we encounter the binary value 010000012.What does it mean? Does it mean 6510 or “A” or is it a description of how eight switches are set?

We would need to know more about its context to answerthat question.

Page 16: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 16

Prerequisites to OOP Text Vs. Binary

Why is the binary numbering system used?

For much the same reason that we all use base 10for arithmetic. The algorithms are easy to learn. Trydoing long division with Roman numerals.

It’s simple to build machines to add and multiply and so on if the numbering system is base 2.

In general, character data is encoded into a series of bitsusing ASCII or Unicode. This is called text data. XML files,for example, are text files.

Everything else is binary data. Machine codeFiles , JPEG files, Java class files, Excel files, etc…, areall binary files.

Page 17: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 17

Prerequisites to OOP Assembly Language

A Programmer is given a problem to solve. She writes a solution in assembly language. Two things are on her mind: 1. The problem 2. The machine architecture (number and type of CPU registers, size of memory, memory addresses, instruction set, etc.) Her solution might look something like this:

mul R1,R2,40 add R1,R1,R2 STR Pay,R1 CALL PrintPayCheck

An assembler tool converts this to machine code and the machine code runs on the processor.

Page 18: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 18

Prerequisites to OOP Procedural Language

A Programmer is given a problem to solve. She writes a solution in Pascal. Two things are on her mind: 1. The problem and 2. Variables, instructions and procedures that change the state of the variables. Her solution looks something like this:

pay := hours * payRate; pay := pay + bonus; printPayCheck(pay);

A compiler converts this to machine code and the machine code is run on the processor.

Page 19: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 19

Prerequisites to OOP Objects

A Programmer is given a problem to solve. She writes a solution in Java. Two things are on her mind: 1. The problem. 2. Classes and objects - how they relate and interact

Her solution might look something like this:

Employee e = new Employee(hours,rate,bonus); e.printCheck();

This gets compiled into byte code and the byte code is executed by the Java Virtual Machine.

Page 20: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 20

Object Oriented Programming:

is a different way to think about software development.

reduces complexity. promotes code reuse and flexibility. promotes important concepts from software

engineering, e.g.,abstraction and information hiding. cannot do anything more than previous paradigms.

But can make our lives more enjoyable, software easier to write and reuse.

Page 21: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 21

Prerequisite to OOP:Variables

Variables are addressable areas of memory that change as a program runs.

Variables may contain simple values such as integers or reals.

Variables may contain addresses of other memory locations. These are called pointers or references.

Variable names should be chosen wisely.

Page 22: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 22

Prerequisite to OOP:Modulo Arithmetic

Modulo arithmetic is often a convenient tool to have handy. Informally, it is clock arithmetic.

01

2

3

4

51 + 5 = 0 (mod 6)12 = 0 (mod 6)5 + 4 = 3 (mod 6)2 * 5 = 4 (mod 6)23 = 5 (mod 6)

Without using a clock, divide 23 by 6 and take the remainder. So,23 mod 6 = 5.

Page 23: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 23

Prerequisite to OOP:Instruction Execution

Some problems, or parts of problems, may be solved bya simple series of instructions that when executed in sequence(one after the other) yield the answer or establish a desired state.

instruction1; instruction2; instruction3;

Page 24: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 24

Prerequisite to OOP:iteration

Some problems require us to iterate or loop over a series of instructions while some condition holds true.

while(someConditionHolds) { instruction1; instruction2; instruction3;}

Page 25: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 25

Prerequisite to OOP:Selection

Some problems require us to select which group of instructionsto execute based upon some criteria.

If(someConditionHolds) { instruction1; instruction2; instruction3;}else { instruction4; instruction5;

}

Page 26: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 26

Prerequisite to OOP:Application Types

Console applications run within a console window or shell.GUI (Graphical User Interface) applications control the windowing, buttons and text boxes and are typically event driven.Client side applications run on a user’s computer that is communicating with another machine. These may be console or GUI applications.Server side applications typically wait for client generated requests.

Page 27: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 27

Prerequisite to OOPDocumenting Code

• Important code is examined by programmers.• Programmers are expensive.• Appropriate use of comments tends to

shorten the time required to understand code. • Document clearly and succinctly.• Use the Javadoc tool.• Indent your programs carefully.

Page 28: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 28

Prerequisite to OOPMaking Assertions

• Programmers are expensive.• Programs are complex.• The liberal use of assertions helps in

debugging.• Make good use of assertions.

Page 29: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 29

Prerequisite to OOPIntegrated Debugging Environments(IDE”s)

• Many high quality Java IDE’s exist.• These include Apache Eclipse, Sun’s

Netbeans, Oracles JDeveloper, etc.• In this class we will use Eclipse.• You must also know how to compile and run

Java from the DOS or Unix command lines.

Page 30: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 30

Compiling and Running a Simple Program

import java.io.*;

public class TemperatureConverter {

public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter temperature in Fahrenheit>");

Page 31: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 31

String fahrenheit = in.readLine(); Double fObj = new Double(fahrenheit); double f = fObj.doubleValue(); double c = ((f - 32.0)/9.0) * 5.0; System.out.println("Celsius = " + c); }}

Page 32: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 32

From the command line…

javac TemperatureConverter.javajava TemperatureConverter

Page 33: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 33

With Eclipse…

Create a workspace.Create a Java project.Create a Java class.Switch between the Java and Debug perspectives.

Page 34: 95-712 Object Oriented             Programming Java

95-712 Lecture 1: Introduction 34

Problems with the code above:

• No comments.• No JavaDoc.• Makes no attempt to handle invalid input.• Not object oriented.