CIS 59 Fall 2014 November 24 Test #2 Part 1: Multiple Choice (50%) - 8 questions For each question, circle one of the options that best answer the question. 1) Which of the following is not true about overloading? a) b) c) d) Overloaded functions share the exact same function name Overloaded functions differ in input parameter types The return type between overloaded functions does not matter Overloaded functions differ in input parameter names 2) Which of the following is true about designing classes that use composition? a) Classes using composition need to know of the existence of other classes b) Classes using composition contain properties that only use simple data types c) Class composition does not encourage code reuse and suggests you write methods your own way even though they may exist in other similar classes d) Classes using composition write very memorable musical scores and essays 3) Which of the following is true about the line of code, “Book[] shelf = new Book[25];” ? a) shelf is an array of 25 allocated Book objects (all 25 Book objects have already been created) b) .shelf is an array designed to hold 25 objects that must be a type of Book c) If a class, “Comic” does not use inheritance, you can say “shelf[0] = new Comic();” d) If “Comic” inherits from “Book”, you can say “Comic c = shelf[0];” 4) Given the inheritance hierarchy, “Vehicle”, “Car extends Vehicle” and “SportsCar extends Car”, which of the following is true? a) The hierarchy can be read as: a “Vehicle” is a type of “Car” and a “Car” is a type of “SportsCar” b) SportsCar is indirectly a type of Vehicle c) “SportsCar” is at the top of the hierarchy because it is the most specific of the 3 types (while “Vehicle” is the most general and is therefore at the bottom of the hierarchy) d) All public methods of “Car” are inherited in the class “Vehicle” 5) A fellow engineer says, “in my subclass, ‘Coffee’ which inherits from class ‘Drink’, I am overriding the ‘consume’ method”. This means which of the following is true? a) The “Drink” class and the “Coffee” class both contain a method called “consume” with the exact same function signature b) “Drink” does not contain a method called consume while “Coffee” does c) “Drink” can contain a method with the signature “void consume()” and Coffee overrides it with a method whose signature is “void consume(int)” d) None of the above is true 6) The class “ThriftStore” inherits from the class “Store”. Which of the following lines of code is not allowed? a) b) c) d) Store s = new ThriftStore(); ThriftStore t = new ThriftStore(); ThriftStore t2 = new Store(); All of the above are allowed 7) The class “Character” contains a property defined as “private int health;” along with the standard get and set methods. The class “Hobbit” inherits from Character. Which of the following is true? a) In the “Hobbit” class, we could not have the code, “health = 100;” b) In the “Hobbit” class, we could not have the code, “setHealth(100);” c) Since the “Character” class is the parent class to “Hobbit”, it can access all private properties in that class d) None of the above are true 8) The abstract class “Shape3D” contains a method “public abstract double getVolume();” and is the parent class to concrete subclasses, “Pyramid”, “Cone” and “Sphere” that are all implemented correctly. Which of the following is true? a) “Pyramid” contains code for “getVolume()” while Sphere overrides it by renaming it as “getSize()” b) The array “Shape3D[] solids = new Shape3D[100];” can safely have code like “solids[n].getVolume()” as long as each object created in the array is a Pyramid, Cone or Sphere in this case. c) I can create another class “Cube” that does not inherit from Shape3D, but I would still have to create a “double getVolume()” method d) None of the above are true Part 2: Actual Output (25%) - 2 program samples All code in the expected output section is syntactically correct and contains no compile errors. The answer, "this code would not run" is not expected. If there is a typo, let me know and I will correct it on the board. If there is a logic error that you see, that may be there on purpose. Your task is to provide the expected output from the code given (not what you think the expected output is supposed to be). Problem 1: What is the output of this code? Assume the use of the parent class “Student” and subclass “HighSchoolStudent” are correct and that all method and constructor calls are valid. public static void main(String[] args) { Student s = new Student(“John Doe”); // Sets Name of Student CollegeStudent c = new CollegeStudent(“Jane Doe”, “Biology”); // Sets name and major of CollegeStudent respectively System.out.println(s.getName()); System.out.println(c.getName()); System.out.println(c.getMajor()); s = c; System.out.println(s.getName()); System.out.println(c.getName()); System.out.println(c.getMajor()); } Output: John Doe Jane Doe Biology Jane Doe Jane Doe Biology Problem 2: Assume the parent class “Home” is abstract with an abstract method, “double calculateValue();” and a property “private double value;”. Next, the subclasses “House” and “Apartment” contain the code below. Assume all classes contain standard get and set methods. After analyzing those classes, what is the output of the code in the main function (remember to analyze what the code will output and not what it should output)? House.java public class House extends Home { private int age; public double calculateValue() { return getValue() + (0.1 * age * getValue()); } } Apartment.java public class Apartment extends Home { private int numberOfDwellings; public double calculateValue() { return getValue() + (numberOfDwellings * 200.00); } } Main program public static void main(String[] args) { Home[] neighborhood = new Home[20]; House h1 = new House(); Apartment a = new Apartment(); House h2 = new House(); h1.setValue(500000.00); h1.setAge(20); a.setValue(750000.00); a.setNumberOfDwellings(10); h2.setValue(1000000.00); h2.setAge(10); neighborhood[0] = h1; neighborhood[1] = a; neighborhood[2] = h2; double total = 0.0; for (int c = 0; c < 3; c++) { double currentValue = neighborhood[c].calculateValue(); System.out.printf(“Value of home %d: %f\n”, (c+1), currentValue); total += currentValue; } System.out.printf(“Average value is %f\n”, total / 2.0); } Value of home 1: 1500000.000000 Value of home 2: 752000.000000 Value of home 3: 2000000.000000 Average value is 2126000.000000 Part 3: Code Errors (25%) - 1 Compile error, 1 Run-time error The following code snippets contain errors that are found by the compiler. Circle the areas in the code where the errors are and describe what code change must be made to fix each one Athlete.java public abstract class Athlete { private int yearsPlayed; public int getYearsPlayed() { return yearsPlayed; } public abstract int calculateYearsLeft(); } FootballPlayer.java public class FootballPlayer extends Athlete { private int age; public int calculateYearsLeft(); { return 40 – age - yearsPlayed; } } BaseballPlayer.java public class BaseballPlayer extends Athlete { public int calculateYearsLeft(int age) { return 20 – getYearsPlayed(); } } No semicolon yearsPlayed should be getYearsPlayed() (or yearsPlayed in parent class should be protected) int age should be removed (or BaseballPlayer should be abstract) Assume the compiler errors in the code of the previous problem are now fixed. The following main program code contains errors that are found at run time. The intent of the program is also provided. Based on the intent and the actual output that will appear, describe the problem with the code and what change(s) must be made to meet the original intent. The following program is intending to determine the average number of years left for the football and baseball players. For test data, the main program is using 5 and 10 years played for each player respectively. All players are expected to be set at 25 years old. Based on the expected formulas, the test output should be: Years left for football player 1: 10 Years left for football player 2: 5 Average number of years left for football players: 7.5 Years left for baseball player 1: 15 Years left for baseball player 2: 10 Average number of years left for baseball players: 12.5 The code for the main program is on the next page. Describe here what the actual output will be from the main and what changes you would make to fix them: Years left for Years left for Average number Years left for Years left for Average number football football of years baseball baseball of years player 1: 14 player 2: 9 left for football players: 11.5 player 0: 15 player 1: 10 left for baseball players: 0.0 Player[] f = new Player[2]; Player[] b = new Player[2]; f[0] f[1] b[0] b[1] = = = = new new new new FootballPlayer(); FootballPlayer(); BaseballPlayer(); BaseballPlayer(); f[0].setYearsPlayed(5); f[1].setYearsPlayed(10); b[0].setYearsPlayed(5); b[1].setYearsPlayed(10); for (int c = 0; c < 2; c++) { f[c].setAge(21); b[c].setAge(25); } double total = 0.0; for (int c = 0; c < 2; c++) { System.out.printf(“Years left for football player %d: %d\n”, (c+1), f[c].calculateYearsLeft()); total += f[c].calculateYearsLeft(); } System.out.printf(“Average number of years left for football players: %d\n”, total / 2.0); double average = 0.0; for (int c = 0; c < 2; c++) { System.out.printf(“Years left for baseball player %d: %d\n”, c, b[c].calculateYearsLeft()); total += f[c].calculateYearsLeft(); } System.out.printf(“Average number of years left for baseball players: %d\n”, average); f[c].setAge(21) should be f[c].setAge(25) The %d’s should really be %f The c should be c+1 to correct the output for the baseball players total could be average and it should be += b[c].calculateYearsLeft(); - if you do this, then you also need “average /= 2.0;” after the loop