View
218
Download
5
Category
Preview:
Citation preview
Final Exam Overview Time and place
10:20am on May 6, at SMTH 108 Types of questions
30 multiple choice questions 5 programming questions on
Objects, inheritance, exception, file I/O, recursion Cover all chapters, with a focus on later chapters that
haven’t been tested FYI: old exams available on course website
Fall 2006 exam I, II, and final
TA evaluation online, open until 11pm, May 2
Object-Oriented Design
Methods Extract small parts of a program and
calculations which will be performed multiple times
Encapsulation Classes, Objects, and Polymorphism
Covered in more detail later
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
4
Primitives Numeric: int, double, float, double, … Character: char Logic: boolean They are NOT Objects
Can be compared with ==, !=, <=, >=, etc. Cannot call methods
Explicit type-castingdoublefloatlongintshortbyte
Implicit type-castingbyteshortintlongfloatdouble
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
5
Objects Objects are handled with a reference address Two objects should be compared with the .equals() method
Do NOT use == to compare objects Assignment operators assign references – they do not make separate
copies NullPointerException – always make sure your object is not null
before calling methods with it Constructors should be used to initialize class variables Calling methods
Need object Static methods
Can use class name Cannot access non-static methods/variables inside
Keyword: this
Strings Strings are a type of object
Also should NOT be compared with == Can be concatenated with the ‘+’ operator Important String functions
charAt indexOf substring length
Selection Modifies the flow of control of the program if/else construct
Must have a boolean condition to check against { } are important, but not necessary for one line
statements else branch is optional Don’t be confused by indentation..
switch construct Multi-way branch which makes a decision based on a
char, byte, short, or int default case break statement
GUI and Event-driven Programming Common classes
JFrame JPanel JLabel JMenu, JMenuItem JButton
Layouts
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
10
Arrays Linear collection of data
Can hold primitives or Objects, but must all be of the same type
length tells the number of elements in the array Member, not a method
Indexed from 0 to length – 1 Protect against
ArrayIndexOutOfBoundsException Can create multiple dimension arrays Usually use for-loops to iterate through members
of the array
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11
Exceptions and Assertions
Use a try/catch/finally block to handle exceptions thrown by a program
Use throw statement to notify caller of an error Exception types and hierarchy. Examples of
RunTimeHierarchy? User defined exceptions must extend Exception Use assertions to detect internal errors. Use
exceptions to notify the client programmer of the misuse of our class. When assertions fail, an AssertionError is thrown.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
12
File I/O Many classes
FileInputStream, DataInputStream, FileReader, BufferedReader, Scanner, PrintWriter, DataOutputStream, etc.
ObjectInputStream, ObjectOutputStream Used to write objects to a file Any object serialized by a stream must implement
Serializable Which classes are used to write text and which
are used to write binary? Always close files you open
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
13
Inheritance and Polymorphism Differences between abstract classes and
interfaces Polymorphism can simplify code Extend specific classes from general
classes Use protected keyword to protect
information and methods No need to rewrite methods which are the
same as in a parent class Super constructor is always called as the
first line of constructor
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
14
Dynamic Data Structures and Generics
Inner classes Lists
Node class Circularly linked lists Doubly linked lists
Java Collections Vector ArrayList LinkedList
Recursion Think about what the sub-problem is Only be concerned with the current level of
recursion Two necessary cases
Base case Recursive case
What is the output?public class A
{
private int x;
public static int doStuff()
{
x = 100;
x /= 3;
x++;
return x;
}
public static void main(String[] args)
{
System.out.println(A.doStuff());
}
}
What is the output?
public class A
{
private int x;
public static int doStuff()
{
x = 100;
x /= 3;
x++;
return x;
}
public static void main(String[] args)
{
System.out.println(A.doStuff());
}
}
Because this method is static, it does not haveaccess to non-static class variables. This code willNot compile because x is non-static.
Types
Given the following classes, which of the following declarations are valid?
public interface I {…}
public interface J extends I {…}
public interface K {…}
public abstract class A {…}
public class B extends A {…} implements J, K
public class C extends B {…}
public class D extends A {…} implements I
A a = new B(); B b = new J();
C c = new B(); B b = new C();
I i = new A(); I i = new B();
I i = new D(); K k = new C();
Types
Given the following classes, which of the following declarations are valid?
public interface I {…}
public interface J extends I {…}
public interface K {…}
public abstract class A {…}
public class B extends A {…} implements J, K
public class C extends B {…}
public class D extends A {…} implements I
A a = new B(); valid – B is a subclass of A
B b = new J(); invalid – cannot instantiate interfaces
C c = new B(); invalid – not all B is the superclass of C
B b = new C(); valid – C is a subclass of B
I i = new A(); invalid – A does not implement I
I i = new B(); valid – A implements J, and J is a type of I
I i = new D(); valid – D impelements I
K k = new C(); valid – C extends B which implements K
Arrays
Write a method which takes in an array of integers and replaces the values of the array with a value ci. Define ci to be the sum of the numbers in indices 0…i in the incoming array.
public void cumulativeArray(int[] a) {
}
Arrays
Write a method which takes in an array of integers and replaces the values of the array with a value ci. Define ci to be the sum of the numbers in indices 0…i in the incoming array.
public void cumulativeArray(int[] a) {
if (a.length <= 1)
return;
for (int k=1; k<a.length; k++)
a[k] = a[k] + a[k-1];
}
Linked Lists Given an appropriate (integer) Node class,
write a recursive method which sums up the numbers in the list.
public int sumList(Node l)
{
}
Linked Lists Given an appropriate (integer) Node class,
write a recursive method which sums up the numbers in the list.
public int sumList(Node l)
{
if (l == null)
retrurn 0;
return l.num + sumList(l.next);
}
Strings
Write a recursive method reverse which takes in a String and returns the reverse of the String. You may NOT use the reverse method in the String class, however you may use other methods available to you.
public String reverseString(String s) {
}
Strings
Write a recursive method reverse which takes in a String and returns the reverse of the String. You may NOT use the reverse method in the String class, however you may use other methods available to you.
public String reverseString(String s) {
if (s == null || s.length() <= 1)
return s;
if (s.length() == 2)
return “” + s.charAt(1) + s.charAt(0);
return s.charAt(s.length()-1) + reverseString(s.substring(1, s.length()-1)) +
s.charAt(0);
}
Linked Lists
Given an appropriate Node class, write a method which removes every other node from the list, starting with the second.
public Node removeEveryOther(Node l)
{
}
Linked Lists
Given an appropriate Node class, write a method which removes every other node from the list, starting with the second.
public Node removeEveryOther(Node l)
{
Node l1 = l;
while (l1 != null && l1.next != null)
{
l1.next = l1.next.next;
l1 = l1.next;
}
return l;
}
Linked Lists
Now write the same method, but recursively.
public Node removeEveryOther(Node l)
{
// base case
if (l == null || l.next == null)
return l;
// recursive case
Node l1 = removeEveryOther(l.next.next);
l.next = l1;
return l;
}
Recursion Given an appropriate node class, write a
recursive method which reverses a singly linked list.
public Node reverse(Node l)
{
}
Recursion Given an appropriate node class, write a
recursive method which reverses a singly linked list.
public Node reverse(Node l)
{
if (l == null || l.next == null)
return l;
Node restreversed = reverse(l.next);
l.next.next = l;
l.next = null;
return restreversed;
}
Linked Lists
Given an appropriate Node class, one way to detect a cycle in your list is by moving two pointers around the list, one one index at a time and the other two indices at a time. Write a method which determines whether or not there is a cycle in a list.
public boolean hasCycle(Node l)
{
}
Linked Lists
Given an appropriate Node class, one way to detect a cycle in your list is by moving two pointers around the list, one one index at a time and the other two indices at a time. Write a method which determines whether or not there is a cycle in a list.
public boolean hasCycle(Node l)
{
Node l1 = l;
Node l2 = l;
while (l1 != null && l2 != null &
l1.next != null && l2.next != null &&
l2.next.next != null)
{
l1 = l1.next;
l2 = l2.next.next;
if (l1 == l2)
return true;
}
return false;
}
Recommended