Computer Science: Application Code FRQ
AP Java FRQ: MagicalWorld Candy
Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN Java.
Notes:
· Assume that the classes listed in the Quick Reference have been imported where needed.
· Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.
· In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods may not receive full credit.
Consider a hierarchy of classes shown loosely based on the fictional world of Harry Potter, where a Magical is a fictional character in the world, a Student is a student at Hogwarts, and a Gryffindor is a student who resides in the Gryffindor house and a Slytherin is a student who resides in the Slytherin house.
Magical
(abstract)
Student (abstract)
Slytherin
Gryffindor
Thus, a Student is-a Magical,a resident of Gryffindor is-a Student, and a resident of Slytherin is-a Student. The class Magical is an abstract class as shown in the implementation below. Each Magical has a name and a spell that are specified when constructed.
public abstract class Magical
{
private String name;
private String spell;
/** Constructor for the Magical class */
public Magical(String studentName, String studentSpell)
{
name = studentName;
spell = studentSpell;
}
public String getName()
{ return name; }
public String getSpell()
{ return spell; }
public abstract String getCandy();
}
A Student is a Magical who can cast a spell "Lumos!", but who also gets to eat candy from the HoneyDukes Sweet Shop! The type of candy that he / she gets to eat depends on which house he / she belongs to, which means that getCandy cannot be implemented in the Student class. Here is the implementation for the Student class.
public abstract class Student extends Magical
{
//Constructor
public Student(String studentName)
{ super(studentName, "Lumos!"); }
}
(a) The headmaster of Hogwart’s, Professor Dumbledore, will randomly allow a Gryffindor to receive some “Chocoballs”, “Fudge Flies”, “Every Flavour Beans”, and “Chocolate Frogs”. Thus, the Gryffindor class should use a random number to determine which candy a Gryffindor will get to snack on. Assume that the Student class has been correctly defined, and given the class hierarchy shown previously, write a complete Gryffindor class, including its constructor and method(s).
(b) Consider the partial declaration of the MagicalWorld class below.
public class MagicalWorld
{
/** List of Magicals */
private Magical[] magicalList;
·
/**Precondition: Each Magical in magicalList has a getCandy method
* implementation.
* Postcondition: For each Magical in magicalList, its name followed by
* the result of a call to its getCandy method has been printed on a new
* line*/
public void snack()
· { /* to be implemented in part(b) */ }
·
· //Constructor and other methods are not shown
}
· Write the MagicalWorld method snack. So that Dumbledore knows which students will get which candy, a list will be printed for him. For each Magical in MagicalWorld, snack prints a line with the name of the Magical followed by the result of a call to its getCandy method.
·
· Complete the snack method below.
·
· /**Precondition: Each Student in studentList has a getCandy method implementation.
· * Postcondition: For each Student in the studentList array, his / her name is followed
· * by the result of a call to the respective getCandy method and has been printed.
· */