Lecture 2, CS 302-6, September 7
- Review
- Last
time - we went over syllabus, talked about computer science, and what an
algorithm is
- What
is an algorithm? Sequence of
steps for doing something.
Basically, a recipe
i.
Unambiguous
ii.
Executable
iii.
Terminating
- Pseudocode
– language for writing algorithms
- What
is a program? Sequence of
instructions and decisions.
- Steps
to writing a program (textbook):
i.
Understand the problem
ii.
Develop and describe an algorithm
iii.
Test the algorithm with simple inputs
iv.
Translate the algorithm into java
v.
Compile and test your program
- Intro
to Java
- Java
is a more formal way of instructing computers how to do something
- Java
is one example of a programming language
- As
stated before, computers only do exactly what you tell them too
- Java
is a language that a computer can understand…kind of. It is what is called a high level
language.
- History
of Java:
i.
Started in 1991, released 1995
ii.
Released by sun Microsystems
iii.
Current stable release – version seven
iv.
Very often used in web applets
v.
Open source
- Note
– if you want to use java on your home computer, you will need to
download the Java Software Development Kit (SDK) – JRE (Java Runtime
Environment) is not enough
- Machine
Code
- The
above is kind of a misnomer – the computer still doesn’t understand java.
- What
does a computer understand?
Machine Code. These are
very low-level instructions – basically, just patterns of bits.
- The
processor of the computer is wired so that it understands these commands
– circuits
- What
is a bit? 0 or 1.
- So,
32 bit - this is what it means when you hear about 32 bit, 64 bit
Windows. Each instruction
consists of 32 0’s and 1’s. This
is directly readable by the computer’s processor.
i.
Machine code has instructions like add, subtract, binary
operators (AND, OR)
- Want
to know more? Take a computer
architecture course.
- Compilers
- Many
programming languages are compiled directly into machine code. This is done by a compiler.
- Basically,
a compiler is a computer program that converts a high level language into
machine code. Note – I will not
use Java as an example here.
Instead, I will use C.
- Say
you write a C program. You’ll
have to run a command to compile it.
If you want to run it on a Windows computer, you’ll have to use
one Compiler to compile it. If
you want to run it on a Mac, you’ll need to use a different Compiler to
compile it.
- Interpreted
Languages
- I
mentioned that Java is not an example of the above. Why?
Java is what is called an interpreted language.
- The
Java compiler does not turn the java program into machine code. Instead, it turns it into java byte
code. Byte code is an
intermediate form of computer code – it is lower level than Java, but
higher level than machine code.
- When
you download a java program, this is what you download.
- Your
computer has a program installed on it called the Java Virtual
Machine. When you run a java
program, this converts the Byte Code into Machine Code at runtime, to
make it readable by the computer.
- This
means that the same java program can run across platforms – Mac, Windows,
etc. They just need the correct
JVM.
- Want
to know more? Take a programming
languages class
- So,
why Java?
- Error
Reporting
- Portability
- - trade off! This makes Java
slower than a compiled language
- Ease
of programming
- Important/useful
language
- Java
class library
- Programming
environments
- Eclipse
i.
This is an IDE – Integrated Development Environment
ii.
Features – in the same screen, you can write code, display
results, compile, see tree structure, etc.
Also annotates code for correctness, kind of like spell check
- Console/text editor
i.
Compile via
console, edit in text editor
- You’ll
be using Eclipse in labs, so I’ll demonstrate with that
- Java files
- Source.java
- Source.class
- .java
is java code, .class is java byte code
- Hello
World
- Class
– fundamental building blocks of java programs, more details later
- Main
method – starting point for program.
When you execute a program, it begins executing instructions
here. Introduce the idea of a
thread
- Other
methods – can do other things.
More later
- Statement;
- Some
hand-waving here – public static void main(String[] args)
i.
Main is where the program starts
- Note
– semi colons, curly brackets, etc
- Strings
- text
- HW:
- R1.11, R1.12, R1.18, R1.16
- R1.6, R1.7, R1.8