Presentation is loading. Please wait.

Presentation is loading. Please wait.

Midterm 2 Review Lecture 23.

Similar presentations


Presentation on theme: "Midterm 2 Review Lecture 23."— Presentation transcript:

1 Midterm 2 Review Lecture 23

2 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(); }

3 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(); }

4 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

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

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

7 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

8 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

9 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

10 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

11 // 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

12 // 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

13 // 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

14 // 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

15 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

16 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.

17 4 indices would need to be tested: 4, 2, 1, 0.
16. Using Binary Search, how many indices would you need to check in order to know if the following array contained the String "Aardvark"? 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, 2, 1, 0.

18 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

19 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


Download ppt "Midterm 2 Review Lecture 23."

Similar presentations


Ads by Google