Results 1 to 4 of 4
  1. #1
    babyburger is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default nextInt(unknown source)

    Heya, im new here, im trying to make a debugger's class for my working class, but when i try to call one of the methods i get a problem with my random number generator, while it works fine in the working class by itself. Here's the following code:

    HEROES CLASS
    Java Code:
    import java.util.Random;
    
    public class Heroes {
    
    private String name;
    private int hitValue;
    private int damage;
    private int currentHitpoints;
    private int maxHitpoints;
    private int protectionValue;
    private double maxCapacity;
    private double heroStrength;
    private boolean combatState;
    private Random random = new Random();
    
    private Object rechterhand;
    private Object linkerhand;
    private Object back;
    
    	public Heroes(String wantedname){
    		damage = 0;
    		protectionValue = 10;
    		heroStrength = 10;
    		combatState = false;
    		setHeroName(wantedname);
    		setMaxCapacity();
    		setHitValue();
    		setInitialHitpoints();
    	}
    	
    	public Heroes(Object linkerhand,Object rechterhand,Object back, String wantedname, int hitpoints ){
    		damage = 0;
    		protectionValue = 10;
    		heroStrength = 10;
    		combatState = false;
    		setLinkerhand(linkerhand);
    		setRechterhand(rechterhand);
    		setBack(back);
    		setHeroName(wantedname);
    		setMaxCapacity();
    		setHitValue();
    	}
    	
    /*****************************************************************************************
    * SETTERS
    ****************************************************************************************/
    	/*
    	 * Anchors
    	 */
    	public void setRechterhand(Object object){rechterhand = object;}	
    	public void setLinkerhand(Object object){linkerhand = object;}
    	public void setBack(Object object){	back = object;}
    	/*
    	 * CAPACITY
    	 */
    	public void setMaxCapacity(){
    		double strength = getStrength();
    		if(strength < 1){ strength = 0.00;}
    		if(strength > 10 && strength < 11){	strength = 10.00;}
    		if(strength > 20 && strength < 21){	strength = 20.00;}
    		if(strength >= 1 && strength <= 10){lowStrength(strength);}
    		if(strength >= 11 && strength <= 20){mediumStrength(strength);}
    		if(strength >= 21){	highStrength(strength);	}
    	}
    	/*
    	 * Maxcapacity mutators
    	 */
    	public void lowStrength(double strength){maxCapacity = 10 * strength;}
    
    	public void mediumStrength(double strength){
    		this.heroStrength = strength;
    		switch((int)this.heroStrength){
    		case 11:
    			maxCapacity = 115;
    			break;
    		case 12:
    			maxCapacity = 130;
    			break;
    		case 13:
    			maxCapacity = 150;
    			break;
    		case 14:
    			maxCapacity = 175;
    			break;
    		case 15:
    			maxCapacity = 200;
    			break;
    		case 16:
    			maxCapacity = 230;
    			break;
    		case 17:
    			maxCapacity = 260;
    			break;
    		case 18:
    			maxCapacity = 300;
    			break;
    		case 19:
    			maxCapacity = 350;
    			break;
    		case 20:
    			maxCapacity = 400;
    			break;
    		default:
    			System.out.println("Error: strength case");
    			break;
    		}
    	}
    	
    	public void highStrength(double Strength){
    		this.heroStrength = Strength - 10;
    		maxCapacity = this.heroStrength * 4;
    	}
    
    	public void setHitValue(){hitValue = random.nextInt(21);}
    	
    	public void setInitialHitpoints(){
    		int additionalHitpoints = random.nextInt(10);
    		int newInitialHitpoints =  10 + additionalHitpoints;
    		if (newInitialHitpoints > 0){
    			maxHitpoints = newInitialHitpoints;
    			}
    		setMaximumHitpoints();
    		currentHitpoints = maxHitpoints;
    	}
    	
    	public void setMaximumHitpoints(){
    		if(combatState == false){setHitpointsToPrime(maxHitpoints);}
    	}
    	
    	public void setHitpointsToPrime(int x){
    		boolean closestpriem = false;
    		while (closestpriem != true)
    		{
    		 closestpriem = isPrime(x);
    		 x++;
    		}
    		x--;
    		maxHitpoints = x;
    		}
    	
    	public void setCurrentHitpoints(int modifier){currentHitpoints += modifier;}
    	
    	public void setStrength(int multiplier){heroStrength *= multiplier;	}
    	
    	public void setHeroName(String wantedHeroname){
    
    	    try{
    	        if(validName(wantedHeroname)==true){
    	            name = wantedHeroname;
    	        }
            else{
                throw new Exception("Exception: Invalid Name");
            	}
    	    }
    	    catch(Exception e){
    	    	System.out.println(e.getMessage());
    	    }
        } 
    	
    /*****************************************************************************************
     * GETTERS
     ****************************************************************************************/
    	public String getName(){return name;}
    	
    	public boolean getCombatState(){return combatState;}
    	
    	public double getMaxCapacity(){return maxCapacity;}
    
    	public int getHitValue(){return hitValue;}
    
    	public int getDamage(){return damage;}
    	
    	public int getCurrentHitpoints(){return currentHitpoints;}
    	
    	public int getMaxHitpoints(){return maxHitpoints;}
    	
    	public int getProtectionValue(){return protectionValue;}
    
    	public double getStrength(){return heroStrength;}
    	/*
    	 * Anchors
    	 */
    	public Object getRechterhand(){return rechterhand;}
    	
    	public Object getlinkerhand(){return linkerhand;}
    	
    	public Object getBack(){return back;}
    		
    /*****************************************************************************************
     * OTHER METHODS
     *****************************************************************************************/
    	
    	public void Attack(){
    		if(compareAttackDefense() == true){
    			calculateDamage();
    			calculateAttack();
    			if(recoveryCheck() == true){
    				calculateRecovery();
    			}
    		}
    	}
    	
    	public void calculateAttack(){
    		// opponent.getHitpoints - getDamage()
    	}
    	
    	public boolean compareAttackDefense(){
    		int hit = getHitValue();
    		int protection = getProtectionValue();
    		if(hit >= protection){
    			return true;
    		}
    		else{return false;}
    	}
    	
    	public void calculateDamage(){
    		combatState = true;
    		double strength = getStrength();
    		strength -= 10;
    		strength /= 2;
    		damage = (int)strength;
    	}
    	
    	
    	public boolean recoveryCheck(){
    		Opponent opponent = new Opponent("Opponent");
    		Heroes currentOpponent = opponent.getOpponent("opponent");
    		if(getDamage() > currentOpponent.getCurrentHitpoints()){
    			combatState = false;
    			setMaximumHitpoints();
    			return true;
    		}
    		else{return false;}
    	}
    	
    	
    	public void calculateRecovery(){
    			int verlorenHitpoints = getMaxHitpoints() - getCurrentHitpoints();
    			// percentage dat hersteld wordt kan 0 zijn
    			int hitpointsRecovered = random.nextInt(verlorenHitpoints);
    			setCurrentHitpoints(hitpointsRecovered);
    		}
    		
    	public boolean isPrime(int i) {
            for (int c = 2; c < i; c++) {
                    if (i % c == 0) {
                            return false;
                    }
            }
         return true;
    	}
    		   
    	 public boolean validName(final String s) {  
    		 
            final char[] chars = s.toCharArray();
            int invalidcounter = 0;
      
            final char y = chars[0];
            if ((y >= 'A') && (y <= 'Z')) {
    		for (int x = 0; x < chars.length; x++) { 
    			final char c = chars[x]; 
    			if ((c >= 'a') && (c <= 'z')) continue;  // lowercase
    			if ((c >= 'A') && (c <= 'Z')) continue; // uppercase
    			if ((c == '\'') && invalidcounter < 2) {invalidcounter++; continue;} // ' < 2
    			if ((c == ' ')) continue;
    			        return false;                    
    		    }
    		    return true;
    		}
    		else{return false;}
    	                    
    	}
    }




    TESTHEROES CLASS (this is the debugging class)
    Java Code:
    import junit.framework.TestCase;
    import junit.framework.Test;
    import junit.framework.TestSuite;
    
    public class TestHeroes extends TestCase {
    
    	private Heroes heroes;
    	private double strength;
    	private int maxHitpoints;
    	private boolean combatState;
    	
    	public TestHeroes(String name) {
    		super(name);
    	}
    
    	protected void setUp() throws Exception {
    		super.setUp();
    		heroes = new Heroes("Tester");
    		strength = heroes.getStrength();
    		maxHitpoints = heroes.getMaxHitpoints();
    		combatState = heroes.getCombatState();
    	}
    
    	protected void tearDown() throws Exception {
    		super.tearDown();
    		heroes = null;
    		strength = 0;
    		maxHitpoints = 0;
    		combatState = false;
    	}
    	
    	public static Test suite(){
            TestSuite suite = new TestSuite();
            suite.addTest(new TestHeroes("testCapacity"));
            suite.addTest(new TestHeroes("testHitValue"));
            suite.addTest(new TestHeroes("testProtectionValue"));
            suite.addTest(new TestHeroes("testCurrentHitpoints"));
            suite.addTest(new TestHeroes("testMaximumHitpoints"));
            suite.addTest(new TestHeroes("testCompareAttackDefense"));
            suite.addTest(new TestHeroes("testCalculateDamage"));
            suite.addTest(new TestHeroes("testRecoveryCheck"));
            suite.addTest(new TestHeroes("testCalculateRecovery"));
            suite.addTest(new TestHeroes("testName"));
            return suite;
        }
    	
    	public void testCapacity(){
    		double capacity = heroes.getMaxCapacity();
    		testStrength();
    		testPositiveCapacity(capacity);
    		testLowStrength(capacity);
    		testMediumStrength(capacity);
    		testHighStrength(capacity);
    	}
    	
    	public void testStrength(){
    		if(strength < 1){assertTrue(strength == 0.00);}
    		if(strength > 10 && strength < 11){assertTrue(strength == 10.00);}
    		if(strength > 20 && strength < 21){assertTrue(strength == 20.00);}
    	}
    	
    	public void testPositiveCapacity(double capacity){
    		assertTrue(capacity >= 0);
    	}
    	
    	public void testLowStrength(double capacity){
    		if(strength < 11){
    			assertTrue(capacity == 10 * strength);
    		}
    	}
    	
    	public void testMediumStrength(double capacity){
    		if(strength >= 11 && strength <= 20){
    			if(strength == 11){assertTrue(capacity == 115);}
    			if(strength == 12){assertTrue(capacity == 130);}
    			if(strength == 13){assertTrue(capacity == 150);}
    			if(strength == 14){assertTrue(capacity == 175);}
    			if(strength == 15){assertTrue(capacity == 200);}
    			if(strength == 16){assertTrue(capacity == 230);}
    			if(strength == 17){assertTrue(capacity == 260);}
    			if(strength == 18){assertTrue(capacity == 300);}
    			if(strength == 19){assertTrue(capacity == 350);}
    			if(strength == 20){assertTrue(capacity == 400);}
    		}
    	}
    	
    	public void testHighStrength(double capacity){
    		if(strength >= 11 && strength <= 20){
    			assertTrue(capacity == strength * 4);
    		}
    	}
    	
    	public void testHitValue(){
    		int hitValue = heroes.getHitValue();
    		assertTrue(hitValue >= 0 && hitValue <= 20);
    	}
    	
    	public void testProtectionValue(){
    		assertTrue(heroes.getProtectionValue() == 10);
    	}
    	
    	public void testCurrentHitpoints(){
    		int currentHitpoints = heroes.getCurrentHitpoints();
    		assertTrue(currentHitpoints > 0);
    		assertTrue(currentHitpoints <= maxHitpoints);
    	}
    	
    	public void testMaximumHitpoints(){
    		assertTrue(maxHitpoints > 0);
    		if(combatState == false){
    			assertTrue(heroes.isPrime(maxHitpoints) == true);
    		}
    	}
    	
    	public void testCompareAttackDefense(){
    		int hit = heroes.getHitValue();
    		int protection = heroes.getProtectionValue();
    		if(hit >= protection){
    			assertTrue(heroes.compareAttackDefense() == true);}
    		else{assertTrue(heroes.compareAttackDefense() == false);}
    	}
    	
    	public void testCalculateDamage(){
    		heroes.calculateDamage();
    		combatState = heroes.getCombatState();
    		assertTrue(combatState == true);
    		int damage = heroes.getDamage();
    		assertTrue(damage == (int)((strength - 10)/2));
    	}
    	
    	public void testRecoveryCheck(){
    		heroes.recoveryCheck();
    		if(heroes.recoveryCheck() == true){
    			assertTrue(combatState == false);
    			testMaximumHitpoints();
    		}
    	}
    	
    	public void testCalculateRecovery(){
    		int defaultCurrentHitpoints = heroes.getCurrentHitpoints();
    		testCurrentHitpoints();
    		heroes.calculateRecovery();
    		int currentHitpoints = heroes.getCurrentHitpoints();
    		assertTrue(defaultCurrentHitpoints <= currentHitpoints);
    		assertTrue(currentHitpoints <= maxHitpoints);
    	}
    	
    	public void testName(){
    		String name = heroes.getName();
    		testCapitalLetter(name);
    		testApostrophe(name);
    		testAllowedChars(name);
    	}
    	
    	public void testCapitalLetter(String name){
    		 final char firstLetter = name.charAt(0);
    	     assertTrue((firstLetter >= 'A') 
    	    		 && (firstLetter <= 'Z'));
    	}
    	
    	public void testApostrophe(String name){
    		final char[] chars = name.toCharArray();
            int invalidcounter = 0;
    
    		for (int x = 0; x < chars.length; x++) { 
    			final char c = chars[x]; 
    			if (c == '\'') {
    				invalidcounter++;
    				assertTrue(invalidcounter <=2);
    			}
    		}
    	}
    	
    	public void testAllowedChars(String name){
    		final char[] chars = name.toCharArray();
      
            final char y = chars[0];
            if ((y >= 'A') && (y <= 'Z')) {
    			for (int x = 0; x < chars.length; x++) { 
    				final char c = chars[x]; 
    				assertTrue((c >= 'a') && (c <= 'z')
    						|| ((c >= 'A') && (c <= 'Z'))
    						|| (c == '\'')
    						|| (c == ' '));
    			}
            }


    OPPONENTS CLASS (this probably isnt necessary, but just to make it complete):

    Java Code:
    public class Opponent {
    
    	private Heroes opponent;
    	
    	public Opponent(String name){
    		opponent = new Heroes(name);
    	}
    	
    	public Heroes getOpponent(String name){
    		return opponent;
    	}
    	
    	
    }
    
    			       
    	}
    
    }




    THE PROBLEM

    the problem is located when i try to run the testCalculateRecovery() test;
    java.lang.IllegalArgumentException: n must be positive
    at java.util.Random.nextInt(Unknown Source)
    at Heroes.calculateRecovery(Heroes.java:246)
    at TestHeroes.testCalculateRecovery(TestHeroes.java:1 42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:164 )
    at junit.framework.TestCase.runBare(TestCase.java:130 )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at junit.framework.TestResult.run(TestResult.java:109 )
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:2 30)
    at junit.framework.TestSuite.run(TestSuite.java:225)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit 3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)

    This is the expanded msg of the error im given, basically, it hangs at
    heroes.calculateRecovery(); in the test class, and at
    int hitpointsRecovered = random.nextInt(verlorenHitpoints);
    in the working class, saying nextInt(unknown source). Ive looked this up on google and most of the problems were something along the lines of putting a string in the nextInt, which isnt my case, its clearly an int. So i'm stuck, anyone got any ideas? It'd be very appreciated!

    Moderator Edit: Code tags added
    Last edited by Fubarable; 03-18-2010 at 04:00 PM. Reason: Moderator Edit: Code tags added

  2. #2
    babyburger is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    nevermind, i just solved it myself :D Its amazing how any time i spent hours on looking at a problem i find it right after i post it on the net. Anyway, i'll share this to any future googlers ;) the integer verlorenHitpoints was always gonna be 0 cause currentHitpoints doesnt change and so it'll always be equal to maxHitpoints and subtracting one from the other results in 0. putting a zero in nextInt causes no number to be generated, so there we have it :p

    Long story short, make sure that nextInt doesnt get a zero.

    EDIT: sorry about the code tags, i didnt know i could add those. I'll remember it for next time

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hello and welcome. I've added code tags to your post above to help its readability. Please read my signature below to learn more on how to do this.

    Edit: congrats on solving this yourself! That's the best resolution one can hope for here.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

Similar Threads

  1. python source to java source converter?
    By conor147 in forum New To Java
    Replies: 0
    Last Post: 01-15-2010, 05:14 AM
  2. NullPointerException at FindAppletJDKLevel(unknown source)
    By PeteMansfield in forum Java Applets
    Replies: 18
    Last Post: 09-21-2009, 06:54 PM
  3. Hi , Error in Jsp page is as "NumberFormatException.forInputString(Unknown Source)"
    By lavanya82 in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 02-20-2009, 07:22 AM
  4. Replies: 0
    Last Post: 01-29-2009, 10:43 AM
  5. Replies: 1
    Last Post: 11-28-2008, 06:27 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
  •