Results 1 to 6 of 6
Thread: Importing a .java
- 09-23-2009, 05:40 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
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)
- 09-23-2009, 06:35 AM #2
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
- 09-23-2009, 01:04 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 09-23-2009, 01:20 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
haha...forgot about the code tags...
That's BattleField.java.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 infantry.javaJava 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; } }
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()
- 09-23-2009, 02:46 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Is there a soilder class somewhere then?
Are the classes in the same package?
Also remember that Java is case sensitive. Soilder != soilder.
- 09-24-2009, 01:33 AM #6
Member
- Join Date
- Sep 2009
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Importing JXL API
By ryanlbowen in forum New To JavaReplies: 0Last Post: 07-17-2009, 07:17 PM -
Importing excel sheets into java application
By blizzard in forum New To JavaReplies: 1Last Post: 04-02-2009, 09:59 PM -
Importing Picture into Java Code
By swimmy101 in forum New To JavaReplies: 1Last Post: 02-12-2009, 08:48 AM -
importing .java files into eclipse
By nickl2002 in forum EclipseReplies: 1Last Post: 01-18-2009, 11:06 PM -
Java - Importing from a Jar file hav touble with set CLASSPATH
By minifish in forum New To JavaReplies: 10Last Post: 10-30-2008, 04:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks