Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS-I Final Review Hao Jiang Computer Science Department Boston College.

Similar presentations


Presentation on theme: "CS-I Final Review Hao Jiang Computer Science Department Boston College."— Presentation transcript:

1 CS-I Final Review Hao Jiang Computer Science Department Boston College

2 About Programs Data and method (procedures, routines, functions) are two fundamental elements of programming Two basic tasks in constructing a program Define data types and data elements Define procedures that process data Two schemes of building a program Procedure oriented (what we have been learning) Object oriented (to be covered soon)

3 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 a trivial base problem. You make sure that the reeducation plan will NOT go infinitely.

4 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; }

5 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); }

6 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 b array (length is a.length)

7 Applications of Arrays You should know how to manipulate elements in 1D and 2D arrays. You should understand how random shuffle works. You should be able to write programs to locate specific elements in an array.

8 Define New Data Types Data type is an abstraction of data and the methods that can be operated on the data. Data ( or properties) Methods Defines a data type

9 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

10 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

11 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 Object p contains a pointer to the picture object The scope of p is restricted in the current { }, but the object has a life time for the whole process.

12 Arguments of Reference Type void fun(T p) { p.method1(); p.method2(); } T q = new T(); fun(q); // q has been modified by // method1 and method2 q T object when calling void fun(T p), q copied its value to p p

13 Pass by Value void fun(T p) { p = new T(); p.method1(); p.method2(); } Does this one have the Same effect?

14 Pass by Value q T object p x void fun(T p) { p = new T(); p.method1(); p.method2(); } The original object does not change.

15 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

16 Object Oriented Programming Different from procedure based approach, object oriented programming starts from abstracting data instead of procedures. functionB functionA functionB Obj1: A Obj2:A Obj3:B Class AClass B Procedure based approach (using static methods in Java) program Object oriented approach

17 Inheritance class A { … public void f() {…} } class B extends A { … public void f() {…} } class C extends A { … } A BC Base class Derived classes A p = new B(); A q = new C(); p.f(); q.f();

18 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(); …

19 Linked List Linked list is a widely used data structure. We can use OOP methods to define the new data type List. null head class List { class Node { Object data; Node next; } Node head = null; public void append(Object d) {…} public void delete(int n) {…} }

20 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. Do not have to read other books. It is a good idea to try writing some programs on the paper. Be confident and relaxed; come to the exam on time.


Download ppt "CS-I Final Review Hao Jiang Computer Science Department Boston College."

Similar presentations


Ads by Google