How to use more than one class?
Hey, I just recently finished my CS class and so I started going out and trying to learn a few various things that the course didn't touch on at all. The one huge problem that I'm having is that, in the course, it never showed how to use more than one .class file; because of this any lengthy program that I write is one huge .java file with up to a dozen different classes all mashed together.
I've attempted to look up how to work with multiple class files but I haven't found any decent links or instructions. Basically, I want to be able to have each class in it's own file which can be accessed by any other class in any other file that I write. If anyone has a minute or two to direct me to a tutorial or even explain it, that would be a huge help!
Re: How to use more than one class?
Quote:
have each class in it's own file
Cut the code for each class (class statement through its ending }) out of the huge file and paste it into a file with the filename the same as the class name.
You'll also have to copy some of the import statements for the packages the code uses.
Re: How to use more than one class?
Quote:
Originally Posted by
Norm
Cut the code for each class (class statement through its ending }) out of the huge file and paste it into a file with the filename the same as the class name.
You'll also have to copy some of the import statements for the packages the code uses.
O.o It's actually just as simple as that? Thanks a ton!
--- Update ---
I just ran into three, to me, strange errors after trying to write a simple program to test out using two separate class files; I think that the errors are caused because I'm incorrectly using the two files or something but I'm not sure. Then again, I could have just messed up typing...
Here is a picture of the errors: http://i.imgur.com/uGe1DJw.png
Core.java
Code :
class Core
{
public static void main(String args[])
{
String namePlayer = "playerTest", nameEnemy = "enemyTest";
int playerAttackMin = 5, playerAttackMax = 10, playerHealth = 100;
int enemyAttackMin = 3, enemyAttackMax = 7, enemyHealth = 100;
Battle battle = new Battle(namePlayer, nameEnemy, playerAttackMin, playerAttackMax, playerHealth, enemyAttackMin, enemyAttackMax, enemyHealth);
System.out.println(battle.calculateLostHealth);
}
}
Battle.java
Code :
import java.util.Random;
class Battle
{
private int playerAttackMin = 0, playerAttackMax = 0, playerHealth = 0;
private int enemyAttackMin = 0, enemyAttackMax = 0, enemyHealth = 0;
private int turn = 0, damage = 0, tempInt = 0;
private String namePlayer = "null", nameEnemy = "null";
public Battle(String namePlayer, String nameEnemy, int playerAttackMin, int playerAttackMax, int playerHealth, int enemyAttackMin, int enemyattackMax, int enemyHealth)
{
this.playerAttackMin = playerAttackMin;
this.playerAttackMax = playerAttackMax;
this.playerHealth = playerHealth;
this.namePlayer = namePlayer;
this.enemyAttackMin = enemyAttackMin;
this.enemyAttackMax = enemyAttackMax;
this.enemyHealth = enemyHealth;
this.nameEnemy = nameEnemy;
}
public int calculateLostHealth() //Calculates damage and then takes it off of the current player/enemys health and returns the health.
{
Random diceRoller = new Random();
if(turn == 0)
{
tempInt = (playerAttackMax - playerAttackMin)
damage = diceRoller.nextInt(tempInt) + 1; //Generates a random number between 0 and tempInt - 1, then adds 1 to make up for the -1.
damage = damage + playerAttackMin;
playerHealth = playerHealth - damage;
if(playerHealth < 0)
playerHealth = 0;
return playerHealth;
}
else if(turn == 1)
{
tempInt = (enemyAttackMax - enemyAttackMin)
damage = diceRoller.nextInt(tempInt) + 1; //Generates a random number between 0 and tempInt - 1, then adds 1 to make up for the -1.
damage = damage + enemyAttackMin;
enemyHealth = enemyHealth - damage;
if(enemyHealth < 0)
enemyHealth = 0;
return enemyHealth;
}
}
//Get/Set methods
public void setTurn(int x)
{
turn = x;
}
public void setPlayerHealth(int x)
{
playerHealth = x;
}
public void setEnemyHealth(int x)
{
enemyHealth = x;
}
public void setPlayerAttackMin(int x)
{
playerAttackMin = x;
}
public void setPlayerAttackMax(int x)
{
playerAttackMax = x;
}
public void setEnemyAttackMin(int x)
{
enemyAttackMin = x;
}
public void setEnemyAttackMax(int x)
{
enemyAttackMax = x;
}
}
These errors seem strange to me as the error on line 10 of Core.java should work, I've used statements exactly like it many times before. As for the errors on lines 29 & 40 of Battle.java, I can see no reason why it would "require" a class instead of a value.
Re: How to use more than one class?
Please copy the full text of the error messages and paste it here. Images can be hard to read and can't be copied and pasted in comments.
Re: How to use more than one class?
Code :
C:\Users\NAME>cd C:\Users\NAME\Java Projects\Text Adventure
C:\Users\NAME\Java Projects\Text Adventure>javac *.java
Battle.java:29: error: unexpected type
tempInt = (playerAttackMax - playerAttackMin)
^
required: class
found: value
Battle.java:40: error: unexpected type
tempInt = (enemyAttackMax - enemyAttackMin)
^
required: class
found: value
Core.java:10: error: cannot find symbol
System.out.println(battle.calculateLostHealth);
^
symbol: variable calculateLostHealth
location: variable battle of type Battle
3 errors
That is the full error message, I copied it straight from the cmd. The error messages make sense but what they're saying doesn't to me if that makes any sense...
Re: How to use more than one class?
Quote:
cannot find symbol
symbol: variable calculateLostHealth
Where is the variable calculateLostHealth defined in the Battle class. The compiler can not find it.
Check the syntax of the statements in the other error messages. The compiler is confused.
Re: How to use more than one class?
I've managed to fix the errors, it looks like I forgot two semicolons and a pair of (). Thanks again!