Study Guides (400,000)
US (230,000)
USD (90)
COMP (10)

COMP 151 Study Guide - Midterm Guide: Switch Statement, Java Compiler, While Loop

Computer Science Courses
Course Code
COMP 151
Jiang Eric
Study Guide

This preview shows page 1. to view the full 5 pages of the document.
Computer Programming 151: Midterm Study Guide
Java Basics
● Branching
Inheritance → defining a new class from an existing class
Syntax: public class Subclass extends Superclass
To call superclass constructor: super(); or super(parameters);
To call superclass methods: super.method(parameters); or SuperClass.staticMethod;
● Looping
For loop → initial-action, condition test, & iteration all in one line (ex: int i = 0; i <= n; i++)
While loop → may not execute statements at all (ex: while (condition test){ … })
Do statement → execute statement first, then checks and is executed until expression is
false -- completes a loop at least once
} while (expression);
Array → collection of elements with the same data type (Index starts with 0)
Syntax: elementType[] refVarName = new elementType[size];
Import java.util.Arrays → static methods for manipulation (ex: Arrays.sort(a);)
Array length = str.length
Multidimensional array: array of arrays (ex: int[][] arg = new int[5][7];)
Location of array is passed to the method & changes made in method will affect variable
Passing by reference
Object Class → the root; java supports a single class inheritance hierarchy
public boolean equals(Object o) // equality based on references
public String toString() // returns a string that textually represents the object
Static Method → called by class name -- objectVar.methodName();
Instance Method → called by object -- ClassName.methodName();
References → data type used to locate an object in memory
Passing by value: applies to primitive type parameters → change in method has no
change or impact on actual argument
Passing by reference: applies to parameters of class type and arrays → change will
affect the string
Null reserved for class types only
String Class and Methods
Most useful next methods: String next(), boolean nextBoolean(), String nextLine(), int nextInt(),
double nextDouble(), etc.
Hit enter key “\n” to start new line
String length: name.length();
StringTokenizer Class → allows a program to break a string into individual pieces or tokens
Constructor to create object: public StringTokenizer(String str)
StringTokenizer st = new StringTokenizer(“Sentence goes here”);
public StringTokenizer(String str, String delim)
st = new StringTokenizer(“I love pizza”, “ .”); //will separate based on “ ” and “.”
public String nextToken(), public boolean hasMoreTokens()
find more resources at
find more resources at
You're Reading a Preview

Unlock to view full version

Only page 1 are available for preview. Some parts have been intentionally blurred.

Computer Programming 151: Midterm Study Guide
Exceptions → handles an error during execution
Throw an exception → method indicates that an error has occurred; give warning
Catch an exception → method deals with the error condition
Try-Catch Block → place statement that might throw an exception within try block which must
be followed by one or more catch blocks and when exception occurs, control is passed to catch
block which indicates type of exception you want to handle (always have IOException)
ArrayIndexOutOfBounds Exception → when index value is < 0 or >= length of array
Checked Exceptions → java compiler; must be handled locally or throw by method
Ex: FileNotFoundException or EOFException (end of file)
Runtime Exception → problem when you execute (JVM)
Ex: IndexOutOfBoundsException or NullPointerException
Finally Block → executed whether or not an exception is thrown (placed after catch blocks)
But can be used even if no catch block is used
finally {
if( !in){ // same as in != null
catch(IOException e){
System.out.println(“Error Closing”);
Throws Clause → indicates a method may throw an exception if error occurs during its
○ Syntax: public methodName throws ExceptionClassName
Throw Statement → used to throw an exception at any time
Syntax: throw new ExceptionClass(stringMessage);
Ex: if (num <0) throw new Exception(“num is negative.”);
File Input/Output
File → sequence of components of the same type that resides in secondary storage; exists after
program execution terminates and can be large
End-of-line symbol → creates illusion that text file contains lines
End-of-file symbol → follows the last component in a file
Text Files (ASCII) designed for easy communication with people - flexible and easy to use
Input and output consist of streams (Sequence of characters that either come from or go to an
I/O device)
3 stream variables: (keyboard), System.out (monitor), & System.err (monitor) -
std error stream
Open a stream to a text file
Text input can be done by FileReader (constructor throws a FileNotFound and
IOException) before you can read from or write to a file
BufferedReader (provides text processing capabilities)
read() - method returns char as int value
find more resources at
find more resources at
You're Reading a Preview

Unlock to view full version