Review Slides - University of Hawaii

Download Report

Transcript Review Slides - University of Hawaii

Midterm Review
ICS 111
Cam Moore
Information and Computer Sciences
University of Hawaii, Manoa
(1)
Computer Fundamentals
Von Neumann Architecture
Central Processing Unit
Control Unit
Arithmetic/Logic Unit
Input
Device
Output
Device
Memory Unit
Fetch, Execute cycle
(2)
Two Types of Languages
Machine Language
• Binary
• Specific to CPU
High-level Programming Languages
• Human readable
• Java, Fortran, C++, COBOL, LISP, etc
• Must be converted to Machine Language
- Compiler
- Interpreter
(3)
High-Level Programming
Languages
Syntax
• Strict rules about what is and isn’t allowed
Semantics
• The meaning of the program (What it does)
(4)
Building Blocks of Programs
Data – Variables
• Names that refer to memory locations
• Typed, what they can hold
• Can change value by assignment =
Instructions
• Sequence of execution steps
• Control Structures – loops and branches
• Subroutines – named “chunks” of code
(5)
Java Syntax
Comments
• Ignored by the computer
• Very important
• //
- Single line
• /* … */
- Multi line
• /** … */
- Javadoc
(6)
/**
* A program to display the message
* "Hello World!" on standard output.
* @author Cam Moore
*/
public class HelloWorld {
/**
* Prints out Hello World!
* @param args not used.
*/
public static void main(String[] args) {
System.out.println("Hello World!");
}
} // end of class HelloWorld
Variables
Program data is stored in memory
Variables
• Variables are not the data, but
the location of the data
• Variables have types
<variable> = <expression>
rate = 0.07;
interest = rate * principal;
(7)
10001010
00110100
01110111
10100100
11010010
10000110
01001111
10100000
00000010
10100010
00010100
11111010
10100000
Location 0
Location 1
Location 2
Location 3
10100000
x
Location 4
Location 5
Location 6
Location 7
Location 8
Location 9
Location 10
Location 11
Location 12
...
• Assignment statements
Memory
Location N
interest
Java Primitive Types
Eight primitive types
• byte
• short
• int
• long
• float
• double
• char
• boolean
(8)
Whole Numbers
Floating Point Numbers
Single Character (Unicode)
true or false
Precedence Rules
Highest to lowest Precedence
(9)
Operator Type
Examples
Parentheses
()
Unary operators
++, --, !, unary -, unary +, type-cast
Multiplication and Division
*, /, %
Addition and Subtraction
+, -
Relational operators
<, >, <=, >=
Equality and Inequality
==, !=
Boolean And
&&
Boolean Or
||
Ternary
?:
Assignment operators
=, +=, -=, *=, /=, %=
Unary and assignment operators: right-to-left
Rest: left-to-right
Blocks, Loops and Branches
Control Structures
• Control the flow of programs
block
while
do..while
for
if
switch
(10)
Blocks
Group statements together
{
<statements>
}
Pair of curly braces { }
Sequence of statements
• 0 or more statements
Doesn’t affect flow of control
(11)
Defines scope
Variable Scope
Variable Scope:
• Where a variable is usable
Variables are defined in a block
• Usable inside that block and all
sub-blocks
{
int x = 3;
...
{
char c = ‘!’;
...
}
...
{
double c = 3e8;
...
}
...
• Not available outside the block
}
(12)
While Loop
Repeat statements over and over
while (<boolean-expression>)
<statement>
while (<boolean-expression>) {
<statements>
}
Will loop while boolean-exression is true
• Can lead to infinite loops
int number = 1;
while (number < 6) { // Keep going as long as number < 6
System.out.println(number);
number = number + 1; // Go on to next number
}
(13)
do..while Loop
Similar to the while loop
do
<statement> // body of the loop
while (<boolean-expression>);
do {
<statements> // body of the loop
} while (<boolean-expression>);
The body of the loop will run at least once
(14)
The for Loop
for (<initialization>; <boolean-expression>; <update>)
<statement> // body of the loop
for (<initialization>; <boolean-expression>; <update>) {
<statements> // body of the loop
}
initialization done once
boolean-expression is evaluated to terminate
loop
update is done each time through the loop
<initialization>;
while(<boolean-expression>) {
<statements> // body of the loop
<update>;
}
(15)
Which Loop to Use
Know how many time to loop (counting)
• for loop
Don’t know how many times
• while loop
Run the body at least once
• do…while loop
(16)
if Statement
if (<boolean-expression>)
<statement-1>
else
<statement-2>
if (<boolean-expression>) {
<statements-1>
}
else {
<statements-2>
}
(17)
Two way branching
• statement(s)-1 executed if boolean-expression
is true
• statement(s)-2 executed if boolean-expression
is false
Multiway Branching
if (<boolean-expression-1>) {
<statements-1>
}
else if (<boolean-expression-2>) {
<statements-2>
}
else {
<statements-3>
}
Three-way branch
• only one of the statements will execute
- expression 1: true => statements-1
- expression 1: false
– expression 2: true => statements-2
– expression 2: false => statements-3
(18)
switch Statement
Second branching statement
switch (<expression>) {
case <constant-1>:
<statements-1>
break;
case <constant-2>:
<statements-2>
break;
.
. // (more cases)
.
case <constant-N>:
<statements-N>
break;
default: // optional default case
<statements-(N+1)>
} // end of switch statement
(19)
Java Exceptions
Java exceptions are represented by objects of
type Exception
Many different subclasses of Exception
• NullPointerException
• IllegalArgumentException
• NumberFormatException
String str = “42”;
int x = Integer.parseInt(str);
str = “fred”;
x = Integer.parseInt(str);
The program “throws” the exception
(20)
Dealing with Exceptions
try..catch
try {
<statements-1>
}
catch (<exception-class-name> <variable-name>) {
<statements-2>
}
If statements-1 throws an exception of type
exception-class-name control jumps to
statements-2
Else statements-2 is skipped
(21)
Arrays
Very basic data structure
Data structures are data items chunked together
Arrays:
• Items are arranged as numbered sequence
- length
- index starts at 0
• All the same type
(22)
Array Variables
String[] nameList;
int[] A;
double[] prices;
Arrays use []
nameList[7];
A[0] = 13;
prices[prices.length – 1];
Creating Arrays
nameList = new String[1000];
A = new int[5];
prices = new double[100];
<array-variable> = new <base-type>[<array-length>];
(23)
Subroutines
Allow us to handle complex programs
Consists of instructions for a task
• Grouped together
• Named
Can be called by the program
Can be called by other subroutines
Build up the complex solution
(24)
Subroutine Definitions
<modifiers> <return-type> <name> ( <parameter-list> ) {
<statements>
}
modifiers
• “static” and “public”
return-type
• The type of the returned value or void
parameter-list
• Information passed into the subroutine
• <type> <parameter-name> pairs separated by
commas
(25)
Access Specifiers
public
• Usable by all
none
• Usable by “package” classes
private
• Usable only by the same class
Choose either public or private*
(26)
Calling Subroutines
For static subroutines
• in the same class
<subroutine-name>(<parameters>);
• in a different class
<class-name>.<subroutine-name>(<parameters>);
(27)
Parameters
Mechanism for passing information to
subroutines
Part of the interface
• Type
• Number
• Order
Get values from outside the subroutine
(28)
Formal and Actual Parameters
Formal
• Parameters in subroutine definition
• Type
• Name
public static void doTask(int N, double x, boolean test) {
// statements to perform the task go here
}
Actual
• Values set by calling subroutine
doTask(17, Math.sqrt(z + 1), z >= 10);
(29)
Three Types of Variables
Local
• Declared inside a subroutine
• Available to the subroutine only
Parameters
• Set outside the subroutine
• Like local variables in the subroutine
Global
• Declared outside subroutines
• Available to all*
(30)
Return Values
Subroutines that returns a value is called a
function
Functions can only return a value of a specified
type
<modifiers> <return-type> <name> ( <parameter-list> ) {
<statements>
}
Java uses the return statement
return <expression>;
The type of the expression must match the
function definition
(31)
Java Packages
Java groups classes into packages
Packages can contain
• Classes
• Other packages “sub-packages”
Two major packages
• java
• javax
(32)
Using Classes from Packages
Two ways to use a class from another package
• Use the full name of the class
java.awt.Color rectColor;
• Import the class
package edu.uhm.ics111;
import java.awt.Color;
public class Example {
private static Color rectColor;
. . .
}
(33)