The CS self-placement is intended to evaluate the skills necessary for you to succeed in Data Structures (COMP 229). Newcomers to computer science should enroll in Fundamentals of Computer Science (COMP 131), which is designed for students with no CS experience.

The self-placement should be completed using the Java programming language. If you are unfamiliar with Java, or are otherwise not fluent in the language, we strongly recommend some studying before attempting this self-placement.

The self-placement consists of two programming questions in Java. The first question looks at general problem solving and control flow, while the second question requires object oriented programming. We have provided a test case for each question, but be aware that they do not comprehensively check your code for bugs. We highly encourage you to write additional test cases, and in particular determine what (if any) edge cases exist and making sure that your code handles them appropriately.

The self-placement is designed to take two hours, with open notes. This means you are allowed to use notes from your previous classes, also use the internet to look up syntax and remind yourself of programming concepts. Do not directly search for solutions to these questions, nor give the questions to GPT. Keep in mind that the goal this self-placement is to determine whether you are ready for Data Structures; submitting code you do not understand will only set you up for failure in the course. We trust you to be honest with both the time limit and the open notes policy.

When you are done with the self-placement, please email Professor Justin Li ( with your code. You will be asked to set up a meeting to talk about your solution, at which point we will determine whether you can be placed into Data Structures.

Starter code is available at

  1. Write a function rainfall(int[]) that takes an array of int, which represent the daily rainfall amounts as measured by field instrument. The data may contain negative numbers, which are errors in the instrument, and should be ignored. The number -999 indicates the end of the data of interest. The function should return the average (as a double) of the non-negative values in the list up to the first -999 (if it shows up).


    int[] data = {1, 5, 7, -2, 1, -999, 4};

    The code above should print out 3.5, because (1 + 5 + 7 + 1) / 4 = 3.5. The -2 is ignored, and since we get to -999 before the final number, it is ignored as well.

  2. Congratulations on your new job as a programmer for Red Woof Inn, the country's leading pet hotel! Alright, so the chain only has one location, but every business has to start somewhere. Red Woof Inn has asked you to create their check-in/check-out system, which will keep track of where the animal guests are staying and which rooms are available. With your help, Red Woof Inn will be the top dog in the animal hostel business in no time.

    You need to create four classes in total: three which represent animals, and one which represents the hotel. The rooms in the pet hotels are simply numbered 0, 1, 2, and so on, up to however many rooms exist. Pets can checkIn and checkOut of the hotel, and staff can also look up who occupies which room and which pet is staying where.

    You must implement the following public methods in the Pet class:

    • Pet(String species, String name) - The constructor. The two arguments are the species and name of the pet, respectively.

    • String getSpecies() - Returns the species of the pet.

    • String getName() - Returns the name of the pet.

    • void makeSound() - Prints "I love the Beach Boys!".

    • boolean equals(Pet other) - Returns true if two pets are the same, or false otherwise. For the purpose of this problem, two pets are the same if they are the same species and have the same name.

    Additionally, you must create two subclasses of Pet: Duck and Goose. Their species should be "duck" and "goose" respectively, and have constructors with only one argument (Duck(String name) and Goose(String name)). They should also override the makeSound() function to print "quack!" and "honk!".

    Finally, you must implement the following public methods in the PetHotel class:

    • PetHotel(int size) - The constructor. The single argument is a non-negative integer that represents the total number of rooms in this hotel.

    • int numRoomsAvailable() - Gets the number of unoccupied rooms in this hotel.

    • boolean checkIn(Pet pet) - Checks in a pet. A pet will always get the empty room with the lowest room number. This function returns true if there is an empty room for the pet, and returns false otherwise.

    • boolean checkOut(Pet pet) - Checks out a pet. The room that was occupied by this pet becomes empty. This function returns true if the pet is staying at the hotel (and so can successfully check out), and returns false otherwise.

    • Pet getOccupantOf(int roomNumber) - Gets the occupant of a particular room. If an invalid roomNumber is given, this function should return null.

    • int getRoomOf(Pet pet) - Gets the room of a specific pet. If the pet is not staying at this hotel, this function should return -1.

    You should add member variables to these classes as necessary to store data. We have provided a main() function in the PetHotel class that demonstrates how these methods might be used. A correct implementation of these classes should give the return values provided in the comments.

Contact Computer Science
Swan Hall B101