Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to use more than one class?

  1. #1
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default 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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to use more than one class?

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    tyeeeee1 (January 25th, 2013)

  4. #3
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: How to use more than one class?

    Quote Originally Posted by Norm View Post
    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
    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
    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.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: How to use more than one class?

    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...

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to use more than one class?

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    tyeeeee1 (January 26th, 2013)

  9. #7
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default 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!

Similar Threads

  1. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 02:30 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  4. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  5. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM