Final_Exam_Review

advertisement
Final Exam Study Guide
For detals about any of these topics consult the CIS 103 Study Guide
Basic functions of a computer
move/copy data between memory and accumulators
performing arithmetic operations
performing relational operations
performing logical operations
performing input/output operations
Control Structures
 Sequence
 Selection single-alternative – if / then OR dual-alternative – if / then / else
 Loop ( aka repetition and iteration )
o
stacking / nesting when using selection or loop control structures
o
pre-test, mid-test, post-test loop
o
counter-controlled
aka
definite
o
sentinel-control
aka
indefinite
 single entry / single exit
Data Hierarchy
file
a collection of records
record
a collection of fields
field
a collection of characters or digits that make up a data item ( name, number, description, etc )
characters A-Z, a-z, 0-9, special characters
Program translation
compiler compiles source statements in one language to equivalent statements in another language
such as: Java source code to bytecode
C++ source code to an executable file (.exe)
interpreter translates a source statement, then executes the statement. The Java Virtual Machine is an
interpreter. It translates bytecode into a machine language statement.
Program Documentation (Java comments)
//
this is a single-line comment
/*
this is a
multi-line comment
*/
Program Errors
syntax
invalid statement (also known as a compile-time error)
logic
incorrect output form a program ( such as an error in a calculation statement )
run-time division by zero, file unavailable or corrupted, memory protection violation, array out of bounds
Charts / Forms / Pseudocode
Hierarchy Chart
shows high-level processing and the relationship of modules in a program
Flowchart
used to develop the logic of an algorithm
Pseudocode
used to develop the logic of an algorithm
Program Structure
one module (often called "main") - very simple procedural program
multiple modules ( methods in Java )
 abstraction
 task
 task is expressed using an algorithm
 functional cohesion
1
Flowchart Symbols: terminal, process, predefined process, input/output, decision, annotation, flowlines,
connector
RAPTOR flowchart symbols:
assignment, call, input(get), output(put), selection, loop
Modules
File Processing - 4 main modules ( module names may be different... )
1. main( )
//”mainline program logic”
2. initalize( )
//initialization activities
3. processRecord( ) //process all the records in the file using a sentinel controlled loop
4. terminate( )
//final activities
Operators
 unary / binary ( one operand
or two operands )
 mathematical, relational comparison, logical operators
 assignment
 precedence
 associativity left-to-right or right-to-left
Precedence of Operators ( from highest to lowest )
 mathematical

relational comparison

logical

assignment
Java data types
Numeric
integral
 byte
 short
 int
 long
floating-point
 float
 double
Alphanumeric
 char
 String
Logical
 boolean
negative and positive whole numbers and 0
Examples
-5, 1000, 0
32-bit integer
real numbers ( numbers with decimal digits )
64-bit floating-point
letters, digits, special characters
Examples
Examples
12.5, 6.0, -7.222
"1234 Oak Street”, ‘c’ , “12.5%”
( A String is NOT a primitive type in Java, it is a reference type )
yes, no, true, false, 1, 0 binary choices
The Java boolean type only accepts the predefined literals true and
Java Arithmetic Operators
+, -, *, /, % ( add, subtract, multiply, divide, modulus [integer remainder] )
Java Relational Comparison Operators
<, <=, >, >=, == (equal to), != (not equal to)
To compare Strings in Java, use the equals( ) method or the compareTo( ) method
Examples:
name.equals(“Jack”)
if ( ! scoutName.equals(holdName) )
name1.compareTo(name2)
The result of calling the compareTo method is
 0
equal comparison
 negative
name1 < name2
 positive
name1 > name2
Strings may be concatenated in Java using the "+" operator: name + " " + id + " " + boxesSold …
System.out.println(name + "
" + id + "
" + boxesSold + "
" + sales);
2
false
Java Logical Operators
Perform logical operations which return a boolean value (true or false)
Java logical operators in order from highest to lowest precedence
 !
Logical NOT
 &&
Logical AND
 ||
Logical OR
 short-circuit boolean evaluation
o OR
first conditions evaluates to true
o AND
first condition evaluates to false
Java Assignment Operators
 =
simple assignment
 +=, -=, *=, /=, %= arithmetic operation and assignment (compound assignment)
Java Identifiers
An identifier is a user-defined name for a program entity
 variable name
int boxesSold, totalBoxesSold;
 named constant
final int MAX_SIZE = 100;
 class name
class ScoutReport /
class AceDiscountSalesReport
 method name
startUp( ), processRecord( ), readRecord( ), scoutBreak( ), etc.
Java Variables
used to store data while a program or module is running
 counter
count = count + 1;
 accumulator
totalSales = totalSales + sales;
 calculated field
maturity = principle * Math.pow( 1 + intRate, payments);
 index or subscript scoutName[scoutID], scoutTotal[scoutID]
 flag/switch
boolean variable: eof = true, eof = false, sentinelValue
Java Variable Scope Levels
 global
defined OUTSIDE of any method in Java (usually at the beginning of the class)
 local
defined INSIDE of a method in Java – only available inside of the method
Java Literals
 integer
 floating-point
 string
 boolean
type int in Java
5 -3 0
type double in Java
12.5 -3.89 123.4567
type String in Java
“Hello World” “A” “123”
predefined in Java --> true / false
“” (empty String)
Java Statements
a statement may be a simple statement or a compound statement (block)
 simple
Examples: x = x + 1; / processRecord( ); / scoutID = input.nextInt( );


compound ( block )
a compound statement is one or more simple statements treated as a unit
The statement(s) must be enclosed in curly braces { }
Example: { x = x + 1; System.out.println(x); }
Compound statements ( blocks ) are often used with selection & loop statements
many statements also use an expression (something which may be evaluated to get a value)
Control Structures in Java
sequence
a single action that does not ask a question or repeat statements
Examples of sequence structures in Java
 input operation
 output operation
 declaration of variables
 performing a calculation
 incrementing a counter
boxesSold = input.nextInt( );
outFile.println(“Total Sales: “ + totalSales);
int x, y, z; / String scoutName; / double sales;
totalSales = boxesSold * 6.75;
scoutCounter = scoutCounter + 1; ++scoutCounter;
3


accumulating into an accumulator
calling a module
selection
totalSales = totalSales + sales;
finishUp( ); / processRecord( ); / printDetailLine( );
* performing a test to determine which of possibly 2 paths of execution should be
followed. Selection statements are a type of conditional statement
* A special version of a selection structure where branching occurs on the ELSE branches
is called a CASE structure.
Examples of selection structures in Java
 if-then [single alternative selection]
if ( condition )
if ( boxesSold != -1 )
statement;
totalBoxesSold += boxesSold;

if-then-else [dual alternative selection]
if ( condition )
if ( x > 0 )
statement_1;
System.out.println(“X is a positive number”);
else
else
statement_2;
System.out.println(“X is 0 or a negative number”);

To perform more than one statement, create a compound statement ( aka block ) ---> { }
loop
a group of statements are executed repeatedly while a condition is true or until a
condition becomes true. AKA repetition and iteration
Type of loop structure based on where the test expression is located
 pre-test
the test occurs before the body of the loop
while, for
 post-test
the test occurs after the body of the loop
do…while
Loop Expressions
 initialize
 test
 update
declare and/or initialize the loop control variable
perform the test expression to see if the body of the loop should be entered (repeated)
increment/decrement the loop control variable using a step value
Examples of loop structures in Java
 while
initialize-expression
while ( test-expression )
{
statement;
update-expression;
}


do..while
initialize-expression
do
{
statement;
update-expression;
} while (test-expression);
int count = 10;
while ( count >= 1 )
{
System.out.println(count);
count = count – 1;
}
int count = 10;
do
{
System.out.println(count);
count = count – 1;
} while ( count >= 1 );
for
for (initialize-expression ; test-expression ; update-expression )
{ <loop body> }
for ( scoutID = 1; scoutID <= 4;
System.out.println(scoutID);
++scoutID )
4
Java Control Break Programs






Control break reports
Control break levels
Control break fields
Hold fields
Priming read
Termination

Control break logic:
o check from highest level to lowest level in order
o if a control break is detected: process breaks from the lowest level to the level at which the break
occurred (current level)
o remember you must force a final control break at end of file (after last record has been read)

Control break activities
o print a summary line ( outFile.println( "…" );
o accumulate into higher-level accumulators ( grandTotal += total; )
o zero out accumulators ( total = 0; )
o update hold fields ( holdScoutName = scoutName; )
o Increment counter ( numRecs += 1; )

Location of call for a control break module (see CIS 103 Study Guide for more details)
o separate calls from one level
o embedded calls in higher level to lower level modules
summary and detail or summary only (detail info is optional)
single, double, triple, etc. (1, 2, 3, 4, … )
file must be sorted on these fields
used to detect a control break (compare hold field to field)
required to initialize a hold field
must force the last control break
Java Arrays





An array is an aggregate structure that exists in memory. Use instead of a bunch of variables.
single/multiple dimension arrays
How to define arrays
int scores[] = new int[20];
int oddNumbers[] = {1, 3, 5, 7, 9};
Types of arrays
o compile-time
data for the table is in the program
o run-time
element values are assigned as program runs
o parallel
related arrays with synchronized data
Uses of arrays
o counters
o accumulators
o names
o product numbers
o student numbers
o range arrays
o to replace nested if logic
scoutBoxes[scoutID] += boxesSold;

zero-based indexing
int x[] = new int[5];

Loading of arrays
o compile time
String stooges[ ] = { "Moe", "Larry", "Curly", “Shemp” };
o

run time
//5 element array - valid indexes are 0 – 4!
int scoutBoxes[ ] = new int[5];
scoutBoxes[scoutID] += boxesSold;
//declaration
//run-time accumulation
Searching of arrays
o Use a boolean variable to make searching efficient
o while ( subscript < arraySize && notFound == true )
5

Sequencing of data in arrays
o unsorted
o sorted in ascending or descending order

Accessing an array element
scores[ 1 ]
o subscript (aka index)
integer expression: x[1], x[count], x[x.length – 1]
o bounds checking
array index out-of-bounds exception
 subscript < 0 or subscript >= array size
o length field
returns the size of an array
 int array[] = new int[20];
array.length is 20.

In Java, valid subscripts are in the range 0 to array size – 1

An array element can be used for anything that a variable of the same type can.

Using loops with arrays
o loop control variable is used as a subscript
 for(x = 1; x < array.length; ++x)
System.out.println( array[x] );
o input variable is used as a subscript
 scoutID = inFile.nextInt(); --> [scoutID] += boxesSold;
Java method template
<return-type><method-name>( <parameter list>
{
)
<method body>
[ return; ]
//optional return statement
}
6
Download