Results 1 to 6 of 6
  1. #1
    Kaito is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default Importing a .java

    I'm using Eclipse, I'm new to Java, and Google/Bing/My book didn't help at all.

    I have two .java files:
    BattleField.java and Infantry.java

    Now, I want to have soldier (part of infantry.java) be used in BattleField. So, i tried infantry.Soldier() SoldierA = new infantry.Soldier(), and many combination's of that. It didn't work.

    My file structure is of this (Since I cant post links or pics since i dont have 20 posts >_>): BattleField>src>(default package)>BattleField.java, infantry.java

    Now my question is: How do I use things from infantry.java in BattleField.java?
    NOTE: As it says in the comment block at the top of BattleField.java, this program is to help me learn java. My teacher isn't doing that good of a job, so I figured this would be a good way for me to learn: put everything I know into this program)

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Since you have the source code, you do not import anything. Simply make a new instance of the class you want to use and then call its methods. Posting your code might help too :D

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    What do you mean didn't work? If you got a compilation message then read it and try to understand what it means. If you can't understand it then post the message here.

  4. #4
    Kaito is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default

    haha...forgot about the code tags...
    Java Code:
    //BattleField
    //By: Jake Peshman
    //Purpose: Create a simulated modern day war, and to Learn Java
    //Date Created: 9/21/09
    //Last Modified: 9/22/09
    import java.util.Scanner;
    public class BattleField
    {
    	public void main (String[] args)
    	{
    		//Declaring all the Variables to make them all in one spot
    		Scanner scan = new Scanner(System.in);
    		final String side1, side2;
    		int casRoundEnd, round;
    		int unitsSide1, unitsSide2, money;
    		//Asks for the side names
    		System.out.println("Enter the name for the first team");
    		side1 = scan.nextLine();
    		System.out.println("Enter the name for the second team");
    		side2 = scan.nextLine();
    		round = 0;
    		
    		System.out.println("Enter the amount of Money Each side will get: ");
    		money = scan.nextInt();
    		System.out.println("Enter the number of TOTAL cassualties before the round ends: ");
    		casRoundEnd = scan.nextInt();
    		if (casRoundEnd == 0)
    		{
    			System.out.println("Enter a number above Zero");
    			System.out.println("Enter the number of TOTAL cassualties before the round ends: ");
    			casRoundEnd = scan.nextInt();
    		}
    		System.out.println("SUMMARY: Side Names: " + side1 + ", " + side2 + 
    				". Money available: " + money + ". Cassualties before round ends: " 
    				+ casRoundEnd + ".");
    		System.out.println("Side 1 Name: " + side1 + ". Side 2 Name: " + 
    				side2 + ".");
    		if (side1.equals(("Allies")))
    		{
    			System.out.println("You get uber bonuses");
    		}
    		System.out.println("Round Begin");
    		round++;
    		System.out.println("Round End");
    		System.out.println("Round number: " + round);
    		
    	}				
    }
    That's BattleField.java.
    Java Code:
    //Infantry Class
    //Includes Soldier, AntiTank, Sniper, CovertOps, Machine Gunner, and Elite
    
    public class infantry
    {
    	//declare basic attributes
    	double hp, armor, effectiveHP, attack, bonus, penalty, speed, range;
    	//declares accuracy for infantry, to keep thing short
    	double accuracyInfantry, accuracySniper, accuracyCovertOps, accuracyArtillery, accuracyAircraft, accuracyHelicopters, accuracyStealth;
    	public void Soldier()
    	{
    		hp = 3;
    		armor = 10;
    		effectiveHP = 13;
    		attack = 5;
    		penalty = //groups of 10
    		speed = 2;
    		range = 8;
    	
    		accuracyInfantry = .99;
    		accuracySniper = .01;
    		accuracyCovertOps = .1;
    		accuracyArtillery = .9;
    		accuracyAircraft = .1;
    		accuracyHelicopters = .1;
    		accuracyStealth = 0.0;
    	
    	}
    	public void Sniper()
    	{
    		hp = 3;
    		armor = 1;
    		effectiveHP = 4;
    		attack = 5;
    		penalty = //groups of 10
    		speed = 2;
    		range = 8;
    	
    		accuracyInfantry = .99;
    		accuracySniper = .01;
    		accuracyCovertOps = .1;
    		accuracyArtillery = .9;
    		accuracyAircraft = .1;
    		accuracyHelicopters = .1;
    		accuracyStealth = 0.0;
    	}
    }
    That's infantry.java

    I want to create an instance of "soldier()" in BattleField.java

    By "It didn't work", Eclipse is telling me that it cant find soldier()

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Is there a soilder class somewhere then?
    Are the classes in the same package?
    Also remember that Java is case sensitive. Soilder != soilder.

  6. #6
    Kaito is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default

    ...
    hahaha........
    I was trying to use Soldier, not infantry....
    *facedesk*
    Sorry for my moron-ness, I'm still new to Java
    I'll see you guys soon with another question!

Similar Threads

  1. Importing JXL API
    By ryanlbowen in forum New To Java
    Replies: 0
    Last Post: 07-17-2009, 07:17 PM
  2. Importing excel sheets into java application
    By blizzard in forum New To Java
    Replies: 1
    Last Post: 04-02-2009, 09:59 PM
  3. Importing Picture into Java Code
    By swimmy101 in forum New To Java
    Replies: 1
    Last Post: 02-12-2009, 08:48 AM
  4. importing .java files into eclipse
    By nickl2002 in forum Eclipse
    Replies: 1
    Last Post: 01-18-2009, 11:06 PM
  5. Replies: 10
    Last Post: 10-30-2008, 04:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •