Final Review Recitation – 05/01/2009 CS 180 Department of Computer Science, Purdue University.

Slides:



Advertisements
Similar presentations
Written by: Dr. JJ Shepherd
Advertisements

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Introduction to Programming Lesson 1. Objectives Skills/ConceptsMTA Exam Objectives Understanding Computer Programming Understand computer storage and.
Chapter 10 Introduction to Arrays
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CS180 Review Questions. Administriva Final Exam –Friday 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS125 Exam Review Winter Some Exam Info Tuesday (22nd) at 4:00-6:30pm in the PAC CHECK YOUR SEAT!!! Read Final Exam Information on website –Practice.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Java ProgrammingtMyn1 Java Programming Timo Mynttinen Mikkeli University of Applied Sciences.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Introduction to Java Java Translation Program Structure
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
1 Review of Java Basic Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – conditional statements –Flow of Control.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
CS1101 Group1 Discussion 10 Lek Hsiang Hui comp.nus.edu.sg
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS 180 Final Exam Review 12/(11, 12)/08. Announcements Final Exam  Thursday, 18 th December, 10:20 am – 12:20 pm in PHYS 112  Format 30 multiple choice.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
A High Flying Overview CS139 – Fall 2006 How far we have come.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object Oriented Programming Lecture 2: BallWorld.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Lecture 2: Data Types, Variables, Operators, and Expressions
CS230 Tutorial Week 3.
null, true, and false are also reserved.
Java Programming Language
Introduction to Java Programming
FINAL EXAM Final Exam Wednesday, Dec 14: 8: :00 AM (Frny G140)
Agenda Types and identifiers Practice Assignment Keywords in Java
Review for Midterm 3.
FINAL EXAM Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112)
Presentation transcript:

Final Review Recitation – 05/01/2009 CS 180 Department of Computer Science, Purdue University

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-casting double  float  long  int  short  byte Implicit type-casting byte  short  int  long  float  double

©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

Repetition for while do-while Pitfalls  Off-by-one errors  Infinite looping  Different control flows

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

Review ends here. Now time for exercises!

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 have access to non-static class variables. This code will Not 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 c i. Define c i 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 c i. Define c i 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); returns.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) { }

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

Finally… Thanks for your hard work this semester! Good luck with your CS180 final and all other finals and projects! Have a wonderful summer!!