Presentation is loading. Please wait.

Presentation is loading. Please wait.

Information and Computer Sciences University of Hawaii, Manoa

Similar presentations


Presentation on theme: "Information and Computer Sciences University of Hawaii, Manoa"— Presentation transcript:

1 Information and Computer Sciences University of Hawaii, Manoa
Midterm Review ICS 111 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

2 Computer Fundamentals
Von Neumann Architecture Fetch, Execute cycle Input Device Output Central Processing Unit Control Unit Arithmetic/Logic Unit Memory Unit

3 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

4 High-Level Programming Languages
Syntax Strict rules about what is and isn’t allowed Semantics The meaning of the program (What it does)

5 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

6 Java Syntax Comments Ignored by the computer Very important //
Single line /* … */ Multi line /** … */ Javadoc /** * A program to display the message * "Hello World!" on standard output. Cam Moore */ public class HelloWorld { * Prints out Hello World! args not used. public static void main(String[] args) { System.out.println("Hello World!"); } } // end of class HelloWorld

7 Variables Program data is stored in memory Variables
Variables are not the data, but the location of the data Variables have types Assignment statements Memory Location 0 Location 1 Location 2 Location 3 Location 4 Location 5 Location 6 Location 7 Location 8 Location 9 Location 10 Location 11 Location 12 Location N . . . x interest <variable> = <expression> rate = 0.07; interest = rate * principal;

8 Java Primitive Types Eight primitive types byte short int long float
double char boolean Whole Numbers Floating Point Numbers Single Character (Unicode) true or false

9 Precedence Rules Highest to lowest Precedence
Unary and assignment operators: right-to-left Rest: left-to-right 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 =, +=, -=, *=, /=, %=

10 Blocks, Loops and Branches
Control Structures Control the flow of programs block while do..while for if switch

11 Blocks Group statements together Pair of curly braces { }
Sequence of statements 0 or more statements Doesn’t affect flow of control Defines scope { <statements> }

12 Variable Scope Variable Scope: Where a variable is usable
Variables are defined in a block Usable inside that block and all sub-blocks Not available outside the block { int x = 3; ... char c = ‘!’; } double c = 3e8;

13 While Loop Repeat statements over and over
Will loop while boolean-exression is true Can lead to infinite loops while (<boolean-expression>) <statement> while (<boolean-expression>) { <statements> } 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 }

14 do..while Loop Similar to the while loop
The body of the loop will run at least once do <statement> // body of the loop while (<boolean-expression>); do { <statements> // body of the loop } while (<boolean-expression>);

15 The for Loop initialization done once
boolean-expression is evaluated to terminate loop update is done each time through the loop for (<initialization>; <boolean-expression>; <update>) <statement> // body of the loop for (<initialization>; <boolean-expression>; <update>) { <statements> // body of the loop } <initialization>; while(<boolean-expression>) { <statements> // body of the loop <update>; }

16 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

17 if Statement Two way branching
statement(s)-1 executed if boolean-expression is true statement(s)-2 executed if boolean-expression is false if (<boolean-expression>) <statement-1> else <statement-2> if (<boolean-expression>) { <statements-1> } else { <statements-2>

18 Multiway Branching Three-way branch
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

19 switch Statement Second branching statement
switch (<expression>) { case <constant-1>: <statements-1> break; case <constant-2>: <statements-2> . . // (more cases) case <constant-N>: <statements-N> default: // optional default case <statements-(N+1)> } // end of switch statement

20 Java Exceptions Java exceptions are represented by objects of type Exception Many different subclasses of Exception NullPointerException IllegalArgumentException NumberFormatException The program “throws” the exception String str = “42”; int x = Integer.parseInt(str); str = “fred”; x = Integer.parseInt(str);

21 Dealing with Exceptions
try..catch If statements-1 throws an exception of type exception-class-name control jumps to statements-2 Else statements-2 is skipped try { <statements-1> } catch (<exception-class-name> <variable-name>) { <statements-2>

22 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

23 Array Variables Arrays use [] Creating Arrays String[] nameList;
int[] A; double[] prices; nameList[7]; A[0] = 13; prices[prices.length – 1]; nameList = new String[1000]; A = new int[5]; prices = new double[100]; <array-variable> = new <base-type>[<array-length>];

24 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

25 Subroutine Definitions
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 <modifiers> <return-type> <name> ( <parameter-list> ) { <statements> }

26 Access Specifiers public Usable by all none
Usable by “package” classes private Usable only by the same class Choose either public or private*

27 Calling Subroutines For static subroutines in the same class
in a different class <subroutine-name>(<parameters>); <class-name>.<subroutine-name>(<parameters>);

28 Parameters Mechanism for passing information to subroutines
Part of the interface Type Number Order Get values from outside the subroutine

29 Formal and Actual Parameters
Parameters in subroutine definition Type Name Actual Values set by calling subroutine public static void doTask(int N, double x, boolean test) { // statements to perform the task go here } doTask(17, Math.sqrt(z + 1), z >= 10);

30 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*

31 Return Values Subroutines that returns a value is called a function
Functions can only return a value of a specified type Java uses the return statement The type of the expression must match the function definition <modifiers> <return-type> <name> ( <parameter-list> ) { <statements> } return <expression>;

32 Java Packages Java groups classes into packages Packages can contain
Other packages “sub-packages” Two major packages java javax

33 Using Classes from Packages
Two ways to use a class from another package Use the full name of the class Import the class java.awt.Color rectColor; package edu.uhm.ics111; import java.awt.Color; public class Example { private static Color rectColor; . . . }


Download ppt "Information and Computer Sciences University of Hawaii, Manoa"

Similar presentations


Ads by Google