LECTURE FOR Day 1
Download
Report
Transcript LECTURE FOR Day 1
Introduction
Introduction of Java
Introduction to Programming
Course Goals/Objectives
Grading
CMIS 102A
1
Programming Languages
A programming language is a language used to write
instructions for a computer to follow
Java is one of many programming languages
Features of Java
Platform independent
OOP= Object Oriented Programming
Applications and applets
CMIS 102A
2
Platform Independent
Platform independent: the same program can run on
different computers with different operating systems or
different hardware. (more later)
Here are three different computers each with their own
unique Hardware and Software.
X86
RISC
PowerPC
(IBM compatible with Windows)
(common in embedded systems)
(Built by Macintosh)
A program for X86 will not run on a PowerPC
CMIS 102A
3
Programming Languages
Machine language
Language that a computer can understand directly
Consists of only 0's and 1's
Different computers have different machine languages
Want to write a program in a machine language?
Measure of information:
bit (b): a 0 or 1
byte (B): eight bits
CMIS 102A
Examples:
file size: 84KB
internet connection: 2Mbps
4
Computer Thinks in Binary
Binary (base 2) - numbers use 2 digits:
JUST 0 and 1
Decimal (base 10) - numbers use 10 digits: 0
THROUGH 9
Converting numbers:
0 0
5
1 1
6
2 10
7
3 11
8
4 100
9
101
110
10
11
12
13
14
5
Measure of Information
Bit (b; short for binary digit)
either a 0 or 1
Byte (B)
1 byte = 8 bits
1
1
1
1
KB = 1024 bytes (2^10)
MB = 1,048,576 bytes (2^20)
GB = 2^30 ~ 10^9
TB
= 2^40 ~ 10^12
6
Programming Languages
Assembly language (low level language)
Using symbolic names for binary codes
Need an assembler to translate an assemblylanguage program to machine code
Example:
Instruction
JMP 2010
STO 617, 201
JZ0 201
CMIS 102A
Machine Language
00100010
10001001
10010010
7
Programming Languages
High-level languages
Closer to a natural language; easy for human beings
to read and write
Two categories:
Compiled
Interpreted
CMIS 102A
8
Programming Languages
Compiler (is a special SW program)
A program that translates a program in a high-level
language to machine code
Source program (code): program in high-level language
Object program (code): translated machine code version
Compiling a program and running a program are two
different activities
Interpreter (is a special SW program)
Translates and executes each instruction in the source
program, one at a time
CMIS 102A
9
Programming Languages
Compiled
Compile once then
run many times
(faster)
If something
changes, the whole
source must be
Compiled again.
Distribution
problems
Interpreted
Since each
instruction is
translated, it’s
slower.
If something
changes, just that
code needs to be
changed.
Distribution not as
hard
10
How Does Java Achieve Platform-Independent?
First, a Java program is compiled into an intermediate
code called byte-code
Byte-code is a common language that all computers
should understand
In order to understand this byte code, a computer has
to install a Java Virtual Machine (JVM)
Then, the byte code is translated into machine code and
executed, one at a time, by Java byte-code interpreter
Byte-code is platform independent!
CMIS 102A
11
H ig h L e v e l
la n g u a g e
SOURCE CODE
C O M P IL E R fo r
W IN D O W S X 8 6
C O M P IL E R
R IS C
X86
N eeded
O b je c t file s
X86
O B J E C T F IL E
R IS C N e e d e d
O b je c t F ile s
C O M P IL E R
P o w e rP C
P o w e rP C
N eeded
O b je c t F ile s
R IS C
O B J E C T F IL E
P o w e rP C
O B J E C T F IL E
R IS C
P o w e rP C
CMIS 102A
X86
12
JAVA
S o u rc e C o d e
JAVA
S o u rc e C o d e
JAVA
S o u rc e C o d e
JAVA
C O M P IL E R
W IN D O W S X 8 6
JAVA
C O M P IL E R
R IS C
JAVA
C O M P IL E R
P o w e rP C
JAVA
BYTECO DE
X86
JVM
X86
CMIS 102A
R IS C
JVM
P o w e rP C
JVM
R IS C
P o w e rP C
13
Java – Pure OOP Language
To be covered next class
CMIS 102A
14
Java – Applications and Applets
Java application
Stand-alone program
CUI: console User interface
GUI: Graphical User Interface
Java applet
Run within a web browser
Sent across the Internet and run on a local computer
Byte-code program is sent
Local computer has to have JVM
CMIS 102A
15
History of Java
Developed by James Gosling and his team at Sun Micro-system in
1991
As James Gosling and Henry McGilton (1996) wrote in their white
paper The Java Language Environment,
"The design requirements of Java are driven by the nature of the
computing environment in which software must be deployed."
Computing environment means: World Wide Web, local area
networks, intranets, embedded systems such as TV set-top boxes,
PDAs, cell-phones, copy machines, and other interconnected devices.
Based on C and C++
Still changing
1.4.2
1.5.0
CMIS 102A
16
Working with JAVA and JCreator
You could write JAVA programs in a text program like
notepad and compile them in the command line. But this is
not convenient.
JCreator is a program that lets you easily:
Type the JAVA program
Compile the program
Run the program
Debug the program
** It’s a program that helps you build JAVA programs
CMIS 102A
17
Working with JAVA and JCreator
CMIS 102A
18
Programming
A computer program: a sequence of instructions for a
computer to follow to solve a problem
Programming: the process of creating a computer
program to solve a problem
A computer can do some basic operations well; it
follows instructions well
But it must be told what to do
A human being comes up with the solutions
How to do programming?----next!
CMIS 102A
19
STEP 1: Understand the requirements
Company Payroll Case Study (from [1])
A small company needs an interactive program to figure its weekly payroll.
The payroll clerk will input data for each employee. The input data includes
employee's pay rate, and the number of hours worked that week.
The program should display the weekly pay for that employee.
The weekly pay is calculated as follows:
Regular pay equals the pay rate times the number of hours worked,
up to 40 hours.
If the employee worked more than 40 hours, wage equals regular pay
for 40 hours plus over time pay for hours over 40
Over time pay equals 1.5 times the pay rate times the overtime hours
These are the requirements!
20
Do some examples to get a feel for the problem…note
the logic you take to solve the problem
Example 1: If an employee's hourly pay rate is $30.00,
and he worked 52 hours.
…………………………………………Then he made ____Dollars
Example 2: If an employee's hourly pay is $10.00, and
she worked 36 hours.
…………………………………………Then he made ____Dollars
Example 3: If an employee's hourly pay is $20.00 and he
worked 40 hours.
…………………………………………Then he made ____Dollars
21
Analyzing the Problem by looking at
INPUT and OUTPUT
INPUT DATA
Hourly payRate
Hours worked
OUTPUT
RESULTS
Weekly pay
22
Step 2) Find an Algorithm for
Calculating Weekly Pay
Repeat the following process for each employee:
1. Get the employee’s hourly payRate
2. Get the hours worked this week
3. Calculate Wage:
If hours worked is more than 40
wage = (40.0 * payRate) + (hours - 40.0) * 1.5 *payRate
Else
wage = hours * payRate
4. Display wage
23
An Algorithm
A set of instructions for solving a problem
Step-by-step solution
Precise (same input gives same output always)
A computer program is, by definition, an algorithm. But we
usually use algorithm to mean instructions expressed in
English or something like English
Algorithms we've seen:
Recipe
Direction
Manual
24
Another Example (from [1])
Procedure to start your car
1.
2.
3.
4.
5.
6.
7.
Insert the key.
Make sure transmission is in Park (or Neutral).
Depress the gas pedal.
Turn the key to the start position.
If the engine starts within six seconds, release the key to the
ignition position.
If the engine doesn't start in six seconds, release the key and gas
peal, wait ten seconds, and repeat Steps 3 through 6, but not more
than five times.
If the car doesn't start, call the garage
CMIS 102A
25
An Exercise
Write an algorithm that gets the price and quantity of an
item bought, and displays the total amount paid for that
item after tax. The tax rate is fixed to be 5%.
To get started, work these:
a)
5 widgets at 12.00 each. How much do you pay?
b)
15 widgets at 2.00 each. How much do you pay?
c)
W widgets at P dollars each. How much do you pay?
CMIS 102A
26
How to Do Programming?
1) Analyzing the problem: Identifying the input data and the
output results
2) Developing the solution algorithm
3) Coding the algorithm using a specific programming language
4) Testing
Running (executing) your program on the computer, to see if it
produces correct results
If it does not, then you must find out what is wrong with your
program or algorithm and fix (debugging)
27
Structured Programming
Top-down development
Any computer program can be written using only three
basic control structures:
Sequence: a sequence of instructions executed in the
order they're written
Selection: executing different instructions based on
certain conditions
Loop: repeating a set of instructions while certain
conditions are met
CMIS 102A
28
Object-Oriented Programming
To be covered next class
CMIS 102A
29
Course Goals and Objectives
Describe the development of computer programming and
the main characteristics of Java
Gain an understanding of the fundamentals of objectoriented programming, including classes, objects, creating
objects, and using methods
Apply top-down and other techniques for algorithmic
problem-solving in designing and writing computer
programs
Apply incremental development techniques in writing
complex programs
CMIS 102A
30
Course Goals and Objectives
Understand and use fundamental programming constructs such as
data types and declarations, assignment statements, variables,
constants, and arithmetic and logical operators
Understand and apply sequence, selection, and loop control
structures in writing programs to solve common computing problems
Understand and use pre-defined classes
Create, compile, and execute Java programs to do the following:
Get input data from the user and display results to the user
Perform simple arithmetic calculations, and string manipulations
Perform different tasks based on user input or results of calculations
(selection)
Repeat tasks (loop)
CMIS 102A
31
Course Goals and Objectives
Document programs effectively
Design and use test data for validating programs
CMIS 102A
32
Grading
Projects (3)
50%
LABS should help complete the projects
Homework including labs (assigned but not graded)
These are the ‘self-test questions’ in text
Quizzes (2)
20%
Short 20 question mc t/f fill-in questions and 1 JAVA code
question.
Final
30%
I’ll give you a sample Final + Answers
Participation counts toward marginal cases
If your on the edge of a grade, I look at the participation
CMIS 102A
33
Miscellaneous
Extra Credit. Short ans: None. 1) I have no ‘extra’ time to
manage extra credit work. 2) Not a good practice as a
student. The intention is good but misdirected.
All military related absences or late assignments are
excusable. You are the one responsible for the learning
the material.
Planned absences should not exceed 25% of the course.
i.e. planned vacation or military exercises should not
exceed 4 classes
Never disappear from class [email protected]
Especially when dropping a course!
CMIS 102A
34
Best Way to Learn Programming?
Quotes from Bill Gates:
the best way to prepare (to be a programmer) is to write
programs, and to study great programs that other people
have written. In my case, I went to the garbage cans at the
Computer Science Center and I fished out listings of their
operating system. You've got to be willing to read other
people's code, then write your own, then have other
people review your code.
CMIS 102A
35
Best Way to do well in class
Do the HW. Don’t just look at answers
Participate in class
Memorize the basic coding structures
Do the projects well (50% of grade!)
Differentiate the ‘interesting’ from the ‘testable’
Time Management !?!
CMIS 102A
36
Review (1)
High-level
programming language
byte-code
program
interpreter
Java
compiler
source
program
machine language
object
program
CMIS 102A
platform
independent
37
Review (2)
programming
algorithm
loop
computer
program
sequence
CMIS 102A
selection
input and
output
38
References
[1] Programming and Problem Solving with C++, 3rd Ed., Nell Dale,
Chip Weems, Mark Headington, pp 33-37.
[2] Structured programming,
http://en.wikipedia.org/wiki/Structured_programming
[3] COMPILERS, INTERPRETERS AND VIRTUAL MACHINES,
http://www.homepages.com.pk/kashman/jvm.htm#_computers
CMIS 102A
39