CS-I Midterm Review

Download Report

Transcript CS-I Midterm Review

CS-I Final Review
Hao Jiang
Computer Science Department
Boston College
About Programs

Data and method (procedures, routines, functions) are two
fundamental elements of programming

Two basic tasks in constructing a program



Define data
Define procedures that process data
Two schemes of building a program


Procedure oriented
Object oriented
Recursion

Recursion is an important method to construct functions to
solve problems.

To construct a recursive function:



You should have a scheme to “reduce” your problem into smaller
same problems that can be combined to solve the current
problem.
You know how to solve trivial base cases.
You make sure that the reeducation plan will NOT go infinitely.
Recursion Example:

Example: A function that returns a string that reverse the
original one, e.g. if the input is “abc” the output is “cba”.
Base case: If the input has a single character, return itself.
Reduction plan: Reverse the substring starting from the second character
and then connect it with the first character.
public static String reverseString(String str)
{
if (str.length() == 1) return str;
string s = reverseString(str.substring(1)) + str.charAt(0);
return s;
}
Recursion Example:

Example: Test whether a number is a power of 2.
public static boolean isPowerOfTwo(int n)
{
if (n == 1)
return true;
if (n % 2 == 1)
return false;
return isPowerOfTwo(n/2);
}
Array


Array is a data structure for representing a sequence of data
which have the same type and are indexed by numbers.
Java Syntax:
T[] a;
// T can be any built-in or
// user defined data types
T[] b = new T[N]; // Declaration and creation
T[][] c = new T[N][M]; // 2D array
a
array (length is a.length)
Applications of Arrays

You should know how to manipulate elements in 1D and 2D
arrays.

You should be able to write programs to locate specific
elements in an array.
Define New Data Types

Data type is an abstraction of data and the methods that can
be operated on the data.
Data ( or properties)
Defines a data type
Methods
Class and Object

In Java, we can define a new data type with a class. Each
instance of a class is called an object.
public class picture {
private int width, height, depth;
private double[][][] image;
public Picture(int w, int h, int d) {
width = w;
height = h;
depth = d;
image = new double[w][h][d];
}
public void read(String fileName) {…}
public void write(String fileName) {…}
public Picture rotate(double t) {…}
public Picture resize(int w, int h) {…}
}
Data members
(the variables are called
instance variables)
Constructor
More methods
Class and Object
…
Picture p = new Picture(256,
256, 3);
p.read(“Spring2008.jpg”);
Picture q = p.resize(512, 512, 3);
q.write(“Spring2008Big.jpg”);
…
Create an Picture object
Use method read
Use method resize
Use method write
Reference Types
Picture p = new Picture(256, 256, 3);



Variable p has a reference type.
The value of p is a “reference” or “pointer” to the Picture
object.
If you do System.out.print(p), you will get a number that is the
address of the picture object in memory (heap memory).
p
p contains a
pointer to the
picture object
Object
The scope of p is restricted in the current { }, but the object
has a life time for the whole process.
Reference Type Argument Variables
void fun(T p)
{
p.method1();
p.method2();
}
q
T q = new T();
fun(q);
// q has been modified by
// method1 and method2
T object
p
when calling void fun(T p), q copied its value to p
Pass by Value
void fun(T p)
{
p = new T();
p.method1();
p.method2();
}
Does this one have the
Same effect?
Pass by Value
q
T object
p
T object
void fun(T p)
{
p = new T();
p.method1();
p.method2();
}
The original object does not
change.
Arrays of Object
void fun(T[] p)
{
for (int i = 0; i < p.length; i++)
{
p[i].method1();
p[i].method2();
}
}
T[] q = new T[3];
q[0] = new T();
q[1] = new T();
q[2] = new T();
fun(q);
Create the reference array
Create the objects
and put their references
into the array
Object Oriented Programming

Different from procedure based approach, object oriented
programming starts from abstracting data instead of
procedures.
Class A
functionA
Class B
functionB
Obj1: A
Obj3:B
functionB
functionA
Obj2:A
program
Procedure based approach
(using static methods in Java)
program
Object oriented approach
Inheritance
Base
class
A
class A {
…
public void f() {…}
}
class B extends A
{…
public void f() {…}
}
class C extends A {
…
}
B
C
Derived classes
A p = new B();
A q = new C();
p.f();
q.f();
Abstract Class and Interface
public abstract class A {
…
public abstract void f();
A is an abstract class.
all the derived classes
must override f().
}
public interface T {
public void g();
public void f();
}
class B implements T {
…
public void g() {…}
public void f() {…}
}
T t = new B();
…
Linked List

Linked list is a widely used data structure.
head

null
We can use OOP methods to define the new data type List.
class List {
class Node {
Object data;
Node next;
}
Node head = null;
public void append(Object d) {…}
public void delete(int n) {…} }
About the Exam

The exam is not hard if you know the stuff we did in class.

Read (browse) the book before the exam. Look at the
examples and try some exercises.

You do not have to read other books.

It is a good idea to practice writing some programs on the
paper.

Be confident and relaxed; come to the exam on time.