CSC 160 Practice Final Review. Question 1 class A { public void blah() { System.out.println("a"); } class B extends A { public void blah() { System.out.println("b");

Slides:



Advertisements
Similar presentations
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
Java Review Interface, Casting, Generics, Iterator.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Computer Science 209 Software Development Iterators.
COMP 103 Linked Stack and Linked Queue.
Stacks, Queues, and Deques
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
CS 280 Data Structures Professor John Peterson. Project 9 Questions? IS280.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
Introduction to Searching and Sorting
Midterm Test Overview CS221 – 3/23/09. Test Curve Current median is a 71 or C- Median will be adjusted to an 81 or B- All test scores will be +10 on.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Effective Java: Generics Last Updated: Spring 2009.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
CIS* Quiz 2. For a large array, and in the worst case, selection sort is faster than insertion sort. False Both selection sort and insertion sort.
Types in programming languages1 What are types, and why do we need them?
Object-Oriented Programming Simple Stack Implementation.
Overriding toString()
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CS 367 Introduction to Data Structures Lecture 3.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Interfaces and Inner Classes
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Linked Data Structures
The need for Programming Languages
Software Development Java Classes and Methods
Software Development Iterators
Lecture 17: Polymorphism (Part II)
Week 15 – Monday CS221.
CS Week 8 Jim Williams, PhD.
Introduction to Searching and Sorting
null, true, and false are also reserved.
Data Structures ADT List
Introduction to Java Programming
The Boolean (logical) data type boolean
ArraySet Methods and ArrayIterator
Need for Iterators O(n2) this is a common application
Everything the light touches, Simba, will be yours
Building Java Programs
Example: LinkedSet<T>
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Lecture 18: Polymorphism (Part II)
Variables and Java vs C++
Building Java Programs
A Few Review Questions Dan Fleck CS211 Fall 2007.
TCSS 143, Autumn 2004 Lecture Notes
Objects with ArrayLists as Attributes
A type is a collection of values
Presentation transcript:

CSC 160 Practice Final Review

Question 1 class A { public void blah() { System.out.println("a"); } class B extends A { public void blah() { System.out.println("b"); } class Test { public void printObject(A a) { a.blah(); } public static void main(String[] args) { A a = new A(); B b = new B(); 1:b.blah(); 2:((A)b).blah(); 3:((B)a).blah(); 4:printObject(b); 5:printObject((A)b); } Answers: 1)b 2)b 3)ClassCastException 4)Compile Error 5)Compile Error

Question 2 class Point { public int x; public int y; public boolean equals(Object o) { // Fill in this method. It should return true if the // value of o is equal to the value of this, return // false otherwise. Two points should be considered // equal if both their x and y values are the same } public boolean equals(Object o) { boolean toReturn = false; if(o instanceof Point) { Point p = (Point)o; toReturn = (this.x==p.x) &&(this.y==p.y); } return toReturn; }

Question 3 public static boolean substring(String a, String b) { boolean toReturn = false; if(a == null || b == null || a.length()==0 || b.length()==0) toReturn = false; else if(a.length() > b.length()) toReturn = false; else { String toCompare = b.substring(0, a.length()); if(a.equals(toCompare)) toReturn = true; else if(a.length() < b.length()) toReturn = substring(a, b.substring(1, b.length()); } return toReturn; }

Question 4 public class MyListIterator implements Iterator { private Object[] items; private int curIndex; public MyListIterator(MyStack stack) { items = new Object[stack.size()]; for(int i = stack.size()-1; i > 0; i--) { items[i] = stack.pop(); } for(int i = 0; i < items.length; i++) { stack.push(items[i]); }

Question 4 cont’d public boolean hasNext() { return curIndex < items.length; } public Object next() { return items[curIndex++]; } public void remove() { throw new UnsupportedOperationException(); }

Question 5 No. If working on a problem where you know n < some number, possible O(n^2) algorithm is faster. Big O notation doesn’t take into account constants in the running time. O(n) algorithm could have a very big constant

Question 6 Java Easier –Java comes with very large API –Java has garbage collection –Java has exceptions –Java is object-oriented C Sometimes Better –Garbage collection not suitable for real-time –Takes up less memory – no Virtual Machine –Lower level access

Question 7 public class BinaryTree implements BinaryTreeInterface { private BTNode root; public void printTree() { if (root != null) { System.out.println(root.element(); printPostOrder(root.getLeft()); printPreOrder(root.getRight()); }

Question 7 cont’d public void printPostOrder (BTNode node) { if (node != null) { printPostOrder(node.getLeft()); printPostOrder(node.getRight()); System.out.println(node.element()); } public void printPreOrder(BTNode node) { if (node != null) { System.out.println(node.element()); printPreOrder(node.getLeft()); printPreOrder(node.getRight()); } } // Ends Class