Download the Chapter 1 PowerPoint slides - Bobby Hoggard

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 1
INTRO TO JAVA
© Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission
Computer Basics
CPU (Central Processing Unit / Processor)
◦ Executes instructions in a computer program
◦ Moves data from place to place
◦ Performs arithmetic and logic operations on the data
Primary memory (main memory / RAM)
◦ Working memory
◦ Holds the currently executing program and the data used by that program
◦ Is volatile (loses contents when power is removed)
Secondary memory (Hard drive, USB drive, CD/DVD, magnetic tape, etc.)
◦ Non-volatile (retains contens without power)
◦ Used for backing up contents of main memory
RAM Memory Structure
RAM is a linear list of memory cells
Address – location of the memory cell
0
5
1
R
2
7
3
M
4
…
154
Data - contents of the memory cell
1 memory cell = 1 byte of memory
1 byte = 8 bits
1 0 0 1 1 0 1 0
1 bit = a single 0 or 1
1
A bit is the smallest unit of storage
A byte is the smallest unit of
addressable storage
Creating Programs
Type your program into the computer
using the computer’s keyboard
The program is stored immediately and
directly into RAM as you type
SAVE
The program is copied onto the
hard drive for more permanent storage
Executing Programs
The program is stored
on the hard drive
The program is loaded into
RAM so it can execute
LOAD
Program may be designed
to save output back to the
hard drive
Program input is usually
supplied from the keyboard
Program output is usually
displayed on the screen
Algorithms
◦ A set of steps to solve a problem
◦ Should be unambiguous
◦ Can be written in any language
◦ English
Algorithms
◦ A set of steps to solve a problem
◦ Should be unambiguous
◦ Can be written in any language
◦ English
◦ Pictures
Algorithms
◦ A set of steps to solve a problem
◦ Should be unambiguous
◦ Can be written in any language
◦ English
◦ Pictures
◦ Flowchart
Calculate the sum of a list of numbers.
If the sum is greater than zero, output
“it’s positive”, otherwise output
“it’s zero or less”.
set total to zero
get list of numbers
loop through each number in the list
add each number to total
end loop
if number more than zero
print “it’s positive” message
else
print “it’s zero or less” message
end if
Algorithms
◦ A set of steps to solve a problem
◦ Should be unambiguous
◦ Can be written in any language
◦ English
◦ Pictures
◦ Flowchart
◦ Pseudocode
Algorithms vs. Programs
ALGORITHM
COMPUTER PROGRAM
A set of steps to solve a problem
An algorithm translated into a language
understood by the computer
Should be unambiguous
Can be written in any language
◦
◦
◦
◦
English
Pictures
Flowchart
Pseudocode
◦
◦
◦
◦
◦
◦
◦
Java
C, C++, C#
Visual Basic
Prolog
Pascal
Python
Etc.
Algorithm
Computer Program
public class Numbers {
public static void main(String [] args) {
set total to zero
int total = 0;
get list of numbers
int [] numbers = getNumbers();
loop through each number in the list
add each number to total
end loop
for (int x = 0; x < numbers.length; x++)
total = total + numbers[x];
if total more than zero
print “it’s positive” message
else
print “it’s zero or less” message
end if
if (total > 0)
System.out.println(“It’s positive”);
else
System.out.println(“It’s zero or less”);
}
}
Levels of Languages
Easy for Computer
Hard for Computer
Hard for Humans
Easy for Humans
1st Generation
Machine
Language
Consists of only
0s and 1s
Ex:
Intel
Machine
Language
2nd Generation
3rd Generation
4th Generation
5th Generation
st
1
Generation Example Program
000010100010
Notes:
000010110101
• Easy to make a mistake in writing / copying
001110101011
• Don’t really know what this program does
001011111010
• Only way to read this is to refer to a chart
to determine the commands and their structure
Levels of Languages
Easy for Computer
Hard for Computer
Hard for Humans
Easy for Humans
1st Generation
2nd Generation
Machine
Language
Assembly
Language
Consists of only
0s and 1s
Uses words to
represent
commands and
memory
locations, and
also uses
numbers
Ex:
Intel
Machine
Language
Ex: Intel x86 Assembly
3rd Generation
4th Generation
5th Generation
st
1
Generation vs
1st Generation
nd
2
2nd Generation
000010100010
Generation
Notes:
LOAD AX, 2
000010110101
LOAD BX, 5
001110101011
ADD AX, BX
001011111010
STORE Y, AX
• Easier to figure out what the program is
doing
• Still need a reference chart for command
structure, and certain named locations
• Must be translated to 1st generation
by a program called an assembler
Assembler
• One-to-one translation
Levels of Languages
Low-Level Languages
High-Level Languages
Machine Dependent
Machine Independent
1st Generation
2nd Generation
3rd Generation
Machine
Language
Assembly
Language
Uses structure
and grammar to
infer commands
Consists of only
0s and 1s
Uses words to
represent
commands and
memory
locations, and
also uses
numbers
Ex:
Intel
Machine
Language
Ex: Intel x86 Assembly
You say what
you want to do
and specify
exactly how it
will be done
Ex: Java, C, C++,
BASIC, Pascal, etc.
4th Generation
5th Generation
st
1
vs
nd
2
vs
rd
3
1st Generation
Generation
Notes:
2nd Generation
000010100010
LOAD AX, 2
000010110101
LOAD BX, 5
001110101011
ADD AX, BX
• Can usually understand
the program without a
chart
Java
3rd Generation
001011111010
STORE Y, AX
Assembler
Compiler / Interpreter
Y=2+5
• Must be translated to
1st generation by a
compiler or an
interpreter
• One 3rd generation line
is often equivalent to
several 1st gen lines
Levels of Languages
Low-Level Languages
High-Level Languages
Machine Dependent
Machine Independent
1st Generation
2nd Generation
3rd Generation
4th Generation
Machine
Language
Assembly
Language
Uses structure
and grammar to
infer commands
Uses structure
and grammar to
infer commands
Consists of only
0s and 1s
Uses words to
represent
commands and
memory
locations, and
also uses
numbers
You say what
you want to do
and specify
exactly how it
will be done
You say what
you want to do
(but how how)
The language
figures out how
it will be done
Ex:
Intel
Machine
Language
Ex: Intel x86 Assembly
Ex: Java, C, C++,
BASIC, Pascal, etc.
Ex: Prolog, SQL
5th Generation
th
4
Generation
Example:
Find the names of all
customers with an
outstanding balance
SELECT Name
FROM Customers
WHERE Balance > 0
Notes:
• Extremely easy to read
• The program describes what needs to be done, but does not
specify how to do it
• Notice that you don’t get to specify how the results will
be displayed
• An equivalent 3rd generation program would be fairly long
rd
3
vs
th
4
Generation
4th Generation
SELECT Name
FROM Customers
WHERE Balance > 0
Example:
Find the names of all
customers with an
outstanding balance
3rd Generation Pseudocode
1.
2.
3.
4.
5.
6.
7.
8.
Create and empty list to hold the results
Open the customers file
Read the first customer name and balance
If the balance > 0, add customer name to result list
Read the next customer
Repeat steps 4 – 6 until the end of file is encountered
Close the customers file
For each name in the results list:
a. Print the name on the screen
b. Print a line break
Levels of Languages
Low-Level Languages
High-Level Languages
Machine Dependent
Machine Independent
1st Generation
2nd Generation
3rd Generation
4th Generation
5th Generation
Machine
Language
Assembly
Language
Uses structure
and grammar to
infer commands
Uses structure
and grammar to
infer commands
Natural
Language
Consists of only
0s and 1s
Uses words to
represent
commands and
memory
locations, and
also uses
numbers
Ex:
Intel
Machine
Language
Ex: Intel x86 Assembly
You say what
you want to do
and specify
exactly how it
will be done
Ex: Java, C, C++,
BASIC, Pascal, etc.
You say what
you want to do
(but how how)
The language
figures out how
it will be done
Ex: Prolog, SQL
Just speak
naturally, and
the computer
figures out
what you want
& how to do it
Ex: English, Spanish
th
4
vs
th
5
4th Generation
SELECT Name
FROM Customers
WHERE Balance > 0
Generation
Example:
Find the names of all
customers with an
outstanding balance
5th Generation
Display the names of all customers
with an outstanding balance.
Compilers vs. Interpreters
COMPILER
INTERPRETER
Translates your program into ML once
Translates your source code every time the
program executes
When finished, you will have two copies of
your program:
◦ One in the original language (source code)
◦ A copy in machine code (executable code)
Will need to retranslate again only if the
source code changes
Your client does not need a compiler
You never receive a machine language copy
You must distribute your source code
Your client needs a copy of the interpreter, in
addition to a copy of your source code
Program Development with Compilers
Data
C++
(Source
Code)
Compiler
Machine
Language
Computer
(Executable
Code)
Output
Program Development
Program Execution
Program Distribution with Interpreters
Interpreter for
Mac
Interpreter for
Windows
BASIC
BASIC
(Source
Code)
(Source
Code)
Windows
Computer
Output
Data
Mac
Computer
Output
Program Development
Program Execution
Program Execution
Data
Program Distribution with Compilers
C++
Code
Compiler for
Windows
Windows
Machine Code
Compiler for
Mac
Mac
Machine Code
Compiler for
Linux
Linux
Machine Code
Program Development
Program Execution
Java Is Both Compiled And Interpreted
Java
Interpreter
JRE
Java Runtime
Environment
Java
(Source
Code)
Java
Compiler
Java
Bytecode
JDK
Java Development Kit
Computer
Output
Program Development
Program Execution
Data
Types of Java Programs
Applications
Applets
Stand-alone desktop programs
Programs designed to execute inside a web
browser
Java is an Object-Oriented Language
Objects are “things”
◦ Objects hold data
◦ The contain blocks of code to manipulate the data
Objects are abstract
◦ Do not have to know how they work in order
to use them
◦ Can be reused in other programs
Variables
Named memory locations
◦ We choose an appropriate name
◦ We specify the type of data it will store
Must be declared before we use them
◦ This lets the system know about your variable name
and type of data
Java is a strongly-typed language
◦ The type of data you put into a variable must
match the variable’s declared type
Variable Types
Primitives
Objects
Store only one piece of data
Store one or more pieces of data
Contains methods to operate on the data
hours
student
40
Name
John
GPA
3.7
Hours
32
Java Program Format
// Your Name
import statements
Comment: the compiler ignores everything after the //
Optional. Used to include other code to help you. Example keyboard processing
public class NameOfProgram {
NameOfProgram must match the filename  NameOfProgram.java
public static void main( String [] args ) {
Starting section of your program code.
This is the piece that begins executing first.
your code goes here
}
}
Everything inside brackets should be indented with a TAB character
Closing brackets should align with the section of code that they close
Items Found in a Typical Java Program
Variable Declaration
dataType variableName;
Using Objects
objectName.fieldname
references data value inside objectName
dataType = what type of data will
will you be storing
variableName = most any name of your choosing
Typical Objects
System.out
object used to represent the screen
System.in
object used to represent the keyboard
objectName.methodName()
references code block inside objectName
Storing Values into Variables
variableName = value;
stores value into variableName
always works right to left
// Author: Bobby Hoggard
Add your name at the top of every program
import java.util.*;
Include code to help make keyboard input easier
public class FirstProgram {
Must be stored in a file called FirstProgram.java
public static void main(String[] args) {
}
}
Declares the starting section of code
Scanner kbd = new Scanner(System.in);
int n1, n2, answ;
Initialize the keyboard / call it “kbd”
Declare variables to hold 3 whole numbers
System.out.println("I will add two numbers");
System.out.println("Enter two numbers:");
Print words to the screen
Print a second line of words to the screen
n1 = kbd.nextInt();
n2 = kbd.nextInt();
answ = n1 + n2;
Get a whole number from keyboard / store into n1
Get a whole number from keyboard / store into n2
Calculate n1 + n2 / Store answer into answ
System.out.println("The sum is:");
System.out.println( answ );
Print words to the screen
Print the value in the variable answ, to the screen
kbd.close();
Close the keyboard connection