Midterm 2 Review Lecture 23.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Java Programming Strings Chapter 7.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
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.
1 SSD3 - Unit 2 Java toString & Equals Presentation Class Website:
1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
slides created by Marty Stepp
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
EXAM 1 REVIEW. days until the AP Computer Science test.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
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?
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
August 6, Operators. August 6, Arithmetic Operators.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
Object Oriented Programming Lecture 2: BallWorld.
Recursion occurs when a method calls itself. public class RecursionOne { public void run(int x) { System.out.println(x); run(x+1); } public static void.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Midterm preview.
More on Arrays Review of Arrays of ints, doubles, chars
The need for Programming Languages
MC Question Strategies
Chapter 5 Ordered List.
Java Generics Lecture 14 CS2110 – Fall 2016
CS 302 Week 11 Jim Williams, PhD.
Chapter 5 Ordered List.
TO COMPLETE THE FOLLOWING:
An Introduction to Java – Part I, language basics
CS Week 9 Jim Williams, PhD.
CS 200 Objects and ArrayList
Operators August 6, 2009.
CSE 214 – Computer Science I More on Linked Lists
python.reset() Also moving to a more reasonable room (CSE 403)
Chapter 6 Array-Based Lists.
Recap Week 2 and 3.
class PrintOnetoTen { public static void main(String args[]) {
Recursion Problems.
Generics, Lambdas, Reflections
Variables and Java vs C++
Suggested self-checks: Section 7.11 #1-11
Peer Instruction 4 Control Loops.
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
LCC 6310 Computation as an Expressive Medium
Objects with ArrayLists as Attributes
Midterm 2 Review Lecture 23.
Presentation transcript:

Midterm 2 Review Lecture 23

Error: You cannot assign a LeadBoots object to p of type PowerUp. public interface PowerUp { public double speed(); public boolean invincible(); } 1. Given the interface and class to the left, and the main method below, are there any errors in this code? Error: You cannot assign a LeadBoots object to p of type PowerUp. Error: LeadBoots does not implement the PowerUp interface. Error: LeadBoots objects cannot be constructed because the class has no constructor. Both A and B. None of the above. There are no errors. public class LeadBoots { public double speed() { return 0.0; } public boolean invincible() { return true; public static void main(String[] args) { PowerUp p = new LeadBoots(); }

Error: AirJordans does not correctly implement the PowerUp interface. public interface PowerUp { public double speed(); public boolean invincible(); } 2. Given the interface and class to the left, and the main method below, are there any errors in this code? Error: AirJordans does not correctly implement the PowerUp interface. Error: The constructor is being called incorrectly. Both A and B. None of the above. There are no errors. public class AirJordans implements PowerUp { private double _speed; public AirJordans(double speed) { _speed = speed; } public double speed() { return _speed; public boolean inconceivable() { return true; public static void main(String[] args) { PowerUp p = new AirJordans(); }

public interface PowerUp { public double speed(); public boolean invincible(); } public class AirJordans implements PowerUp { private double _speed; public AirJordans(double speed) { _speed = speed; } public double speed() { return _speed; public boolean invincible() { return true; public class LeadBoots implements PowerUp { public double speed() { return 0.0; } public boolean invincible() { return true; public class NikeFrees implements PowerUp { public double speed() { return 2.0; } public boolean invincible() { return false; 3. What is printed when the following code runs: Map<String, PowerUp> brands = new HashMap<String, PowerUp>(); brands.put("Uggs", new LeadBoots()); brands.put("Nike", new NikeFrees()); brands.put("Nike", new AirJordans(11)); PowerUp powerUp = brands.get("Nike"); System.out.println(powerUp.invincible()); // true is printed

4. What is printed when the following code runs: public interface PowerUp { public double speed(); public boolean invincible(); } public class LeadBoots implements PowerUp { public double speed() { return 0.0; } public boolean invincible() { return true; 4. What is printed when the following code runs: List<PowerUp> powerUps = new ArrayList<PowerUp>(); powerUps.add(new LeadBoots()); powerUps.add(new NikeFrees()); powerUps.add(new AirJordans(23.0)); double x = 0.0; for (PowerUp p : powerUps) { x = x + p.speed(); } System.out.println(x); // 25.0 public class NikeFrees implements PowerUp { public double speed() { return 2.0; } public boolean invincible() { return false; public class AirJordans implements PowerUp { private double _speed; public AirJordans(double speed) { _speed = speed; } public double speed() { return _speed; public boolean invincible() { return true;

kennedy.run(); // Meeks ran 23 tiles. public class Hero { private String _name; private PowerUp _powerUp; public Hero(String name) { _name = name; _powerUp = new LeadBoots(); } public void setPowerUp(PowerUp powerUp) { _powerUp = powerUp; public void run() { double d = 1.0 * _powerUp.speed(); System.out.println(_name + " ran " + d + " tiles."); public interface PowerUp { public double speed(); public boolean invincible(); } public class LeadBoots implements PowerUp { public double speed() { return 0.0; } public boolean invincible() { return true; public class NikeFrees implements PowerUp { public double speed() { return 2.0; } public boolean invincible() { return false; 5. What is printed when the following code runs: Hero kennedy = new Hero("Meeks"); List<PowerUp> powerUps = new ArrayList<PowerUp>(); powerUps.add(new NikeFrees()); powerUps.add(new AirJordans(23.0)); kennedy.setPowerUp(powerUps.get(1)); kennedy.run(); // Meeks ran 23 tiles. public class AirJordans implements PowerUp { private double _speed; public AirJordans(double speed) { _speed = speed; } public double speed() { return _speed; public boolean invincible() { return true;

public class RecursiveTraversal { public void traverse(String[] a, int i) { System.out.println(a[i]); this.traverse(a, i); } // Q6. What is the output when the following code runs. RecursiveTraversal q = new RecursiveTraversal(); String[] strings = new String[] { "hello", "world", "!" }; q.traverse(strings, 0); // Stack Overflow Error

public class RecursiveTraversal { public void traverse(String[] a, int i) { if (i < a.length) { System.out.println(a[i]); this.traverse(a, i + 1); } // Q7. What is the output when the following code runs. RecursiveTraversal q = new RecursiveTraversal(); String[] strings = new String[] { "hello", "world", "!" }; q.traverse(strings, 0); // Hello / World / ! / ! / World / Hello

public class Funk { public int f(int x) { if (x <= 1) { return x; } else { return this.f(x - 1) + this.f(x - 2); } // 8. What is the output? Funk o = new Funk(); System.out.println(o.f(1)); System.out.println(o.f(2)); System.out.println(o.f(3)); System.out.println(o.f(4)); System.out.println(o.f(5)); System.out.println(o.f(6)); // 1, 1, 2, 3, 5, 8

Arbitrary a = new Arbitrary(10); a.zero(i); System.out.println(i); public class Arbitrary { private int _i; public Arbitrary(int i) { _i = i; } public void zero(int i) { i = 0; public void subtract(int x) { _i = _i - x; public void difference(Arbitrary a) { a.subtract(this.getI()); public int getI() { return _i; public String toString() { return "i: " + _i; 9. Given the code below and class to the left, which of the following expressions is NOT used as an argument? int i = 10; Arbitrary a = new Arbitrary(10); a.zero(i); System.out.println(i); System.out.println(a); i 10 this.getI() x a

// 10. What is the output of the following? int i = 3; public class Arbitrary { private int _i; public Arbitrary(int i) { _i = i; } public void zero(int i) { i = 0; public void subtract(int x) { _i = _i - x; public void difference(Arbitrary a) { a.subtract(this.getI()); public int getI() { return _i; public String toString() { return "i: " + _i; // 10. What is the output of the following? int i = 3; Arbitrary a = new Arbitrary(5); a.zero(i); System.out.println("i: " + i); System.out.println(a); // i: 3 // i: 5

// 11. What is the output of the following? public class Arbitrary { private int _i; public Arbitrary(int i) { _i = i; } public void zero(int i) { i = 0; public void subtract(int x) { _i = _i - x; public void difference(Arbitrary a) { a.subtract(this.getI()); public int getI() { return _i; public String toString() { return "i: " + _i; // 11. What is the output of the following? Arbitrary a = new Arbitrary(5); Arbitrary b = new Arbitrary(7); a.subtract(b.getI()); System.out.println(a); // i: -2

// 12. What is the output of the following? public class Arbitrary { private int _i; public Arbitrary(int i) { _i = i; } public void zero(int i) { i = 0; public void subtract(int x) { _i = _i - x; public void difference(Arbitrary a) { a.subtract(this.getI()); public int getI() { return _i; public String toString() { return "i: " + _i; // 12. What is the output of the following? Arbitrary a = new Arbitrary(5); Arbitrary b = a; a.subtract(2); b.subtract(3); System.out.println(b); // i: 0

// 13. What is the output of the following? public class Arbitrary { private int _i; public Arbitrary(int i) { _i = i; } public void zero(int i) { i = 0; public void subtract(int x) { _i = _i - x; public void difference(Arbitrary a) { a.subtract(this.getI()); public int getI() { return _i; public String toString() { return "i: " + _i; // 13. What is the output of the following? Arbitrary a = new Arbitrary(10); Arbitrary b = new Arbitrary(15); a.difference(b); System.out.println(a); System.out.println(b); // i: 10 / i: 5

14. What is the output of the following code? String[] array = new String[1]; List<String> list = new ArrayList<String>(); list.add("lets"); list.add("go"); for (int i = 0; i < list.size(); i++) { array[i] = list.get(i) + " *clap*"; } for (int i = 0; i < array.length; i++) { System.out.println(a[i]); // Error: ArrayIndexOutOfBoundsException

15. Given the following array, what is the 2nd index you would test if you were using Binary Search to determine whether the array contains the String "Geronimo"? 1 2 3 4 5 6 7 8 Apple Banana Cherry Donut Fig Jupiter Orange Prunes Strawberry 4 is the 1st index tested. 6 is the 2nd index to test.

4 indices would need to be tested: 4, 6, 7, 8 16. Using Binary Search, how many indices would you need to check in order to know if the following array contained the String "Tango"? 1 2 3 4 5 6 7 8 Apple Banana Cherry Donut Fig Jupiter Orange Prunes Strawberry 4 indices would need to be tested: 4, 6, 7, 8

this.index("chapel"); // 2 private int index(String s) { char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' }; List<Integer> indices = new ArrayList<Integer>(); for (int i = 0; i < vowels.length; i++) { indices.add(s.indexOf(vowels[i])); } int min = s.length(); for (int x : indices) { if (x < min && x != -1) { min = x; return min; The String class' indexOf method will return the first index of a particular character in a String. If that character does not exist in the String, it will return -1. 17. What are the return values of calling the method to the left with the following Strings: this.index("bc"); // 2 this.index("ab"); // 0 this.index("ba"); // 1 this.index("chapel"); // 2

The String class' substring method has the following signature: String substring(int start, int end); It returns the characters of the original string beginning with index start and ending with index end – 1. public class Translator { public String translate(String input) { int idx = this.index(input); String head = input.substring(0, idx); String tail = input.substring(idx, input.length()); return tail + head + "ay"; } private int index(String s) { char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' }; List<Integer> indices = new ArrayList<Integer>(); for (int i = 0; i < vowels.length; i++) { indices.add(s.indexOf(vowels[i])); int min = s.length(); for (int x : indices) { if (x < min && x != -1) { min = x; return min; 18. What is the output of the following code? Translator t = new Translator(); System.out.println(t.translate("chapel")); // apelchay System.out.println(t.translate("hill")); // illhay System.out.println(t.translate("shy")); // shyay