Title: 95-712 Object Oriented Programming Java
1 95-712 Object Oriented
Programming Java
2Structure 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
3Readings
- 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
4Grading
- Programming (5-7) 40
- Three quizzes (low score dropped) 15
- Midterm
20 - Final Exam 25
- I will assign exactly one A per section.
5Teaching Assistant Yubao
Office hours will be announced soon.
6Important 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/lib
rary/769.html
7Prerequisites to OOP Logic
8Prerequisites to OOP Logic
- Examples of Logical Negation
9Prerequisites 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,
10Prerequisites to OOPTypes
- Expressions have types as well as
- values
- Expression Type Value
- 42 integer 6
- 4lt2 boolean false
- 4gt2 boolean true
- 4.02.0 real 6.0
11Prerequisites 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
12Prerequisites 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
13Prerequisites 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
14Prerequisites 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
15Prerequisites 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.
6510010000012. 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 answer that
question.
16Prerequisites to OOP Text Vs. Binary
Why is the binary numbering system used? For
much the same reason that we all use base 10 for
arithmetic. The algorithms are easy to learn.
Try doing long division with Roman
numerals. Its 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 bits using ASCII or Unicode.
This is called text data. XML files, for example,
are text files. Everything else is binary data.
Machine code Files , JPEG files, Java class
files, Excel files, etc, are all binary files.
17Prerequisites 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.
18Prerequisites 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.
19Prerequisites 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.
-
20Object 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.
21Prerequisite to OOPVariables
- 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.
22Prerequisite to OOPModulo Arithmetic
- Modulo arithmetic is often a convenient tool to
have handy. Informally, it is clock arithmetic.
0
1 5 0 (mod 6) 12 0 (mod 6) 5 4 3 (mod
6) 2 5 4 (mod 6) 23 5 (mod 6)
1
5
2
4
3
- Without using a clock, divide 23 by
- 6 and take the remainder. So,
- 23 mod 6 5.
23Prerequisite to OOPInstruction Execution
Some problems, or parts of problems, may be
solved by a simple series of instructions that
when executed in sequence (one after the other)
yield the answer or establish a desired state.
instruction1 instruction2
instruction3
24Prerequisite to OOPiteration
Some problems require us to iterate or loop over
a series of instructions while some condition
holds true. while(someConditionHolds)
instruction1 instruction2
instruction3
25Prerequisite to OOPSelection
Some problems require us to select which group of
instructions to execute based upon some
criteria. If(someConditionHolds)
instruction1 instruction2
instruction3 else instruction4
instruction5
26Prerequisite to OOPApplication 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 users computer
that is - communicating with another machine. These may
be console - or GUI applications.
- Server side applications typically wait for
client generated - requests.
-
27Prerequisite 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.
28Prerequisite to OOPMaking Assertions
- Programmers are expensive.
- Programs are complex.
- The liberal use of assertions helps in debugging.
- Make good use of assertions.
29Prerequisite to OOPIntegrated Debugging
Environments(IDEs)
- Many high quality Java IDEs exist.
- These include Apache Eclipse, Suns 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.
30Compiling 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
Fahrenheitgt")
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)
32From the command line
javac TemperatureConverter.java java
TemperatureConverter
33With Eclipse
Create a workspace. Create a Java
project. Create a Java class. Switch between the
Java and Debug perspectives.
34Problems with the code above
- No comments.
- No JavaDoc.
- Makes no attempt to handle invalid input.
- Not object oriented.