Download 01Introduction To Programming

Document related concepts
no text concepts found
Transcript
Introduction To Programming
About me
• Prof. Lyon,
–
–
–
–
vc (203)641-6293 cell.
Fax (203)877-4187
[email protected]
Office Hours; appt only (for now).
About the Book
• Java for Programmers:
– by D. Lyon, Pren. Hall.
• Grading:
– 1/3, homework
– 1/3 midterm
– 1/3 final
Homework
•
•
•
•
•
•
weekly or bi-weekly
Due on time
You loose 10 points per day late!
Midterms are due in class with a demo.
Final is due in class with a demo.
Tests are take home.
Course Info
• Web page:
– http://www.docjava.com
• Distributed Computing
–
–
–
–
multiple computers working on a single task.
Network gaming
client-server
browser – web-server
Examples of Distributed Computing
• Services provided by a server
–
–
–
–
–
–
–
ftp server
print server
file server
mail server
echo server
ssh server
telnet server
Services
• Where are services listed?
• /etc/services
– gives you a list of “ports”
– Distributed Computing is Network Computing
– You study Network Programming to do
Network Computing
Cluster Computing
• Group(s) of computers that are
geographically co-located. Typically central
administration using a master-work
paradigm.
Cluster Problems
•
•
•
•
hard to load balance
need benchmark
need a mechanism to register a volunteer
Configuration problem – need a compute
server on every machine.
• need to detect when idle.
Cluster Problems
• Need a way to partition a problem.
• I need a way to transfer jobs from one
machine to another.
• How do you run a program on demand?
• Java Web Start!
Examples of Cluster Computing
• File System
• SETI
• etc…
What is Java?
• A programming language
• A technology that supports the language
What is a Programming
Language?
• A language that can run on a computer.
• An invention that helps facilitate
communication.
• Generally, it helps people communicate
with a computer.
What is a Computer?
• A machine that processes data.
• E.g. Brain= bio-computer
What is a machine?
• A device that transforms energy.
• Computers use energy to perform
information processing.
• E.g., Wheel, human, brain=biological
computer!
Can Machines Think?
• Humans think that they can think.
• Human brain is a kind of machine.
• Therefore machines can think.
Example Application of A
programming Language
• recipe
– ingredients – data structures (<-input)
– procedure to follow - algorithms
– results - output
So Java Can
•
•
•
•
Take input
Fill Data structures
Run algorithms
Generate Output
What is HTML?
• Hypertext Markup Language
• Used by a Browser
• Presentation of content
What is a browser?
• a program that decodes HTML and presents
it to the user.
– For example:
• Explorer
• Netscape
• Lynx
What is the Internet?
• a collection of local area networks.
• It is often viewed using a browser.
What is a Computer network?
• A means of communicating between
computers.
Internet=Internetwork
• Collection of Networks
• Message forwarding on a data packet net
What is the Web?
• GUI on the Internet
• Can use HTML for presentation of content
• Browsers decode presentation data for
display
What is the HTML Model?
• Browsers present data embedded in HTML
– HTML
– Images
• GIF, JPEG, PNG, ....
– Audio
• AIFF, AU,...
– PLUGINS
What’s a Plugin
• A program that helps a browser process
data.
– Shockwave
– RealPlayer
– Quicktime
Java Model
• Add to the number of formats we can
decode.
• Run the Java program automatically when
we visit a web page using a browser with
Java.
Would you run a program on
Demand?
• A big security risk?
• Feature Limits help us to be secure.
– trusted programs can do anything
– untrusted programs have big limits.
• no file access
• no access across the net to other machine.
• Other restrictions too.
What is a Compiler?
• Is a program that inputs a programming
language and outputs another programming
language!
• Output language is lower level than input
language.
What is Javac?
• The java compiler
• Inputs java source code
• outputs BYTE CODES
How do I run BYTE CODES?
• java is the command that invokes the JVM.
• java inputs BYTE CODES and RUNs them!
Java Environment/
Life Cycle of Java Code
Runtime
Environment
Compile-time
Environment
Bytecode
Verifier
Java
Source
(.java)
Java
Compiler
Class
Loader
Java
Bytecodes
move locally
or through
network
Java
Interpreter
Just in
Time
Compiler
Runtime System
Java
Bytecode
(.class )
Operating System
Hardware
Java
Class
Libraries
Java
Virtual
machine
Lifecycle of Java Code
Whats a Byte?
• a collection of 8 bits
Primitive Data Types
• What is a Data type?
– It is a structured record that hold bits in a
format that is defined by either the programmer
or the language designer.
– Eg. The bit is a data type.
What is a bit?
• Binary Digit = bit
• It is symbolized by the numbers 0 or 1.
• When you use a bit to count, you use a twostate numbering system.
• Binary is a BI (two) state numbering (nary)
system
What is Binary?
•
•
•
•
Two numbered system
Generally S={0,1}
00, 01, 10, 11 (binary)
0, 1, 2, 3 (decimal)
What is Hexidecimal?
• 16 symbols in a numbering system.
• Generally S={0,1,2,3,4,5,6,7,8,9,A,
B,C,D,E,F}
• (DEAD) base 16 (hexidecimal)
• (CAB)
• (7F)
• (FF)
What is Octal?
•
•
•
•
•
•
•
A Base 8 numbering system
8 symbols
generally S={0,1,2,3,4,5,6,7}
(177) (10)
(7) + (1) = (10) base 8
(10)+(7)=(17) base 8
(17)+(1)=(20) base 8
What is a base?
• The number of symbols in a numbering
system.
• Radix of a numbering system is the base
Why do they call it the base?
•
•
•
•
In decimal we write: 42.
42 = 4 * 10**1 + 2*10**0
In binary we write: 1101
1101 = 1 * 2 ** 3 + 1 *
2**2+0*2**1+1*2**0=8 + 4 +1=13 base
10
What is a primitive data type?
• Is a built in (to the language) data type.
• That means that the language comes with
the primitive data type.
Why do I need a primitive data
type?
• Holds some value represented by bits in
computer memory.
• It helps with computations by storing data.
What is an example of a PDT?
• boolean – true, false.
–
–
–
–
–
–
boolean b = true;
boolean t = false;
boolean theChairsAreHard = true;
boolean theExampleIsGettingSilly = true;
The = sign means assignment.
Variables are on the left hand side of the = sign.
There are 8 primitive data types.
•
•
•
•
boolean
char
SignedFixedPoint (byte, short, int, long)
SignedFloatingPoint (float, double)
Two types of primitive data types
• Signed
• Unsigned
Two types of unsigned data types
• boolean
• char
– example:
• char c = ‘t’;
• char qwerty = ‘q’;
• char thisIsGettingSilly = ‘w’;
What is a Java statement?
• Some Java code followed by a ‘;’
– char fooBar = ‘e’;
How many fixed point PDTs are
there?
•
•
•
•
•
4, byte, short, int, long
byte uses 8 bits to store values.
short uses 16 bits
int uses 32
long uses 64
What are some examples?
•
•
•
•
•
byte b = 127;
byte b = 128; // does not work!
bytes range from –128...127
The most significant bit is the sign bit.
byte thisIsGettingSilly = -128;
How do I use the short?
• short thisIsShort = 32767; //max value
• short thisisTheSmallestShort = -32768;
How do I use the int?
• int whatWouldYouDoWithoutInts = 20;
• int thisIsTheBiggestInt = 2147483647;
• int thisIsTheSmallestInt = -2147483648;
How do I add numbers?
•
•
•
•
•
int i = 2;
int j = 3;
int k = i + j; //k becomes 5.
Comments are written using “//”
// this is a comment.
How do I subtract numbers?
• float f1 = 2.5;
• float f2 = 3.4;
• float f3 = f1 - f2; // f3 = - 0.9
How do I multiply?
•
•
•
•
double d = 2;
double thisIsTheSecondDouble = 10;
double a = d * thisIsTheSecondDouble;
a’s value is: 20
How do I divide?
•
•
•
•
•
short w = 5;
short r = 2;
short q = w/2; // 2
The 2.5 can’t fit in a fixed point number.
It always truncates the part after the decimal
point.
How do I control the order of
Evaluation?
•
•
•
•
•
•
a = b * c + d;
how do I evaluate c+d first?
use a = b * (c + d);
d = w / (q + r);
d = 2 /(1 + 3) = 0.5
d = 2/1 + 3 = 5
How do I print a random
number?
• System.out.println(Math.random());
• will output a number that ranges from 0 to
1.
Example
What is positional notation?
• It is a system that multiples a symbol by the
base raised to an exponent that represents
the symbols position.
What is an Operator?
• A symbol that performs the invocation of
some method that manipulates some
arguments.
–
–
–
–
double a = b + c; // + is the addition operator
double d = e – f; // - is the sub op.
double f = f / 2.0; // / is the division op.
= represents the assignment op.
Why do I need operators?
• It makes it easier to do computations.
What kind of ops are there?
• Binary ops, take two arguments.
–
–
–
–
–
a = a + b;
a = a – b;
a = a / b;
a = a * b;
A.set(a.mul(b)); // a = a * b
Unary operators
•
•
•
•
•
•
a++ is the same as a = a + 1;
a– is the same as a = a – 1;
a +=25; // a = a + 25;
a -= 25; // a = a – 25;
a /= 4; // a = a / 4;
a *= 3; // a = a * 3;
What is a truth Table?
• Shows the results of
doing ops on a list of
all the binary
combinations.
• Count in binary to
create the input.
• apply a boolean alg.
function to create the
output.
x
y
0
0
1
1
x AND y
0
1
0
1
0
0
0
1
Bitwise Op.
• AND, OR, NOT, XOR
x
y
0
0
1
1
x AND y
0
1
0
1
0
0
0
1
AND truth table
in Boolean Logic
x
x AND y
y
An Inclusive OR
• OR truth table in the Boolean Alg.
y
x
x
y
0
0
1
1
x OR y
0
1
0
1
0
1
1
1
XOR
• X exclusive OR Y
x
y
0
0
1
1
x XOR y
0
1
0
1
0
1
1
0
Doing bitwize Ops in Java
•
•
•
•
•
•
•
int x = y & z; //AND
int x = y | z; // OR
int x = y ^ z; // bitwise xor function.
int x = ~ y // bitwise negation
Use && when the args are of type boolean
Use || when the args are of type boolean
Use ! For inverting a boolean value
What is a boolean OP?
• AND, OR, NOT
• The output is true or false
– && // boolean AND (not bitwise)
– || // boolean OR
– ! // boolean NOT
What are some examples?
• a = b && c; //a is boolean, so is b and C
• a = b || c;
• a = ! (b || c);
What is an Augmented Operator?
• A short cut (that sometimes is unreadable!).
i *= 10
i = i * 10;
i -= 10
i = i – 10;
i +=10
i = i + 10;
i >>=3
i = i >> 3;
i ^=9
i = i ^ 9;
Unary operators are augmented