Intro Computer Science Mock Session Scenarios Contents Scenario 1: .................................................................................................................................................................... 2 Scenario 2: .................................................................................................................................................................... 3 Scenario 3: .................................................................................................................................................................... 4 Scenario 1: Consider the following class definition: public abstract class Animal { protected String m_color; public String getColor() { return m_color; } public abstract void makeSound(); } Using this class, 1. Create classes Dog and Cat which make their respective noises. Code should showcase knowledge of inheritance and good class design. 2. Write a method firstGreen that takes a list of Animal objects and returns the first one with a color of "green". Sample Answer: 1. public class Dog extends Animal{ //extends is important as well as having at least some kind of constructor. //In fact, the "student" may want to ask about a constructor. public Dog() { m_color = "none"; } public Dog(String color) { m_color = color; } //The "student" may ask about directions here. public void makeSound() { System.out.println("Bark"); } We're looking for syntax here. } //Do the same thing for Cat though probably as the student you may "get it" and want to move to #2. 2. // Very simple traversal of a list here. Look for the syntax of the method, the foreach loop being appropriate here since we change nothing, and using animal.getColor instead of animal.color and the .equals for content. "Student" may ask what if an animal is not found for clarification. public Animal firstGreen(List<Animal> animals) { for (Animal animal : animals) if(animal.getColor().equals("green")) return animal; return null; } Scenario 2: A phrase is a palindrome if it reads the same way forwards as in reverse. So "rise to vote sir" and "racecar" would both be palindromes. Implement the following method to check whether a phrase represented by an ArrayList of Strings is a palindrome or not. You are only allowed to use raw data type methods and ArrayList operations and no other libraries or built-in methods. You may implement any helper methods that you need though. public boolean isPalindrome(ArrayList<String> phrase) { } Sample Answer: There are a ton of solutions here, with the main themes being a recursive route or an iterative route as well as basic knowledge of the String class. Make sure the applicant can handle edge cases like an empty ArrayList or an empty phrase, etc.. Here are a few ideas: 1. "Flatten" phrase into a char[] array built up from each string element in phrase. Then using recursion or iteration, make sure char[] is the same as its reverse using raw character comparisons. 2. Create one String object which represents all the strings in phrase concatenated. You now need to write a simple method called reverse(String word) using iteration or recursion. Scenario 3: A School consists of Students, Teachers, and Subjects where Student and Teacher are both types of People. It cannot exist without all three of those. A School is assigned a non-negative integer-valued grade which represents how strong the school is. A District is made up of many Schools. Design all classes for this scenario showcasing your knowledge of inheritance and aggregation. Then write a method public int getTopGrade(District district) that takes a District and returns the grade of the top School. Answer: The applicant should see that School has a field member of grade and three different Lists or ArrayLists of Student, Teacher, and Subject objects and should also see that Student and Teacher are both extensions of People. Each member of School should have public accessors and there should be a constructor. As the student you may want to "understand" the various members and then jump to the method implementation below (sample) public int getTopGrade(District district) { int topGrade = 0; for (School school : district.getSchools()) if (school.getGrade() > topGrade) topGrade = school.getGrade(); return topGrade ; }