Intro Computer Science Mock Session Scenarios

Contents

Scenario 1:

Scenario 2:

Scenario 3:

Scenario 1:

Consider the following class definition:

public abstract class Animal {

protected String m_color;

public String getColor() {

returnm_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 firstGreenthat takes a list of Animalobjects 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. We're looking for syntax here.

public void makeSound() {

System.out.println("Bark");

}

}

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

publicbooleanisPalindrome(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 whereStudent and Teacher are both types of People. It cannot exist without all three of those. ASchoolis assigned a non-negativeinteger-valued grade which represents how strong the school is. ADistrict is made up of many Schools.

Design allclasses for this scenario showcasing your knowledge of inheritance and aggregation. Then write a method

publicintgetTopGrade(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 gradeand three different Lists or ArrayListsof Student, Teacher, and Subject objects and should also see that Student and Teacher are both extensions of People.Each member of Schoolshould 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)

publicintgetTopGrade(District district) {

inttopGrade = 0;

for (School school : district.getSchools())

if (school.getGrade() > topGrade)

topGrade = school.getGrade();

returntopGrade ;

}