Computer Science 523
Advanced Programming
Summer 2014, The College of Saint Rose
Spells BlueJ Project
Click here to download a BlueJ project for Spells.
Spells Source Code
The Java source code for Spells is below. Click on a file name to download it.
/* $Id: Spells.java 2377 2014-06-10 03:12:01Z terescoj $ */
/*
Spell lookup program to demonstrate the use of Associations
and ArrayLists.
Special thanks to J.K. Rowling.
@author Jim Teresco, terescoj@strose.edu
*/
import structure5.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Spells {
public static void main(String[] args) {
// build the list of magic spells we know
ArrayList<Association<String,String>> spells = new ArrayList<Association<String,String>>();
spells.add(new Association<String,String>("Reparo","Fixes damaged object"));
spells.add(new Association<String,String>("Evanesco","Makes something vanish"));
spells.add(new Association<String,String>("Expelliarmus","Disarm opponent"));
spells.add(new Association<String,String>("Accio","Summon object"));
spells.add(new Association<String,String>("Alohomora","Open a locked door"));
spells.add(new Association<String,String>("Lumos","Illuminate wand"));
spells.add(new Association<String,String>("Crucio","Inflict great pain"));
spells.add(new Association<String,String>("Engorgio","Cause target to swell"));
spells.add(new Association<String,String>("Immobulus","Stop an object's motion"));
spells.add(new Association<String,String>("Incendio","Start a fire"));
// we play a little game matching spells to descriptions
// until an invalid spell is specified
Scanner keyboard = new Scanner(System.in);
int spellnum = 0;
while (spellnum >= 0) {
System.out.print("Which spell will you use? ");
String spellName = keyboard.next();
spellnum = spells.indexOf(new Association<String,String>(spellName));
if (spellnum >= 0) {
Association<String,String> spell = spells.get(spellnum);
System.out.println(spell.getValue());
}
else {
System.out.println("Your wand doesn't know that one. It explodes. Bye!");
}
}
}
}