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.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Written by: Dr. JJ Shepherd
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Final Review.
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,
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.
J.43 ARRAYS  A Java array is an Object that holds an ordered collection of elements.  Components of an array can be primitive types or may reference.
Final Review Recitation – 05/01/2009 CS 180 Department of Computer Science, Purdue University.
CS180 Review Questions. Administriva Final Exam –Friday 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Final Review. Review A Binary Search divides a sorted data structure into two equal parts and each time throws away the part that cannot contain the item.
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
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.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
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.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
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.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
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.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
1 Review of Java Basic Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – conditional statements –Flow of Control.
CSC 212 Object-Oriented Programming and Java Part 2.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
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,
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
April 27, 2017 COSC Data Structures I Review & Final Exam
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
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.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 7 User-Defined Methods.
3 Introduction to Classes and Objects.
“Form Ever Follows Function” Louis Henri Sullivan
Yanal Alahmad Java Workshop Yanal Alahmad
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Starting JavaProgramming
null, true, and false are also reserved.
Introduction to Java Programming
FINAL EXAM Final Exam Wednesday, Dec 14: 8: :00 AM (Frny G140)
Chap 2. Identifiers, Keywords, and Types
Review for Midterm 3.
FINAL EXAM Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112)
Presentation transcript:

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 questions 5 programming questions  More stress on topics (chap 11 – 15) covered after exam 2 Recursion Dynamic Data Structures Window Interfaces using Swing Applets and HTML More Swing

Object-Oriented Design Methods  Extract functionalities of a program which will be performed multiple times Encapsulation and Information Hiding Classes, Objects, and Polymorphism  Covered in more detail later

Primitives int, double, boolean, float, char, etc. These are NOT Objects  Can be compared with ==, !=, =, etc.  Wrapper classes for each primitive Why do we need them? Explicit type-casting may need to be done  byte  short  int  long  float  double

Objects and Classes Class variables hold references (the address) to the objects Always compare with the equals() method  Do NOT use == to compare objects Assignment operators assign references – they do not make separate copies 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  Pass by value always Keyword: this

Strings Strings are a type of object  Also should not be compared with == Important String functions  charAt  indexOf  substring  length  Concatenation operator +

Selection Statements Modifies the flow of control of the program if/else construct  Must have a boolean condition to check against  { } are strongly recommended, but not necessary for one line statements switch construct  Multi-way branch which makes a decision based on a char, byte, short, or int type  default case  break statement

Repetition statements - Loops for while do-while Beware of -  Off-by-one errors  Infinite looping  Overflow

Arrays Linear collection of data  Can hold primitives or class types, 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 Multiple dimension arrays Convenient to use for-loops to iterate through members of the array Remember – arrays are reference types

Exceptions Use a try/catch/finally block to handle exceptions thrown by a program Use throw statement to notify caller of an error User defined exceptions must extend Exception

File I/O Useful 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

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 Reuse inherited functionality from parent class. Call to base class’s constructor is always called at the first line of constructor.

Dynamic Data Structures Inner classes Lists  Node class  Circularly linked lists  Doubly linked lists Trie data structure ( from project 7 ) Java Collections  Vector  ArrayList  LinkedList

Recursion Break down a problem to one or more sub-problems Be concerned only with the current level of recursion Two necessary cases  Base case  Recursive step

GUI and Event-driven Programming Common classes  JFrame  JPanel  JLabel  JMenu, JMenuItem  JButton Layouts  BorderLaout  GridLayout  FlowLayout

Challenges

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(); 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(); valid – B is a subclass of A invalid – cannot instantiate interfaces invalid – B is the superclass of C valid – C is a subclass of B invalid – A does not implement I valid – A implements J, and J is-a I valid – D implements I valid – C extends B which implements K

Recursion and Linked Lists Question- A gradebook is made out of the the following Node class. class Node { private int[] values;// these values are sorted in ascending order private Node next; public Node(int[] values, Node next) public int[] getValues() public void setValues(int[] values) public Node getNext() public void setNext(Node next) } 1) Each Node contains an array of scores that are within a certain range. 2) The gradebook contains a linked list of the Node class. For example, you may use the 1 st Node to store all the scores in the F range, the 2 nd Node to store all the scores in the D range, the 3 rd Node to store all the scores in the C range, etc.

Recursion and Linked Lists Question continued- Each node represents a non-overlapping group of grade values and each successive Node contains a range of group values greater than the previous Node. Write an efficient method gradeExists(int), which returns true if the grade exists within the Gradebook, and false otherwise. Since the values array is sorted, you should take advantage of this and perform a binary search for the specified value.

Recursion and Linked Lists Approach to the solution: The solution can be broken into two parts: 1. Finding the node which might contain the grade. Since all nodes have non-overlapping ranges (in sorted order) and the arrays in turn are sorted, we only need to compare the range of each node with the given grade. 2. Do binary search on the values in that node.

Recursion and Linked Lists Part 1: public boolean gradeExists(int grade) { Node temp = gradebook; while (temp != null) { int[] values = temp.getValues(); if (grade < values[0]) return false; if (grade >= values[0] && grade <= values[values.length-1]) return binarySearch(values, grade, 0, values.length-1); temp = temp.getNext(); } return false; }

Recursion and Linked Lists Part 2: private boolean binarySearch(int[] array, int value, int lo, int hi) { if (lo > hi) return false; if (lo == hi) return array[lo] == value; int mid = (hi + lo)/2; if (array[mid] == value) return true; else if (array[mid] > value) return binarySearch(array, value, lo, mid-1); else return binarySearch(array, value, mid+1, hi); }

Recursion Write a function isPalindrome(String) which takes in a String and returns true if the String is a palindrome, false otherwise. Ignore the case of the letters and skip over blanks when you make the comparison. public boolean isPalindrome(String s) { }

Recursion Solution: public boolean isPalindrome(String s) { if (s == null) return false; if (s.length() <= 1) return true; return ( s.charAt(0) == s.charAt(s.length()-1) && isPalindrome(s.substring(1, s.length()-1)) ); } public static void main(String[] args) {... result = isPalindrome(s.toLowerCase().replace(“ “,””)); }

GUI Question: Create a complete Java GUI program that allows the user to enter the text for JLabels, possibly with multiple lines, and then preview them. You should use the following design: The user can enter text in the text area. Then, when the button is pushed, this text is displayed as a label in the label preview. Title Button Text Area Label Preview Enter Text:Label Preview:

GUI - solution public class CH12 extends JFrame implements ActionListener { JButton theButton = new JButton( "Preview" ); JTextArea theText = new JTextArea(); JLabel theLabel = new JLabel(); public CH12() { super( "Label Previewing Program" ); setSize( 400, 200 ); //button in south JPanel aPanel = new JPanel(); aPanel.add( theButton ); this.add( "South", aPanel ); //label in north aPanel = new JPanel(); aPanel.add( new JLabel("The Label Previewer") ); this.add( "North", aPanel ); JPanel centerPanel = new JPanel( new GridLayout(1,2) ); //text area in center left aPanel = new JPanel( new BorderLayout()); aPanel.add( "North", new JLabel("Enter Text:") ); theText.setLineWrap( true ); aPanel.add( "Center", theText ); centerPanel.add( aPanel ); //preview label in center right aPanel = new JPanel( new BorderLayout()); aPanel.add( "North", new JLabel("Label Preview:") ); aPanel.add( "Center", theLabel ); centerPanel.add( aPanel ); this.add( "Center", centerPanel ); theButton.addActionListener( this ); }

GUI – solutioncontd. public void actionPerformed( ActionEvent e ) { theLabel.setText( theText.getText() ); } public static void main( String[] args ) { JFrame ch12Frame = new CH12(); ch12Frame.setVisible( true ); } }//end of class def

Arrays Write a method which takes in an array of integers and replaces the values of the array with a value c i, where c i is defined as – c i = 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, where c i is defined as – c i = 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]; }

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.

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