Exception in thread error when running Interface - java

I have written a code in which I have made an interface and a class that implements that interface. Now when I run the code, i give my self 2 options i.e. 1 to Enter string and 2 to quit. Now when i run the code, if i select 1, it lets me put in string, but after this when i press 0, it gives me error: Exception in thread "main" java.lang. Can some body please help me figure out what I am missing here. Below is the code:
import java.util.ArrayList;
import java.util.List;
public interface ITimsSaveable {
List<String> write(); // this will save the data;
void read (List<String> savedValues); //this will print the data
}
import java.util.ArrayList;
import java.util.List;
public class TimsPlayers implements ITimsSaveable {
private String name;
private int hitPoints;
private int strength;
private String weapon;
public TimsPlayers(String name, int hitPoints, int strength) {
this.name = name;
this.hitPoints = hitPoints;
this.strength = strength;
this.weapon = "Sword";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHitPoints() {
return hitPoints;
}
public void setHitPoints(int hitPoints) {
this.hitPoints = hitPoints;
}
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
public String getWeapon() {
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
//now go to generate > to string method
#Override
public String toString() {
return "TimsPlayers{" +
"name='" + name + '\'' +
", hitPoints=" + hitPoints +
", strength=" + strength +
", weapon='" + weapon + '\'' +
'}';
}
//now let us create stubs, i.e. the empty methods from interface
#Override
public List<String> write() {
List<String > values = new ArrayList<String >();
values.add(0,this.name);
values.add(1,""+this.hitPoints); //"" is quick trick to convert int to String
values.add(2,""+this.strength);
values.add(3,this.weapon);
return values;
}
#Override
public void read(List<String> savedValues) {
if ((savedValues != null) && (savedValues.size() > 0)) {
this.name = savedValues.get(0);
this.hitPoints = Integer.parseInt(savedValues.get(1));
this.strength = Integer.parseInt(savedValues.get(2));
this.weapon = savedValues.get(3);
}
// now let us go back to our main class
}
}
public class Main {
public static void main(String[] args) {
TimsPlayers tim = new TimsPlayers("Tim",10,15);
System.out.println(tim.toString());
saveObject(tim);
tim.setHitPoints(8);
System.out.println(tim);
tim.setWeapon("Stormbringer");
saveObject(tim);
loadObject(tim);
System.out.println(tim);
}
public static ArrayList<String> readValues() {
ArrayList<String> values = new ArrayList<String>();
Scanner scanner = new Scanner(System.in);
boolean quit = false;
int index = 0;
System.out.println("Choose\n" +
"1 to enter a string\n" +
"0 to quit");
while (!quit) {
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 0:
quit = true;
break;
case 1:
System.out.print("Enter a string: ");
String stringInput = scanner.nextLine();
values.add(index, stringInput);
index++;
break;
}
}
return values;
}
public static void saveObject(ITimsSaveable objectToSave) { //ITims is the interface that is being implemented by the player
//class,so we can use ITims as object type
for (int i = 0; i < objectToSave.write().size(); i++) {
System.out.println("Saving " + objectToSave.write().get(i) + " to storage device");
}
}
public static void loadObject (ITimsSaveable objectToLoad) {
ArrayList<String > values = readValues();
objectToLoad.read(values);
}
}
Stacktrace:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:373)
at java.base/java.util.ArrayList.get(ArrayList.java:427)
at org.mm.sandbox.so.TimsPlayers.read(TimsPlayers.java:77)
at org.mm.sandbox.so.Main.loadObject(Main.java:62)
at org.mm.sandbox.so.Main.main(Main.java:17)

This method
#Override
public void read(List<String> savedValues) {
if ((savedValues != null) && (savedValues.size() > 0)) {
this.name = savedValues.get(0);
this.hitPoints = Integer.parseInt(savedValues.get(1));
this.strength = Integer.parseInt(savedValues.get(2));
this.weapon = savedValues.get(3);
}
// now let us go back to our main class
}
expects the savedValues to be completely filled, i.e. having 4 elements - name, HP, strength and weapon. You do savedValues.size() > 0 but that doesn't mean you didn't just input one of the required and then stopped and it still tries to access an element which does not exist - this.hitPoints = Integer.parseInt(savedValues.get(1));

Related

Creating an printing an object that stores strings and integers in java

I have the following problem. Five classes are interacting with each other. Two of theme are doing fine. But with the creating of an Object of one class (Ticket) in my main class Event (getting user input from another class (UserInput), an processing this in the costructor) i have now problem to display the results.
Main class Event with main methode
import java.util.ArrayList;
public class Event {
private static String artistName;
private static int artistSalary;
private Language language;
private static ArrayList<String> trackList;
private InputReader inputReader;
private Ticket ticket;
private int amountOfTicketCategories;
private static Object[] ticketList;
private static int index;
public Event() {
}
public Event(Artist artist, Ticket ticket) {
artistName = artist.getArtistName();
artistSalary = artist.getArtistSalary();
trackList = artist.getArrayList();
for (index = 0; index < amountOfTicketCategories; index++) {
ticketList[index] = ticket.getTicket();
ticketList[index].toString();
}
}
public void chooseWhatToDefine() {
language = new Language();
language.whatToSpecify();
}
public void setTicketPrice(String ticketCategory, int ticketPrice) {
}
public void displayArtist(String artistName, int artistSalary) {
language = new Language();
language.displayArtistAndSalary(artistName, artistSalary);
}
public void displayTracklist(ArrayList<String> trackList) {
language = new Language();
language.displayTrackList(trackList);
}
public void displayTickets(Object[] ticketList) {
language = new Language();
language.displayTicket(ticketList);
}
public static void main(String[] args) {
Event event1 = new Event(new Artist(), new Ticket());
event1.displayArtist(artistName, artistSalary);
event1.displayTracklist(trackList);
event1.displayTickets(ticketList);
}
}
Ticket class with constructor that initalize the class with the user input comming from the InputReader class, and creates an object of Strings and Integers.
import java.util.Arrays;
public class Ticket {
private static String ticketCategory;
private static int ticketAmount;
private static int ticketPrice;
private InputReader inputReader;
private int amountOfTicketCategories;
private int index;
private Ticket[] ticketList;
public Ticket(String ticketCategory,int ticketAmount,int ticketPrice) {
}
public Ticket() {
inputReader = new InputReader();
inputReader.specifyTicketCategories();
ticketList = new Ticket[amountOfTicketCategories];
for (index = 0; index < amountOfTicketCategories; index++) {
inputReader.specifyTicket(ticketCategory, ticketAmount, ticketPrice);
ticketList[index] = new Ticket(ticketCategory, ticketAmount, ticketPrice);
}
}
public String toString() {
return("TicketCategory: " + ticketCategory + "Amount of Tickets: " + ticketAmount + "Ticket Price: " +ticketPrice);
}
public Object getTicket() {
return ticketList[index];
}
public int getAmountOfTicketCategories() {
amountOfTicketCategories = inputReader.specifyTicketCategories();
return amountOfTicketCategories;
}
}
InptReader class that processes the user input:
import java.util.ArrayList;
import java.util.Scanner;
public class InputReader {
private Scanner sc;
private Language language;
private ArrayList <String> tracks;
public InputReader() {
tracks = new ArrayList<String>();
language = new Language();
sc = new Scanner(System.in);
}
public int specifyTicketCategories() {
language.defineAmountOfTicketCategories();
return sc.nextInt();
}
public void specifyTicket(String ticketCategory, int ticketAmount, int ticketPrice) {
language.specifyTicketCategory();
ticketCategory = sc.next();
language.specifyTicketAmount();
ticketAmount = sc.nextInt();
language.specifyTicketPrice();
ticketPrice = sc.nextInt();
}
public int amountOfTickets() {
return sc.nextInt();
}
public int ticketPrice() {
return sc.nextInt();
}
public String readName() {
language.specifyArtist();
return sc.nextLine();
}
public int readInteger() {
language.specifyArtistSalary();
return sc.nextInt();
}
public void addTitle() {
int anzahlSongs = 3;
int index = 0;
while (index < anzahlSongs) {
language.specifyTrackList();
tracks.add(sc.nextLine());
index++;
}
}
public ArrayList<String> getArray() {
return tracks;
}
}
Language class that consists of the language statements
import java.util.ArrayList;
public class Language {
public Language () {
}
public void whatToSpecify() {
System.out.println("What would you like to specify fist? For Artist press 1, for Ticket press 2");
}
public void specifyArtist() {
System.out.println("Who is the artist? ");
}
public void specifyArtistSalary() {
System.out.println("How much does the artist earn? ");
}
public void displayTicket(Object[] ticketList) {
System.out.println("Ticketlist: " + ticketList);
}
public void displayArtistAndSalary(String artistName, int artistSalary) {
System.out.println("Artist: " + artistName + " " + "Salary: " + artistSalary);
}
public void displayTrackList(ArrayList<String> trackList) {
System.out.println("Tracklist: " + trackList);
}
public void specifyTicketCategory() {
System.out.println("What is the ticket category? ");
}
public void specifyTicketAmount() {
System.out.println("What ist the amount of tickets? ");
}
public void specifyTicketPrice() {
System.out.println("What is the price for your ticket?");
}
public void specifyTrackList() {
System.out.println("Add title: ");
}
public void defineAmountOfTicketCategories() {
System.out.println("How many ticket categories you like to set up?");
}
public void line() {
System.out.println("***********************************");
}
}
Artist class that that has creates an instance of an artist in the main class (same idea as for ticket) but with other variables and parameters.
import java.util.ArrayList;
public class Artist {
private int artistSalary;
private String artistName;
private InputReader inputReader;
ArrayList <String> trackList;
public Artist() {
inputReader = new InputReader();
artistName = inputReader.readName();
artistSalary = inputReader.readInteger();
inputReader.addTitle();
trackList = inputReader.getArray();
trackList.remove(2);
}
public String getArtistName() {
return artistName;
}
public int getArtistSalary() {
return artistSalary;
}
public ArrayList<String> getArrayList(){
return trackList;
}
}
Output in the console:
Add title:
Hello
Add title:
Hello
How many ticket categories you like to set up?
5
Artist: David Salary: 5000
Tracklist: [, Hello]
Ticketlist: null
First of all, in the Ticket class's constructor, you use the other constructor (The one with the 3 arguments), which has an empty body.
ticketList[index] = new Ticket(ticketCategory, ticketAmount, ticketPrice);
public Ticket(String ticketCategory,int ticketAmount,int ticketPrice) {
//this is empty
}
That means you're creating an object with.. nothing in it (null variables).
Try this:
public Ticket(String ticketCategory,int ticketAmount,int ticketPrice) {
this.ticketCategory = ticketCategory;
this.ticketAmount = ticketAmount;
this.ticketPrice = ticketPrice;
}
Then, your getTicket method is wrong. You never define the "index" integer in your Ticket class.
public Object getTicket() {
return ticketList[index];
}
Where "index" is undefined.
The ticketList should not be in the Ticket class => each time you create a Ticket instance, it will probably not be the same as the previous one.

Any reason why I can't create a class? I'm trying to have a Animal array to hold different animals

I am trying to create a animal array to try and hold the information of different types of animals/owners. Been trying to solve this for 2 hours reading the book but nothing is working. Can anyone point me to the right direction? Also how would I go about importing information from a URL to a array?
import java.net.URL;
import java.math.BigInteger;
import java.net.URL;
import java.net.HttpURLConnection;
import static java.util.Arrays.sort;
public class janesj_Program5 {
public static void main(String[] args) {
Animal[] j = new Animal[1];
storeFile(j);
sort(j);
printArray(j);
}
public static class Animal {
String OwnerName;
int birthYear;
public int billBalance;
String Species;
String feature;
public Animal() {}
public Animal(String OwnerName,int birthYear,int billBalance,String Species,String feature) {
this.OwnerName = OwnerName;
this.birthYear = birthYear;
this.billBalance = billBalance;
this.Species = Species;
this.feature = feature;
}
public int getBalance() {
return billBalance;
}
public String toString() {
return OwnerName + "\t" + birthYear + "\t" + getBalance() + "\t" + Species + "\t" + feature;
}
}
public static void storeFile(Animal[] x) {
String URLString = "http://yoda.kean.edu/~pawang/CPS2231/Program5_veterinarian_input.txt";
try {
java.net.URL url = new java.net.URL(URLString);
int count = 0;
Scanner input = new Scanner(url.openStream());
while(input.hasNext()) {
String line = input.nextLine();
count+= line.length();
x = new Animal[count];
}
}catch(java.net.MalformedURLException ex){
System.out.println("Invalid URL");
}
catch(java.io.IOException ex) {
System.out.println("I/O Errors: no such file");
}
}
public static class sorts extends Animal implements Comparator<Animal> {
public int compare(Animal a, Animal b) {
return a.getBalance() - b.getBalance();
}
}
public static void printArray(Animal[] x) {
System.out.println("\t Veterinarian Services Report \t Page:1");
System.out.println(" ==================================");
System.out.println("No\tOwner Name\tYear\tBalance\tSpecies\tLegs\tFeature");
System.out.println("== ==================== ==== ============ ============ ============");
for(int i = 1; i<=x.length;i++) {
System.out.println(i + " " + x.toString());
}
}
}
As so kindly pointed out by MTilsted you really should have your Animal class within its own .java file.
If you plan to create an array of Animal objects where the data to fill those objects is coming from a data file then you need to realize that arrays are of a fixed size, they can't just grow on a whim (at least not without more coding). You would need to know how may animals are contained within the data file so as to properly size your Animal array. Your in luck, by the look of it your data file contains the number of animals within the first line of the file (which needs to be ignored when reading in the actual animal data).
First, make sure the file actually exist. No sense going through heartache if it's not even there or there is something wrong with the connection to its' location. Once done, you can declare and initialize your Animals array to the proper size so as to handle all the data rows you are about to re-read into the array.
Below is your code to demonstrate how this can be accomplished.
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class AnimalCare {
static Animals[] animals; // Declare The Anmimal Array as a class member variable
public static void main(String[] args) {
try {
URL url = new URL("http://yoda.kean.edu/~pawang/CPS2231/Program5_veterinarian_input.txt");
int lines = 0;
// Get the number of lines in file
try (Scanner s = new Scanner(url.openStream())) {
String firstFileLine = "";
while (firstFileLine.equals("")) {
firstFileLine = s.nextLine().trim();
if (!firstFileLine.equals("")) {
lines = Integer.parseInt(firstFileLine);
}
}
}
catch (IOException ex) {
String msg = "";
if (!ex.getMessage().equals(url.toString())) {
msg = "No Network Connection!";
}
else {
msg = "File Not Found! - " + ex.getMessage();
}
System.err.println(msg);
}
// Declare our Animals Array.
animals = new Animals[lines];
// Re-read the data file on network...
try (Scanner s = new Scanner(url.openStream())) {
int i = 0;
String dataLine;
s.nextLine(); // Skip the first line of file!
while (s.hasNextLine()) {
dataLine = s.nextLine().trim();
// Skip blank lines (if any)
if (dataLine.equals("")) {
continue;
}
String[] dataParts = dataLine.split("\\s+");
animals[i] = new Animals(dataParts[0],
Integer.parseInt(dataParts[1]),
Integer.parseInt(dataParts[2]),
dataParts[3],
dataParts[4]);
i++; // Increment i to create the next index value for array
}
// The Animals array is now filled with network file data
}
catch (IOException ex) {
String msg = "";
if (!ex.getMessage().equals(url.toString())) {
msg = "No Network Connection!";
}
else {
msg = "File Not Found! - " + ex.getMessage();
}
System.err.println(msg);
}
}
catch (MalformedURLException ex) {
System.err.println(ex.getMessage());
}
// Display the Animals array within the Console Window...
for (int i = 0; i < animals.length; i++) {
System.out.println(animals[i].toString());
}
}
}
And the Animals Class:
import java.util.Arrays;
public class Animals {
private String OwnerName;
private int birthYear;
private int billBalance;
private String Species;
private String feature;
//----------------- Constructors -------------------
public Animals() { }
public Animals(String OwnerName, int birthYear, int billBalance, String Species, String feature) {
this.OwnerName = OwnerName;
this.birthYear = birthYear;
this.billBalance = billBalance;
this.Species = Species;
this.feature = feature;
}
public Animals(Object[] data) {
if (data.length != 5 || !(data[0] instanceof String) ||
!(data[1] instanceof Integer) || !(data[2] instanceof Integer) ||
!(data[3] instanceof String) || !(data[4] instanceof String)) {
throw new IllegalArgumentException("Error in Animals Constructor data array! "
+ "Insufficiant data or invalid data element!" + System.lineSeparator() +
Arrays.deepToString(data));
}
this.OwnerName = data[0].toString();
this.birthYear = (int) data[1];
this.billBalance = (int) data[2];
this.Species = data[3].toString();
this.feature = data[4].toString();
}
//---------------------------------------------------
public int getBalance() {
return billBalance;
}
public String getOwnerName() {
return OwnerName;
}
public void setOwnerName(String OwnerName) {
this.OwnerName = OwnerName;
}
public int getBirthYear() {
return birthYear;
}
public void setBirthYear(int birthYear) {
this.birthYear = birthYear;
}
public int getBillBalance() {
return billBalance;
}
public void setBillBalance(int billBalance) {
this.billBalance = billBalance;
}
public String getSpecies() {
return Species;
}
public void setSpecies(String Species) {
this.Species = Species;
}
public String getFeature() {
return feature;
}
public void setFeature(String feature) {
this.feature = feature;
}
#Override
public String toString() {
String string = String.format("%-10s %-8d %-8d %-10s %-15s",
OwnerName, birthYear, getBalance(), Species, feature);
return string;
}
}

Java static/nonstatic method

I started working on my first java project, which is a basic RPG, and I have a question regarding the spells. I have an abstract class named Character, which is extended by some subclasses (like Fighter, Mage etc.). Only spellcasters can cast spells. Regarding Spells - I have a class named Spell that describes a spell (it's name, it's effect, mana etc.). All the spells are stored in SpellsList class that has a spellsList list (objects of class Spell). I have an Effect class (very plausible that it will become an interface) that has some effects like "damage" and "heal", but I don't make use of that for the meanwhile, I just want to test that what I have works.
My problem is that Mage's methods addToSpellBook and showSpellBook give a compiler error: java can't find symbol: method addToSepllBook, location: variable hero of type Game.Character. also for showSpellBook. Why and how to fix it ?
(The problem is probably in Mage/Spell/SpellsList class, and not Character which is long, so it's less intimidating :) )
public class Game {
public static void main(String[] args) throws IOException {
CharacterCreator heroCreator = new CharacterCreator();
CharacterCreator.showAllClasses();
Scanner sc = new Scanner(System.in);
int scan = sc.nextInt();
String chosenClass = CharacterCreator.getCharacterClass(scan);
Character hero = CharacterCreator.createCharacter(chosenClass);
try {
hero.displayCharacter();
}catch (Exception e){
System.out.println("Wrong input");
}
if (hero.getCharacterClass().equals("Mage")){
hero.addToSpellBook("Fireball");
hero.showSpellBook();
}
}
}
public class CharacterCreator {
public static Character createCharacter(String chosenClass) {
Character hero = null;
System.out.println("Choose Name:");
Scanner nameIn = new Scanner(System.in);
String name = nameIn.next();
switch (chosenClass) {
case "Fighter":
return new Fighter(name);
case "Rogue":
return new Rogue(name);
case "Mage":
return new Mage(name);
case "Cleric":
return new Cleric(name);
case "def":
System.out.println("Wrong input");
return null;
default:
return null;
}
}
public static void showAllClasses(){
System.out.println("Choose a character: ");
System.out.println("1. Fighter");
System.out.println("2. Rogue");
System.out.println("3. Mage");
System.out.println("4. Cleric");
}
public static String getCharacterClass(int scan){
String classIn;
switch (scan) {
case 1:
classIn = "Fighter";
break;
case 2:
classIn = "Rogue";
break;
case 3:
classIn = "Mage";
break;
case 4:
classIn = "Cleric";
break;
default:
System.out.println("Enter again");
classIn = "def";
}
return classIn;
}
}
abstract public class Character {
private String name;
private String characterClass;
private int level;
private int hp;
private int currentHp;
private int armorClass;
private long xp;
/*private int BAB; /*Base attack bonus*/
private int strength;
private int constitution;
private int dexterity;
private int intelligence;
private int wisdom;
private int charisma;
protected Character(String name){
setName(name);
setCharacterClass("Class");
setLevel(1);
setStrength(10);
setConstitution(10);
setDexterity(10);
setIntelligence(10);
setWisdom(10);
setCharisma(10);
setHp(0);
setCurrentHp(getHp());
setArmorClass(10);
setXp(0);
}
void displayCharacter() throws IOException{
System.out.print("\n\n\n");
System.out.println("Name: " + getName());
System.out.println("Class: " + getCharacterClass());
System.out.println("Level: " + getLevel());
System.out.println("HP: " + getHp());
System.out.println("Current HP: " + getCurrentHp());
System.out.println("Armor Class: " + getArmorClass());
System.out.println("***************");
System.out.println("Attributes: ");
System.out.println("Strength: " + getStrength());
System.out.println("Constitution: " + getConstitution());
System.out.println("Dexterity: " + getDexterity());
System.out.println("Intelligence: " + getIntelligence());
System.out.println("Wisdom: " + getWisdom());
System.out.println("Charisma: " + getCharisma());
System.out.println("***************");
System.out.println("XP: " + getXp());
}
public int getModifier(int number){
int mod = (int)((number -10)/2);
return mod;
}
public String getName() { return name; }
public String getCharacterClass() { return characterClass; }
public int getLevel() { return level; }
public int getHp() { return hp; }
public int getCurrentHp() { return currentHp; }
public int getArmorClass() { return armorClass; }
public int getStrength(){ return strength; }
public int getConstitution(){ return constitution; }
public int getDexterity(){ return dexterity; }
public int getIntelligence(){ return intelligence; }
public int getWisdom(){ return wisdom; }
public int getCharisma(){ return charisma;}
public long getXp(){ return xp;}
protected void setName(String Name) { name = Name; }
protected void setCharacterClass(String characterClass) { this.characterClass = characterClass; }
protected void setLevel(int lvl){ level = lvl; }
protected void setHp(int hitPoints){ hp = hitPoints; }
protected void setCurrentHp(int curHp){ currentHp = curHp; }
protected void setArmorClass(int ac){ armorClass = ac; }
protected void setStrength(int str){ strength = str; }
protected void setConstitution(int con){ constitution = con; }
protected void setDexterity( int dex) { dexterity = dex; }
protected void setIntelligence(int intel){ intelligence = intel; }
protected void setWisdom(int wis){ wisdom = wis; }
protected void setCharisma(int cha){charisma = cha; }
protected void setXp(int XP){xp = XP; }
}
public class Mage extends Character {
private List<Spell> spellBook;
public Mage(String name){
super(name);
setName(name);
setCharacterClass("Mage");
setLevel(1);
setStrength(10);
setConstitution(10);
setDexterity(14);
setIntelligence(16);
setWisdom(14);
setCharisma(10);
setHp((int) (4 + getModifier(getConstitution())));
setCurrentHp(getHp());
setArmorClass(10 + getModifier(getDexterity()));
spellBook = null;
}
void addToSpellBook(String spellName){
Spell newSpell;
newSpell = SpellsList.getSpell(spellName);
spellBook.add(newSpell);
}
void showSpellBook(){
for (Iterator<Spell> iter = spellBook.iterator(); iter.hasNext(); ) {
Spell spell = iter.next();
if (spellBook.equals(spell.getSpellName())) {
System.out.println("Spell name: " + spell.getSpellName());
System.out.println("Spell effect: " + spell.getEffect());
}
}
}
}
public class Spell {
private String name;
private int spellLevel;
private String effect;
private int manaCost;
private int duration;
Spell(String name, int spellLevel, String effect, int manaCost, int duration){
this.name = name;
this.spellLevel = spellLevel;
this.effect = effect;
this.manaCost = manaCost;
this.duration= duration;
}
void castSpell(String spellName, Character hero, Character target){
try {
Spell spell = SpellsList.getSpell(spellName);
System.out.println("You casted: " + spellName);
System.out.println("Spell effect: " + spell.effect);
}
catch (Exception e){
System.out.println("No such spell");
}
}
String getSpellName(){ return name; }
int getSpellLevel() {return spellLevel; }
String getEffect(){ return effect; }
int getManaCost(){
return manaCost;
}
int getDuration() { return duration; }
}
public class SpellsList {
static List<Spell> spellsList = new ArrayList<Spell>();
static
{
spellsList.add(new Spell("Fireball", 3, "damage", 5,0 ));
spellsList.add(new Spell("Ice Storm", 4, "damage", 8, 0));
spellsList.add(new Spell("Heal", 2, "heal", 4, 0));
}
static Spell getSpell(String spellName) {
try {
for (Iterator<Spell> iter = spellsList.iterator(); iter.hasNext(); ) {
Spell spell = iter.next();
if (spellName.equals(spell.getSpellName())) {
return spell;
}
}
} catch (Exception e){
System.out.println(spellName + " haven't been found in spells-list");
return null;
}
return null;
}
}
hero is of type Character. You should cast it to Mage. or add addToSpellBook in Character class and override it in Mage class. Something like:
if(hero instanceof Mage)
((Mage) hero).addToSpellBook();
There is a difference between the type of variables when compiling and their class during execution. The problem is that your hero variable is not of type Mage but of type Character and only has access to methods available to any Character.
The compiler also doesn't notice your logic in attempting to make sure hero is an instance of Mage. To tell it you know you have Mage and want to use Mage methods you need to cast.
Your way of verifying is okay, but I would advise using theinstanceof keyword.
if(hero instanceof Mage) {
((Mage)hero).addToSpellBook("Fireball");
((Mage)hero).showSpellBook();
}
You call the methodes addToSpellBook and showSpellBook on the class Character, but you have no methodes with this names in your class Character.

Cannot find symbol in Java application

I am working on a simple text-based rpg battler program as an introduction to Java. I seem to have a decent understanding of the majority of code, but I have ran into a couple of issues.
The issues I am having are in the Project class.
In my switch statement I am trying to use the setSpells() and setArrows() methods and I am getting a "cannot find symbol" error message. I realize that this is probably due to something I have set up incorrectly in the sub-classes, but I am unsure what that is.
The second issue is in the print statement pulling the character name by use of c.getName(). The c part of that statement gives the same error message.
Is there something simple that I am misunderstanding in these situations? Any help resolving this would be appreciated. Thank you.
Here is my main project class file:
package project;
import java.util.Scanner;
public class Project {
public static void main(String[] args) {
System.out.println("Welcome to Lands of the Sun\n");
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
System.out.print("Please choose your class (wizard or elf): \n");
String classChoice = sc.next();
sc.nextLine();
System.out.print("Please choose a name for your " + classChoice + ": ");
String charName = sc.next();
sc.nextLine();
int healthVal = (int) (Math.random() * 10) + 1;
switch (classChoice) {
case "wizard":
{
Character c = new Wizard();
c.setName(charName);
c.setGold(25);
c.setHealth(healthVal);
c.setSpells(10);
break;
}
case "elf":
{
Character c = new Elf();
c.setName(charName);
c.setGold(25);
c.setHealth(healthVal);
c.setArrows(10);
break;
}
}
System.out.print(c.getName());
System.out.print("Continue? (y/n): ");
choice = sc.nextLine();
System.out.println();
}
}
}
Here is my Character class:
package project;
public abstract class Character {
private String name;
private int gold;
private int health;
public static int count = 0;
public Character()
{
name = "";
gold = 0;
health = 0;
}
public Character(String name, int gold, int health) {
this.name = name;
this.gold = gold;
this.health = health;
}
public void setName(String name)
{
this.name = name;
}
public String getName(){
return name;
}
public void setGold(int gold)
{
this.gold = gold;
}
public int getGold()
{
return gold;
}
public void setHealth(int health)
{
this.health = health;
}
public int getHealth()
{
return health;
}
#Override
public String toString()
{
return "Name: " + name + "\n" +
"Gold: " + gold + "\n" +
"Health: " + health + "\n";
}
public static int getCount()
{
return count;
}
public abstract String getDisplayText();
}
Here is my Wizard sub-class:
package project;
public class Wizard extends Character {
private int spells;
public Wizard()
{
super();
spells= 0;
count++;
}
public void setSpells(int spells)
{
this.spells= spells;
}
public int getSpells(){
return spells;
}
#Override
public String getDisplayText()
{
return super.toString() +
"Spells: " + spells+ "\n";
}
}
And finally my Elf sub-class:
package project;
public class Elf extends Character{
private int arrows;
public Elf()
{
super();
arrows = 0;
count++;
}
public void setArrows(int arrows)
{
this.arrows = arrows;
}
public int getArrows(){
return arrows;
}
#Override
public String getDisplayText()
{
return super.toString() +
"Arrows: " + arrows + "\n";
}
}
When you create one of your Characters...
Character c = new Elf();
You are downcasting the instance to "act" like Character, this is very useful feature in Object Oriented Programming, but is causing you issues in this case, as Character does not have the methods you are looking for.
Instead, start by assigning the class to a concrete version of the instance...
Elf elf = new Elf();
Apply the properties you need and then assign it to a Character reference...
Character c = null;
//...
switch (classChoice) {
//...
case "elf":
{
Elf elf = new Elf();
//...
c = elf;
}
}
c = elf;
for example...
Have a look at the section on Polymorphism for more details
Your issue is with these lines
Character c = new Wizard();
....
Character c = new Elf();
The character class itself doesn't have the setFireballs or SetArrows methods. You need to define the object as a wizard or elf in order to get access to said methods... EG:
Elf c = new Elf();
Wizard c = new Wizard();
etc
Character c = new Wizard();
Character has neither a setFireBalls method nor a setArrows method.
I have made the some changes to your code..
here is the final code.
package h;
import java.util.Scanner;
public class he {
public static void main(String[] args) {
System.out.println("Welcome to Lands of the Sun\n");
Character c = new Wizard();
Character e = new Elf();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
System.out.print("Please choose your class (wizard or elf): \n");
String classChoice = sc.next();
sc.nextLine();
System.out.print("Please choose a name for your " + classChoice + ": ");
String charName = sc.next();
sc.nextLine();
int healthVal = (int) (Math.random() * 10) + 1;
switch (classChoice) {
case "wizard":
{
c.setName(charName);
c.setGold(25);
c.setHealth(healthVal);
c.setFireballs(10);
break;
}
case "elf":
{
;
e.setName(charName);
e.setGold(25);
e.setHealth(healthVal);
e.setArrows(10);
break;
}
}
System.out.print(c.getName());
System.out.print("Continue? (y/n): ");
choice = sc.nextLine();
System.out.println();
}
}
}
Wizard class
public class Wizard extends Character {
private int fireballs;
public Wizard()
{
super();
fireballs = 0;
count++;
}
public void setFireballs(int fireballs)
{
this.fireballs = fireballs;
}
public int getFireballs(){
return fireballs;
}
#Override
public String getDisplayText()
{
return super.toString() +
"Fireballs: " + fireballs + "\n";
}
#Override
public void setArrows(int i) {
}
}
Elf class
public class Elf extends Character {
private int arrows;
public Elf()
{
super();
arrows = 0;
count++;
}
public void setArrows(int arrows)
{
this.arrows = arrows;
}
public int getArrows(){
return arrows;
}
#Override
public String getDisplayText()
{
return super.toString() +
"Arrows: " + arrows + "\n";
}
#Override
public void setFireballs(int i){
}
}
abstract class Character
public abstract class Character {
private String name;
private int gold;
private int health;
public static int count = 0;
public Character() {
name = "";
gold = 0;
health = 0;
}
public Character(String name, int gold, int health) {
this.name = name;
this.gold = gold;
this.health = health;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setGold(int gold) {
this.gold = gold;
}
public void setHealth(int health) {
this.health = health;
}
public int getHealth() {
return health;
}
#Override
public String toString() {
return "Name: " + name + "\n" + "Gold: " + gold + "\n" + "Health: "
+ health + "\n";
}
public static int getCount() {
return count;
}
public abstract String getDisplayText();
public abstract void setFireballs(int i);
public abstract void setArrows(int i);
}
hope this helps...

input from Keyboard into Array list of persons

I want to accept input from user to populate an Array list of Person. for some reason. I can't get it to work. I can add an item into the list I have created below are my code for reference. in the SimplepersonDatabase class in switch function case 2:, I want to accept an an input of names, date of birth from the user and the program should automatically assign the position number starting from
e.g
001. Damien Kluk September, 12.09.1975
002. James Hunt January , 12.09.2000
I should be able to also delete a person and sort the list of Persons. here are what I have implemented so far.
public class Person { //Person.java
public String fn;
public String ln;
public Date dob;
public int id;
public Person() {
}
public Person(String fn, String ln, Date dob, int id) {
this.fn = fn;
this.ln = ln;
this.dob = dob;
this.id = id;
}
}
class List {//List.java
int MAX_LIST = 20;
Person[] persons;
int count;
public List() {
persons = new Person[MAX_LIST];
count=0;
}
public int numberOfPersons() {
return count;
}
public void add(Person person) {
checkUniqueId(person);
if (count >= persons.length) {
// Enlarge array
persons = Arrays.copyOf(persons, persons.length + 100);
}
persons[count] = person;
++count;
}
private void checkUniqueId(Person person) {
for (int i = 0; i < count; ++i) {
if (persons[i].id == person.id) {
throw new IllegalArgumentException("Already a person with id "
+ person.id);
}
}
}
public void remove(int personId) {
for (int i = 0; i < count; ++i) {
if (persons[i].id == personId) {
--count;
persons[i] = persons[count];
persons[count] = null;
return;
}
}
throw new IllegalArgumentException("No person known with id "
+ personId);
}
}
public class SimplePersonDataBase { //SimplePersonDataBase.java
private static List list;
private static int nextPersonId;
public static void main(String[] args) {
go();
}
public static void go() {
List link = new List();
TextIO.put("Welcome to the SimplePersonDatabase.\n");
TextIO.putln();
int option;
do{
TextIO.put("available options:\n1) list\n2) add\n3) remove\n4) sort\n5) find\n6) settings\n0) quit\nyour choice:");
option = TextIO.getInt();
switch(option){
case 1:
PersonFunctions.display();
break;
case 2: // Should accept inputs from a user and update the Persons database
TextIO.put("Firstname:");
String fn = TextIO.getlnWord();
TextIO.put("Lastname:");
String ln = TextIO.getlnWord();
Date date = DateFunctions.scanDate();
int pos = link.count;
Person item = new Person(fn,ln,date,pos);
add(item);
break;
case 3:
break;
case 4:
TextIO.putln("sort by:\n1) Firstname\n2) Birth\nall other values: lastname");
switch(TextIO.getInt()){
case 1:
break;
case 2:
break;
default :
break;
}
break;
case 5:
break;
case 6:
break;
case 0:
TextIO.put("Thank you for using the SimplePersonDatabase.");
break;
case 99:
break;
default :
TextIO.put("illegal option.");
break;
}
}while(option !=0);
}
public static boolean add(Person personadd) {
personadd.id = nextPersonId;
++nextPersonId;
list.add(personadd);
return true;
}
}
Your list is working well (I tried + or -)
import java.util.Arrays;
import java.util.Date;
class Person { // Person.java
public String fn;
public String ln;
public Date dob;
public int id;
public Person() {
}
public Person(String fn, String ln, Date dob, int id) {
this.fn = fn;
this.ln = ln;
this.dob = dob;
this.id = id;
}
}
the list
public class MyList {
int MAX_LIST = 20;
Person[] persons;
int count;
public MyList() {
persons = new Person[MAX_LIST];
count = 0;
}
public int numberOfPersons() {
return count;
}
public void add(Person person) {
checkUniqueId(person);
if (count >= persons.length) {
// Enlarge array
System.out.println("enlarging");
persons = Arrays.copyOf(persons, persons.length + 100);
}
persons[count] = person;
++count;
}
private void checkUniqueId(Person person) {
for (int i = 0; i < count; ++i) {
if (persons[i].id == person.id) {
throw new IllegalArgumentException("Already a person with id "
+ person.id);
}
}
}
public void remove(int personId) {
for (int i = 0; i < count; ++i) {
if (persons[i].id == personId) {
--count;
persons[i] = persons[count];
persons[count] = null;
return;
}
}
throw new IllegalArgumentException("No person known with id "
+ personId);
}
public Person get(int i) {
return persons[i];
}
public static void main(String[] args) {
MyList list = new MyList();
for (int i=0; i<1000; i++) {
list.add(new Person("fn"+i,"sn"+i,new Date(),i));
System.out.println(list.get(i) + " " + list.count);
}
}
}
the problem is in the "go" function, why you are using link and then you add in list?

Categories