My GUI is not recognizing my instance method. How can I fix this?












-2














1) I can not call my instance method in the GUI



2) I can not implement a specific feature.



I have two basic problems. First of all, I need my GUI to recognize my instance method (namely, when I call my isWin method in my GUI, the method is not recognized, "symbol can not be found").



My other problem is in the execution of a specific feature. If my fighter is a WaterFighter then he should be able to beat a FireFighter up to three levels higher. so a level 8 waterfighter should beat a level 10 firefighter. I have no clue how to implement this feature.



I tried to put it in the isWin method that whatever opposing character is passed in should drop three levels if the waterfighter calls the method and that if the fighter who calls isWin is FireFighter then the fighter who called it should drop 3 level to give the opposition an advantage. But this clearly does not work because what if I get a firefighter verses another fireFighter? Then one of the fighters has an unfair advantage.



//my interface



   package OOPFight;

import java.util.ArrayList;

public interface Character {

public boolean isWin(Character c);

public String getName();

public int getLevel();

public String toString();

}


//my superclass fighter



package OOPFight;

import java.util.ArrayList;

/**
*
* @author Owner
*/
public class fighter implements Character {



public String name;
public int level;
public String type;

public fighter(String n, int l, String t) {

name = n;
level = l;
type = t;

}

public boolean isWin(Character c) {

if (level > c.getLevel()) {

return true;
} else if (c.getLevel() > level) {

return false;
} else {

int random = (int) (Math.random() * 2 + 1);

if (random == 1) {
return true;
} else {

return false;
}
}

}//end iswin

public String toString() {

String desc;
desc = name + ":" + level + ":" + type;
return desc;

}//end toString()

public String getType() {

return type;

}//end toString()

public String getName() {
return name;
}

public int getLevel() {
return level;
}

}


//my subclass WaterFighter



import java.util.ArrayList;


public class WaterFighter extends fighter{


public String name;
public int level;
public String type;




public WaterFighter(String n, int l, String t) {
super(n, l, t);
}

public boolean isWin(Character c){


int level2 = c.getLevel() - 3;

int level1 = level;

if (level1 > level2){

return true;

} else if (level2 > level1){

return false;
} else {

int random = (int) (Math.random() * 2 + 1);

if (random == 1) {
return true;
} else {

return false;
}//end if

}//end if

}//end isWin

}//end class


//my subclass FireFighter



package OOPFight;

import java.util.ArrayList;


public class FireFighter extends fighter{


public String name;
public int level;
public String type;




public FireFighter(String n, int l, String t) {
super(n, l, t);
}

public boolean isWin(Character c){



int level2 = c.getLevel();

int level1 = level - 3;

if (level1 > level2){

return true;

} else if (level2 > level1){

return false;
} else {

int random = (int) (Math.random() * 2 + 1);

if (random == 1) {
return true;
} else {

return false;
}//end if

}//end if

}//end isWin

}//end class


//my Gui



package OOPFight;

import java.util.ArrayList;

/**
*
* @author Owner
*/
public class fight extends javax.swing.JFrame {

public static ArrayList allFighters = new ArrayList();


/**
* Creates new form fight
*/
public fight() {
initComponents();


ArrayList allFighterTypes = new ArrayList();

allFighterTypes.add("Water Fighter");
allFighterTypes.add("Fire Fighter");

for (int i = 0; i < 2; i++){

typesComboBox.addItem((String) allFighterTypes.get(i));

}

}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
nameField = new javax.swing.JTextField();
createFighter = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
typesComboBox = new javax.swing.JComboBox<>();
jLabel5 = new javax.swing.JLabel();
levelField = new javax.swing.JTextField();
jPanel2 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
firstCombo = new javax.swing.JComboBox<>();
jLabel6 = new javax.swing.JLabel();
secondCombo = new javax.swing.JComboBox<>();
jButton2 = new javax.swing.JButton();
display = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(255, 255, 0));

jLabel1.setFont(new java.awt.Font("Gadugi", 0, 24)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Fighter Regristration");

jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel3.setText("Name:");

createFighter.setText("Create Fighter!");
createFighter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createFighterActionPerformed(evt);
}
});

jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel4.setText("Type:");

jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel5.setText("Level:");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nameField)
.addComponent(typesComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(levelField))))
.addComponent(createFighter))
.addContainerGap(33, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(47, 47, 47)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(typesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(levelField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(57, 57, 57)
.addComponent(createFighter)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

jPanel2.setBackground(new java.awt.Color(0, 255, 255));

jLabel2.setFont(new java.awt.Font("Gadugi", 0, 24)); // NOI18N
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Fighting Arena");

jLabel6.setText("VS.");

jButton2.setText("Fight!");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

display.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
display.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(170, 170, 170))
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(display, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(firstCombo, 0, 184, Short.MAX_VALUE)
.addGap(33, 33, 33)
.addComponent(jLabel6)
.addGap(18, 18, 18)
.addComponent(secondCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(116, 116, 116)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(firstCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6)
.addComponent(secondCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(108, 108, 108)
.addComponent(jButton2)
.addGap(67, 67, 67)
.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(113, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);

pack();
}// </editor-fold>

private void createFighterActionPerformed(java.awt.event.ActionEvent evt) {

String n = nameField.getText();
String leveler = levelField.getText();
int level = Integer.parseInt(leveler);
int typer = typesComboBox.getSelectedIndex();
String type = null;

if (typer == 1){

type = "WaterFighter";

} else if (typer == 2){
type = "FireFighter";

}


allFighters.add( new fighter(n, level, type));

firstCombo.addItem(allFighters.toString());
secondCombo.addItem(allFighters.toString());


}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

int monster1 = firstCombo.getSelectedIndex();
int monster2 = secondCombo.getSelectedIndex();

boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));



}

/**
* @param args the command line arguments
*/
public static void main(String args) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new fight().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton createFighter;
private javax.swing.JLabel display;
private javax.swing.JComboBox<String> firstCombo;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField levelField;
private javax.swing.JTextField nameField;
private javax.swing.JComboBox<String> secondCombo;
private javax.swing.JComboBox<String> typesComboBox;
// End of variables declaration



}


//end code



I expect that I can properly call my isWin method in my GUI class and that I can properly implement the feature where there is a differene in ability where a WaterFighter can beat a FireFighter that is 3 levels higher. So a level 8 waterfighter can beat a level 10 firefighter.










share|improve this question




















  • 2




    Welcome to Stack Overflow! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? and What topics can I ask about here?. From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." please add the complete error message.
    – Timothy Truckle
    Dec 27 '18 at 22:04








  • 4




    Please post a Minimal, Complete, and Verifiable example.
    – OldProgrammer
    Dec 27 '18 at 22:04










  • Please send a error message. Did you try to cast to Character?
    – user1474111
    Dec 27 '18 at 22:05
















-2














1) I can not call my instance method in the GUI



2) I can not implement a specific feature.



I have two basic problems. First of all, I need my GUI to recognize my instance method (namely, when I call my isWin method in my GUI, the method is not recognized, "symbol can not be found").



My other problem is in the execution of a specific feature. If my fighter is a WaterFighter then he should be able to beat a FireFighter up to three levels higher. so a level 8 waterfighter should beat a level 10 firefighter. I have no clue how to implement this feature.



I tried to put it in the isWin method that whatever opposing character is passed in should drop three levels if the waterfighter calls the method and that if the fighter who calls isWin is FireFighter then the fighter who called it should drop 3 level to give the opposition an advantage. But this clearly does not work because what if I get a firefighter verses another fireFighter? Then one of the fighters has an unfair advantage.



//my interface



   package OOPFight;

import java.util.ArrayList;

public interface Character {

public boolean isWin(Character c);

public String getName();

public int getLevel();

public String toString();

}


//my superclass fighter



package OOPFight;

import java.util.ArrayList;

/**
*
* @author Owner
*/
public class fighter implements Character {



public String name;
public int level;
public String type;

public fighter(String n, int l, String t) {

name = n;
level = l;
type = t;

}

public boolean isWin(Character c) {

if (level > c.getLevel()) {

return true;
} else if (c.getLevel() > level) {

return false;
} else {

int random = (int) (Math.random() * 2 + 1);

if (random == 1) {
return true;
} else {

return false;
}
}

}//end iswin

public String toString() {

String desc;
desc = name + ":" + level + ":" + type;
return desc;

}//end toString()

public String getType() {

return type;

}//end toString()

public String getName() {
return name;
}

public int getLevel() {
return level;
}

}


//my subclass WaterFighter



import java.util.ArrayList;


public class WaterFighter extends fighter{


public String name;
public int level;
public String type;




public WaterFighter(String n, int l, String t) {
super(n, l, t);
}

public boolean isWin(Character c){


int level2 = c.getLevel() - 3;

int level1 = level;

if (level1 > level2){

return true;

} else if (level2 > level1){

return false;
} else {

int random = (int) (Math.random() * 2 + 1);

if (random == 1) {
return true;
} else {

return false;
}//end if

}//end if

}//end isWin

}//end class


//my subclass FireFighter



package OOPFight;

import java.util.ArrayList;


public class FireFighter extends fighter{


public String name;
public int level;
public String type;




public FireFighter(String n, int l, String t) {
super(n, l, t);
}

public boolean isWin(Character c){



int level2 = c.getLevel();

int level1 = level - 3;

if (level1 > level2){

return true;

} else if (level2 > level1){

return false;
} else {

int random = (int) (Math.random() * 2 + 1);

if (random == 1) {
return true;
} else {

return false;
}//end if

}//end if

}//end isWin

}//end class


//my Gui



package OOPFight;

import java.util.ArrayList;

/**
*
* @author Owner
*/
public class fight extends javax.swing.JFrame {

public static ArrayList allFighters = new ArrayList();


/**
* Creates new form fight
*/
public fight() {
initComponents();


ArrayList allFighterTypes = new ArrayList();

allFighterTypes.add("Water Fighter");
allFighterTypes.add("Fire Fighter");

for (int i = 0; i < 2; i++){

typesComboBox.addItem((String) allFighterTypes.get(i));

}

}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
nameField = new javax.swing.JTextField();
createFighter = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
typesComboBox = new javax.swing.JComboBox<>();
jLabel5 = new javax.swing.JLabel();
levelField = new javax.swing.JTextField();
jPanel2 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
firstCombo = new javax.swing.JComboBox<>();
jLabel6 = new javax.swing.JLabel();
secondCombo = new javax.swing.JComboBox<>();
jButton2 = new javax.swing.JButton();
display = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(255, 255, 0));

jLabel1.setFont(new java.awt.Font("Gadugi", 0, 24)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Fighter Regristration");

jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel3.setText("Name:");

createFighter.setText("Create Fighter!");
createFighter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createFighterActionPerformed(evt);
}
});

jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel4.setText("Type:");

jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel5.setText("Level:");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nameField)
.addComponent(typesComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(levelField))))
.addComponent(createFighter))
.addContainerGap(33, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(47, 47, 47)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(typesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(levelField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(57, 57, 57)
.addComponent(createFighter)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

jPanel2.setBackground(new java.awt.Color(0, 255, 255));

jLabel2.setFont(new java.awt.Font("Gadugi", 0, 24)); // NOI18N
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Fighting Arena");

jLabel6.setText("VS.");

jButton2.setText("Fight!");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

display.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
display.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(170, 170, 170))
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(display, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(firstCombo, 0, 184, Short.MAX_VALUE)
.addGap(33, 33, 33)
.addComponent(jLabel6)
.addGap(18, 18, 18)
.addComponent(secondCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(116, 116, 116)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(firstCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6)
.addComponent(secondCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(108, 108, 108)
.addComponent(jButton2)
.addGap(67, 67, 67)
.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(113, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);

pack();
}// </editor-fold>

private void createFighterActionPerformed(java.awt.event.ActionEvent evt) {

String n = nameField.getText();
String leveler = levelField.getText();
int level = Integer.parseInt(leveler);
int typer = typesComboBox.getSelectedIndex();
String type = null;

if (typer == 1){

type = "WaterFighter";

} else if (typer == 2){
type = "FireFighter";

}


allFighters.add( new fighter(n, level, type));

firstCombo.addItem(allFighters.toString());
secondCombo.addItem(allFighters.toString());


}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

int monster1 = firstCombo.getSelectedIndex();
int monster2 = secondCombo.getSelectedIndex();

boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));



}

/**
* @param args the command line arguments
*/
public static void main(String args) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new fight().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton createFighter;
private javax.swing.JLabel display;
private javax.swing.JComboBox<String> firstCombo;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField levelField;
private javax.swing.JTextField nameField;
private javax.swing.JComboBox<String> secondCombo;
private javax.swing.JComboBox<String> typesComboBox;
// End of variables declaration



}


//end code



I expect that I can properly call my isWin method in my GUI class and that I can properly implement the feature where there is a differene in ability where a WaterFighter can beat a FireFighter that is 3 levels higher. So a level 8 waterfighter can beat a level 10 firefighter.










share|improve this question




















  • 2




    Welcome to Stack Overflow! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? and What topics can I ask about here?. From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." please add the complete error message.
    – Timothy Truckle
    Dec 27 '18 at 22:04








  • 4




    Please post a Minimal, Complete, and Verifiable example.
    – OldProgrammer
    Dec 27 '18 at 22:04










  • Please send a error message. Did you try to cast to Character?
    – user1474111
    Dec 27 '18 at 22:05














-2












-2








-2







1) I can not call my instance method in the GUI



2) I can not implement a specific feature.



I have two basic problems. First of all, I need my GUI to recognize my instance method (namely, when I call my isWin method in my GUI, the method is not recognized, "symbol can not be found").



My other problem is in the execution of a specific feature. If my fighter is a WaterFighter then he should be able to beat a FireFighter up to three levels higher. so a level 8 waterfighter should beat a level 10 firefighter. I have no clue how to implement this feature.



I tried to put it in the isWin method that whatever opposing character is passed in should drop three levels if the waterfighter calls the method and that if the fighter who calls isWin is FireFighter then the fighter who called it should drop 3 level to give the opposition an advantage. But this clearly does not work because what if I get a firefighter verses another fireFighter? Then one of the fighters has an unfair advantage.



//my interface



   package OOPFight;

import java.util.ArrayList;

public interface Character {

public boolean isWin(Character c);

public String getName();

public int getLevel();

public String toString();

}


//my superclass fighter



package OOPFight;

import java.util.ArrayList;

/**
*
* @author Owner
*/
public class fighter implements Character {



public String name;
public int level;
public String type;

public fighter(String n, int l, String t) {

name = n;
level = l;
type = t;

}

public boolean isWin(Character c) {

if (level > c.getLevel()) {

return true;
} else if (c.getLevel() > level) {

return false;
} else {

int random = (int) (Math.random() * 2 + 1);

if (random == 1) {
return true;
} else {

return false;
}
}

}//end iswin

public String toString() {

String desc;
desc = name + ":" + level + ":" + type;
return desc;

}//end toString()

public String getType() {

return type;

}//end toString()

public String getName() {
return name;
}

public int getLevel() {
return level;
}

}


//my subclass WaterFighter



import java.util.ArrayList;


public class WaterFighter extends fighter{


public String name;
public int level;
public String type;




public WaterFighter(String n, int l, String t) {
super(n, l, t);
}

public boolean isWin(Character c){


int level2 = c.getLevel() - 3;

int level1 = level;

if (level1 > level2){

return true;

} else if (level2 > level1){

return false;
} else {

int random = (int) (Math.random() * 2 + 1);

if (random == 1) {
return true;
} else {

return false;
}//end if

}//end if

}//end isWin

}//end class


//my subclass FireFighter



package OOPFight;

import java.util.ArrayList;


public class FireFighter extends fighter{


public String name;
public int level;
public String type;




public FireFighter(String n, int l, String t) {
super(n, l, t);
}

public boolean isWin(Character c){



int level2 = c.getLevel();

int level1 = level - 3;

if (level1 > level2){

return true;

} else if (level2 > level1){

return false;
} else {

int random = (int) (Math.random() * 2 + 1);

if (random == 1) {
return true;
} else {

return false;
}//end if

}//end if

}//end isWin

}//end class


//my Gui



package OOPFight;

import java.util.ArrayList;

/**
*
* @author Owner
*/
public class fight extends javax.swing.JFrame {

public static ArrayList allFighters = new ArrayList();


/**
* Creates new form fight
*/
public fight() {
initComponents();


ArrayList allFighterTypes = new ArrayList();

allFighterTypes.add("Water Fighter");
allFighterTypes.add("Fire Fighter");

for (int i = 0; i < 2; i++){

typesComboBox.addItem((String) allFighterTypes.get(i));

}

}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
nameField = new javax.swing.JTextField();
createFighter = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
typesComboBox = new javax.swing.JComboBox<>();
jLabel5 = new javax.swing.JLabel();
levelField = new javax.swing.JTextField();
jPanel2 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
firstCombo = new javax.swing.JComboBox<>();
jLabel6 = new javax.swing.JLabel();
secondCombo = new javax.swing.JComboBox<>();
jButton2 = new javax.swing.JButton();
display = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(255, 255, 0));

jLabel1.setFont(new java.awt.Font("Gadugi", 0, 24)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Fighter Regristration");

jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel3.setText("Name:");

createFighter.setText("Create Fighter!");
createFighter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createFighterActionPerformed(evt);
}
});

jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel4.setText("Type:");

jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel5.setText("Level:");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nameField)
.addComponent(typesComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(levelField))))
.addComponent(createFighter))
.addContainerGap(33, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(47, 47, 47)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(typesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(levelField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(57, 57, 57)
.addComponent(createFighter)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

jPanel2.setBackground(new java.awt.Color(0, 255, 255));

jLabel2.setFont(new java.awt.Font("Gadugi", 0, 24)); // NOI18N
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Fighting Arena");

jLabel6.setText("VS.");

jButton2.setText("Fight!");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

display.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
display.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(170, 170, 170))
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(display, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(firstCombo, 0, 184, Short.MAX_VALUE)
.addGap(33, 33, 33)
.addComponent(jLabel6)
.addGap(18, 18, 18)
.addComponent(secondCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(116, 116, 116)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(firstCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6)
.addComponent(secondCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(108, 108, 108)
.addComponent(jButton2)
.addGap(67, 67, 67)
.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(113, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);

pack();
}// </editor-fold>

private void createFighterActionPerformed(java.awt.event.ActionEvent evt) {

String n = nameField.getText();
String leveler = levelField.getText();
int level = Integer.parseInt(leveler);
int typer = typesComboBox.getSelectedIndex();
String type = null;

if (typer == 1){

type = "WaterFighter";

} else if (typer == 2){
type = "FireFighter";

}


allFighters.add( new fighter(n, level, type));

firstCombo.addItem(allFighters.toString());
secondCombo.addItem(allFighters.toString());


}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

int monster1 = firstCombo.getSelectedIndex();
int monster2 = secondCombo.getSelectedIndex();

boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));



}

/**
* @param args the command line arguments
*/
public static void main(String args) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new fight().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton createFighter;
private javax.swing.JLabel display;
private javax.swing.JComboBox<String> firstCombo;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField levelField;
private javax.swing.JTextField nameField;
private javax.swing.JComboBox<String> secondCombo;
private javax.swing.JComboBox<String> typesComboBox;
// End of variables declaration



}


//end code



I expect that I can properly call my isWin method in my GUI class and that I can properly implement the feature where there is a differene in ability where a WaterFighter can beat a FireFighter that is 3 levels higher. So a level 8 waterfighter can beat a level 10 firefighter.










share|improve this question















1) I can not call my instance method in the GUI



2) I can not implement a specific feature.



I have two basic problems. First of all, I need my GUI to recognize my instance method (namely, when I call my isWin method in my GUI, the method is not recognized, "symbol can not be found").



My other problem is in the execution of a specific feature. If my fighter is a WaterFighter then he should be able to beat a FireFighter up to three levels higher. so a level 8 waterfighter should beat a level 10 firefighter. I have no clue how to implement this feature.



I tried to put it in the isWin method that whatever opposing character is passed in should drop three levels if the waterfighter calls the method and that if the fighter who calls isWin is FireFighter then the fighter who called it should drop 3 level to give the opposition an advantage. But this clearly does not work because what if I get a firefighter verses another fireFighter? Then one of the fighters has an unfair advantage.



//my interface



   package OOPFight;

import java.util.ArrayList;

public interface Character {

public boolean isWin(Character c);

public String getName();

public int getLevel();

public String toString();

}


//my superclass fighter



package OOPFight;

import java.util.ArrayList;

/**
*
* @author Owner
*/
public class fighter implements Character {



public String name;
public int level;
public String type;

public fighter(String n, int l, String t) {

name = n;
level = l;
type = t;

}

public boolean isWin(Character c) {

if (level > c.getLevel()) {

return true;
} else if (c.getLevel() > level) {

return false;
} else {

int random = (int) (Math.random() * 2 + 1);

if (random == 1) {
return true;
} else {

return false;
}
}

}//end iswin

public String toString() {

String desc;
desc = name + ":" + level + ":" + type;
return desc;

}//end toString()

public String getType() {

return type;

}//end toString()

public String getName() {
return name;
}

public int getLevel() {
return level;
}

}


//my subclass WaterFighter



import java.util.ArrayList;


public class WaterFighter extends fighter{


public String name;
public int level;
public String type;




public WaterFighter(String n, int l, String t) {
super(n, l, t);
}

public boolean isWin(Character c){


int level2 = c.getLevel() - 3;

int level1 = level;

if (level1 > level2){

return true;

} else if (level2 > level1){

return false;
} else {

int random = (int) (Math.random() * 2 + 1);

if (random == 1) {
return true;
} else {

return false;
}//end if

}//end if

}//end isWin

}//end class


//my subclass FireFighter



package OOPFight;

import java.util.ArrayList;


public class FireFighter extends fighter{


public String name;
public int level;
public String type;




public FireFighter(String n, int l, String t) {
super(n, l, t);
}

public boolean isWin(Character c){



int level2 = c.getLevel();

int level1 = level - 3;

if (level1 > level2){

return true;

} else if (level2 > level1){

return false;
} else {

int random = (int) (Math.random() * 2 + 1);

if (random == 1) {
return true;
} else {

return false;
}//end if

}//end if

}//end isWin

}//end class


//my Gui



package OOPFight;

import java.util.ArrayList;

/**
*
* @author Owner
*/
public class fight extends javax.swing.JFrame {

public static ArrayList allFighters = new ArrayList();


/**
* Creates new form fight
*/
public fight() {
initComponents();


ArrayList allFighterTypes = new ArrayList();

allFighterTypes.add("Water Fighter");
allFighterTypes.add("Fire Fighter");

for (int i = 0; i < 2; i++){

typesComboBox.addItem((String) allFighterTypes.get(i));

}

}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
nameField = new javax.swing.JTextField();
createFighter = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
typesComboBox = new javax.swing.JComboBox<>();
jLabel5 = new javax.swing.JLabel();
levelField = new javax.swing.JTextField();
jPanel2 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
firstCombo = new javax.swing.JComboBox<>();
jLabel6 = new javax.swing.JLabel();
secondCombo = new javax.swing.JComboBox<>();
jButton2 = new javax.swing.JButton();
display = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(255, 255, 0));

jLabel1.setFont(new java.awt.Font("Gadugi", 0, 24)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Fighter Regristration");

jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel3.setText("Name:");

createFighter.setText("Create Fighter!");
createFighter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createFighterActionPerformed(evt);
}
});

jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel4.setText("Type:");

jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel5.setText("Level:");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nameField)
.addComponent(typesComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(levelField))))
.addComponent(createFighter))
.addContainerGap(33, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(47, 47, 47)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(typesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(levelField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(57, 57, 57)
.addComponent(createFighter)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

jPanel2.setBackground(new java.awt.Color(0, 255, 255));

jLabel2.setFont(new java.awt.Font("Gadugi", 0, 24)); // NOI18N
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Fighting Arena");

jLabel6.setText("VS.");

jButton2.setText("Fight!");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

display.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
display.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(170, 170, 170))
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(display, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(firstCombo, 0, 184, Short.MAX_VALUE)
.addGap(33, 33, 33)
.addComponent(jLabel6)
.addGap(18, 18, 18)
.addComponent(secondCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(116, 116, 116)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(firstCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6)
.addComponent(secondCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(108, 108, 108)
.addComponent(jButton2)
.addGap(67, 67, 67)
.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(113, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);

pack();
}// </editor-fold>

private void createFighterActionPerformed(java.awt.event.ActionEvent evt) {

String n = nameField.getText();
String leveler = levelField.getText();
int level = Integer.parseInt(leveler);
int typer = typesComboBox.getSelectedIndex();
String type = null;

if (typer == 1){

type = "WaterFighter";

} else if (typer == 2){
type = "FireFighter";

}


allFighters.add( new fighter(n, level, type));

firstCombo.addItem(allFighters.toString());
secondCombo.addItem(allFighters.toString());


}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

int monster1 = firstCombo.getSelectedIndex();
int monster2 = secondCombo.getSelectedIndex();

boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));



}

/**
* @param args the command line arguments
*/
public static void main(String args) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(fight.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new fight().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton createFighter;
private javax.swing.JLabel display;
private javax.swing.JComboBox<String> firstCombo;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField levelField;
private javax.swing.JTextField nameField;
private javax.swing.JComboBox<String> secondCombo;
private javax.swing.JComboBox<String> typesComboBox;
// End of variables declaration



}


//end code



I expect that I can properly call my isWin method in my GUI class and that I can properly implement the feature where there is a differene in ability where a WaterFighter can beat a FireFighter that is 3 levels higher. So a level 8 waterfighter can beat a level 10 firefighter.







java instance-variables instanceof






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 27 '18 at 23:29









pnkkr

7711




7711










asked Dec 27 '18 at 21:59









Mohammad Abu Steit

1




1








  • 2




    Welcome to Stack Overflow! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? and What topics can I ask about here?. From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." please add the complete error message.
    – Timothy Truckle
    Dec 27 '18 at 22:04








  • 4




    Please post a Minimal, Complete, and Verifiable example.
    – OldProgrammer
    Dec 27 '18 at 22:04










  • Please send a error message. Did you try to cast to Character?
    – user1474111
    Dec 27 '18 at 22:05














  • 2




    Welcome to Stack Overflow! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? and What topics can I ask about here?. From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." please add the complete error message.
    – Timothy Truckle
    Dec 27 '18 at 22:04








  • 4




    Please post a Minimal, Complete, and Verifiable example.
    – OldProgrammer
    Dec 27 '18 at 22:04










  • Please send a error message. Did you try to cast to Character?
    – user1474111
    Dec 27 '18 at 22:05








2




2




Welcome to Stack Overflow! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? and What topics can I ask about here?. From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." please add the complete error message.
– Timothy Truckle
Dec 27 '18 at 22:04






Welcome to Stack Overflow! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? and What topics can I ask about here?. From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." please add the complete error message.
– Timothy Truckle
Dec 27 '18 at 22:04






4




4




Please post a Minimal, Complete, and Verifiable example.
– OldProgrammer
Dec 27 '18 at 22:04




Please post a Minimal, Complete, and Verifiable example.
– OldProgrammer
Dec 27 '18 at 22:04












Please send a error message. Did you try to cast to Character?
– user1474111
Dec 27 '18 at 22:05




Please send a error message. Did you try to cast to Character?
– user1474111
Dec 27 '18 at 22:05












2 Answers
2






active

oldest

votes


















2














So, after much digging, the problem comes down to this line ...



boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));


This generates the error



fight.java:117: error: cannot find symbol
boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));


The problem with this can be traced back to how you are declaring allFighters...



public static ArrayList allFighters = new ArrayList();


By default, ArrayList can manage any type of Object, so when you call get, it is returning an instance Object, which obviously does not have a isWin method.



You could cast the result of get, but a simpler solution is to take advantage of Java's inbuilt generic support.



Now, because all your fighter types derive from Character, you can simply constraint the allFighters ArrayList to Character, something like...



public static ArrayList<Character> allFighters = new ArrayList<>();


This places a compile time constraint which ensures that allFighters only ever contains instances of Character



Take a look at Generics for more details.



You should also become familiar with the Java Language Coding Conventions. It will make it easier for people to read your code and for you to read other peoples code






share|improve this answer





























    0














    To help with your battle mechanism, first thing you should do is to create correct classes when initiating fighter. Maybe implement something like this in createFighterActionPerformed:



    int typer = typesComboBox.getSelectedIndex();
    fighter f;

    if (typer == 0) {
    f = new fighter(n, level, "Regular fighter");
    } else if (typer == 1) {
    f = new WaterFighter(n, level, "WaterFighter");
    } else if (typer == 2) {
    f = new FireFighter(n, level, "FireFighter");
    }

    allFighters.add(f);


    To make that fighter comparison reality, you could use instance of clause in each fighters isWin class to check for specific opponent. Like:



    if (c instanceof FireFighter) {
    // Do special stuff
    }


    I'd also advice you to reconsider naming an interface as Character, since it is easily messed up with java.lang.Character class.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951283%2fmy-gui-is-not-recognizing-my-instance-method-how-can-i-fix-this%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      So, after much digging, the problem comes down to this line ...



      boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));


      This generates the error



      fight.java:117: error: cannot find symbol
      boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));


      The problem with this can be traced back to how you are declaring allFighters...



      public static ArrayList allFighters = new ArrayList();


      By default, ArrayList can manage any type of Object, so when you call get, it is returning an instance Object, which obviously does not have a isWin method.



      You could cast the result of get, but a simpler solution is to take advantage of Java's inbuilt generic support.



      Now, because all your fighter types derive from Character, you can simply constraint the allFighters ArrayList to Character, something like...



      public static ArrayList<Character> allFighters = new ArrayList<>();


      This places a compile time constraint which ensures that allFighters only ever contains instances of Character



      Take a look at Generics for more details.



      You should also become familiar with the Java Language Coding Conventions. It will make it easier for people to read your code and for you to read other peoples code






      share|improve this answer


























        2














        So, after much digging, the problem comes down to this line ...



        boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));


        This generates the error



        fight.java:117: error: cannot find symbol
        boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));


        The problem with this can be traced back to how you are declaring allFighters...



        public static ArrayList allFighters = new ArrayList();


        By default, ArrayList can manage any type of Object, so when you call get, it is returning an instance Object, which obviously does not have a isWin method.



        You could cast the result of get, but a simpler solution is to take advantage of Java's inbuilt generic support.



        Now, because all your fighter types derive from Character, you can simply constraint the allFighters ArrayList to Character, something like...



        public static ArrayList<Character> allFighters = new ArrayList<>();


        This places a compile time constraint which ensures that allFighters only ever contains instances of Character



        Take a look at Generics for more details.



        You should also become familiar with the Java Language Coding Conventions. It will make it easier for people to read your code and for you to read other peoples code






        share|improve this answer
























          2












          2








          2






          So, after much digging, the problem comes down to this line ...



          boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));


          This generates the error



          fight.java:117: error: cannot find symbol
          boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));


          The problem with this can be traced back to how you are declaring allFighters...



          public static ArrayList allFighters = new ArrayList();


          By default, ArrayList can manage any type of Object, so when you call get, it is returning an instance Object, which obviously does not have a isWin method.



          You could cast the result of get, but a simpler solution is to take advantage of Java's inbuilt generic support.



          Now, because all your fighter types derive from Character, you can simply constraint the allFighters ArrayList to Character, something like...



          public static ArrayList<Character> allFighters = new ArrayList<>();


          This places a compile time constraint which ensures that allFighters only ever contains instances of Character



          Take a look at Generics for more details.



          You should also become familiar with the Java Language Coding Conventions. It will make it easier for people to read your code and for you to read other peoples code






          share|improve this answer












          So, after much digging, the problem comes down to this line ...



          boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));


          This generates the error



          fight.java:117: error: cannot find symbol
          boolean win = allFighters.get(monster1).isWin(allFighters.get(monster2));


          The problem with this can be traced back to how you are declaring allFighters...



          public static ArrayList allFighters = new ArrayList();


          By default, ArrayList can manage any type of Object, so when you call get, it is returning an instance Object, which obviously does not have a isWin method.



          You could cast the result of get, but a simpler solution is to take advantage of Java's inbuilt generic support.



          Now, because all your fighter types derive from Character, you can simply constraint the allFighters ArrayList to Character, something like...



          public static ArrayList<Character> allFighters = new ArrayList<>();


          This places a compile time constraint which ensures that allFighters only ever contains instances of Character



          Take a look at Generics for more details.



          You should also become familiar with the Java Language Coding Conventions. It will make it easier for people to read your code and for you to read other peoples code







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 27 '18 at 22:21









          MadProgrammer

          299k17153265




          299k17153265

























              0














              To help with your battle mechanism, first thing you should do is to create correct classes when initiating fighter. Maybe implement something like this in createFighterActionPerformed:



              int typer = typesComboBox.getSelectedIndex();
              fighter f;

              if (typer == 0) {
              f = new fighter(n, level, "Regular fighter");
              } else if (typer == 1) {
              f = new WaterFighter(n, level, "WaterFighter");
              } else if (typer == 2) {
              f = new FireFighter(n, level, "FireFighter");
              }

              allFighters.add(f);


              To make that fighter comparison reality, you could use instance of clause in each fighters isWin class to check for specific opponent. Like:



              if (c instanceof FireFighter) {
              // Do special stuff
              }


              I'd also advice you to reconsider naming an interface as Character, since it is easily messed up with java.lang.Character class.






              share|improve this answer


























                0














                To help with your battle mechanism, first thing you should do is to create correct classes when initiating fighter. Maybe implement something like this in createFighterActionPerformed:



                int typer = typesComboBox.getSelectedIndex();
                fighter f;

                if (typer == 0) {
                f = new fighter(n, level, "Regular fighter");
                } else if (typer == 1) {
                f = new WaterFighter(n, level, "WaterFighter");
                } else if (typer == 2) {
                f = new FireFighter(n, level, "FireFighter");
                }

                allFighters.add(f);


                To make that fighter comparison reality, you could use instance of clause in each fighters isWin class to check for specific opponent. Like:



                if (c instanceof FireFighter) {
                // Do special stuff
                }


                I'd also advice you to reconsider naming an interface as Character, since it is easily messed up with java.lang.Character class.






                share|improve this answer
























                  0












                  0








                  0






                  To help with your battle mechanism, first thing you should do is to create correct classes when initiating fighter. Maybe implement something like this in createFighterActionPerformed:



                  int typer = typesComboBox.getSelectedIndex();
                  fighter f;

                  if (typer == 0) {
                  f = new fighter(n, level, "Regular fighter");
                  } else if (typer == 1) {
                  f = new WaterFighter(n, level, "WaterFighter");
                  } else if (typer == 2) {
                  f = new FireFighter(n, level, "FireFighter");
                  }

                  allFighters.add(f);


                  To make that fighter comparison reality, you could use instance of clause in each fighters isWin class to check for specific opponent. Like:



                  if (c instanceof FireFighter) {
                  // Do special stuff
                  }


                  I'd also advice you to reconsider naming an interface as Character, since it is easily messed up with java.lang.Character class.






                  share|improve this answer












                  To help with your battle mechanism, first thing you should do is to create correct classes when initiating fighter. Maybe implement something like this in createFighterActionPerformed:



                  int typer = typesComboBox.getSelectedIndex();
                  fighter f;

                  if (typer == 0) {
                  f = new fighter(n, level, "Regular fighter");
                  } else if (typer == 1) {
                  f = new WaterFighter(n, level, "WaterFighter");
                  } else if (typer == 2) {
                  f = new FireFighter(n, level, "FireFighter");
                  }

                  allFighters.add(f);


                  To make that fighter comparison reality, you could use instance of clause in each fighters isWin class to check for specific opponent. Like:



                  if (c instanceof FireFighter) {
                  // Do special stuff
                  }


                  I'd also advice you to reconsider naming an interface as Character, since it is easily messed up with java.lang.Character class.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 27 '18 at 22:35









                  pnkkr

                  7711




                  7711






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951283%2fmy-gui-is-not-recognizing-my-instance-method-how-can-i-fix-this%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas