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 5 of 5

Thread: Java beginner is confused....

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java beginner is confused....

    Ok so In my java programming class we were supposed to write a class pretty much that makes the "CheckersTable" class work. I am confused with the error message I am getting as I can't find any apparent errors in my program. The error isn't with the CheckersTable class, as we were instructed not to edit it, just to utilize it (though I will include it in my post anyway just in case it would be useful in helping me figure this out). The run-time error doesn't occur until after the program has gathered the data from the user.

    If anyone has any pointers as to the error I'm getting I would me most grateful for your help.

    Error message :

    Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
    at java.util.Random.nextInt(Random.java:265)
    at Contestant.takeTurn(Contestant.java:50)
    at CheckersTable.simulateTurn(CheckersTable.java:65)
    at CheckersTable.simulateGame(CheckersTable.java:46)
    at checkers.main(checkers.java:37)


    Main class, this is what I have to execute to pass this off
    import java.util.Scanner;
    public class checkers
    {
    	public static void main (String[] args)
    	{
    		String name1, name2;
    		int skilllevel1, skilllevel2, maxturntime1, maxturntime2;
     
    		Scanner scan = new Scanner (System.in);
     
    		System.out.println ("Player 1 Name");
    		name1 = scan.next();
     
    		System.out.println ("Player 1 Skill Level (1-10)");
    		skilllevel1 = scan.nextInt();
     
    		System.out.println ("Player 1 max time per turn");
    		maxturntime1 = scan.nextInt();
     
    		System.out.println ("Player 2 Name");
    		name2 = scan.next();
     
    		System.out.println ("Player 2 Skill Level (1-10)");
    		skilllevel2 = scan.nextInt();
     
    		System.out.println ("Player 2 max time per turn");
    		maxturntime2 = scan.nextInt();
     
    		Contestant player1 = new Contestant(name1, skilllevel1, maxturntime1);
    		Contestant player2 = new Contestant(name2, skilllevel2, maxturntime2);
     
    		int gametimeperplayer; 
    		gametimeperplayer = ((maxturntime1 + maxturntime2)/2);
     
    		CheckersTable game = new CheckersTable(player1, player2, gametimeperplayer);
     
    		game.simulateGame();
     
    		Contestant gamehistory;
    		gamehistory = game.getWinner();
     
    		String gamewinner;
    		gamewinner = game.getMoveHistory();
     
    		System.out.println (gamewinner + " won the game");
    		System.out.println (gamehistory);
     
    		int clock1, clock2;
     
    		clock1 = player1.getClockTime();
    		clock2 = player2.getClockTime();
    		System.out.println ("Player 1 had " + clock1 + " Seconds left");
    		System.out.println ("Player 2 had " + clock2 + " Seconds left");
     
    		int pieces1, pieces2;
     
    		pieces1 = player1.getNumPieces();
    		pieces2 = player2.getNumPieces();
     
    		System.out.println ("Player 1 had " + pieces1 + " pieces left");
    		System.out.println ("Player 2 had " + pieces2 + " Pieces left");
     
     
     
    	}
    }

    Contestant class, I had to write this as well.
    import java.util.Random;
    public class Contestant
    {
    String Name;
    int skillValue, maxTurnTime, timeRemaining, piecesLost, numlost, seconds, remaining;
     
    public Contestant (String name, int Skillvalue, int maxturntime)
    {
    	name = Name;
    	Skillvalue = skillValue;
    	maxturntime = maxTurnTime;
    }
    public int getClockTime()
    {
    return seconds;
    }
     
    public String getName()
    {
    return Name;
    }
     
    public int getNumPieces()
    {
    int remaining = 12;
    remaining =  remaining - numlost;
    return remaining;
    }
     
    public int getSkillValue()
    {
    return skillValue;
    }
     
    public void initializeGameClock (int seconds)
    {
    seconds = timeRemaining;
     
    }
     
    public void losePieces (int numlost)
    {
    numlost = piecesLost;
    }
     
    public int takeTurn ()
    {
    Random turnTime = new Random();
    int turnDuration;
    turnDuration = turnTime.nextInt(maxTurnTime);
    timeRemaining = timeRemaining - turnDuration;
    return turnDuration;
    }
    }

    This is the class I am not supposed to have to even look at
    /**
     * Checkers table is a class provided to you for lab 3.  This class contains methods necessary for simulating a checkers game.
     * Remember, this class has been provided for you and your job is to study the methods so you can use them to complete the lab assignment.
     */
     
    public class CheckersTable {
     
    	  private Contestant ad3f5d;
    	  private Contestant f21c3;
    	  private int c3dd3a;
    	  private String b3e342;
    	  private Contestant da329f;
     
    	  /**
    	   * 
    	   * @param player1 The fist player in the checkers game
    	   * @param player2 The second player in the checkers game
    	   * @param timePerPlayer The number of seconds given to each player for their moves
    	   */
     
    	  public CheckersTable(Contestant player1, Contestant player2, int timePerPlayer)
    	  {
    	    this.ad3f5d = player1;
    	    this.f21c3 = player2;
    	    this.c3dd3a = timePerPlayer;
     
    	    this.b3e342 = "MOVE HISTORY:\nName\t\tOpponent's Pieces Captured\t\tTime Left\tPieces Left\n-----------------------------------------------------------------------------------\n";
     
    	    this.da329f = null;
    	  }
     
    	  /**
    	   * Simulates a game on the checkers table with the two players specified when the CheckersTable object was constructed.
    	   * After a game has been simulated, the getMoveHistory and getWinner can be used to obtain information about the simulated game.
    	   */
     
    	  public void simulateGame()
    	  {
    	    this.ad3f5d.initializeGameClock(this.c3dd3a);
    	    this.f21c3.initializeGameClock(this.c3dd3a);
     
    	    int i = 0;
    	    do
    	    {
    	      simulateTurn(++i); }
    	    while (!(isGameOver()));
    	  }
     
    	  private void simulateTurn(int paramInt)
    	  {
    	    Contestant localContestant1;
    	    Contestant localContestant2;
    	    if (paramInt % 2 == 0)
    	    {
    	      localContestant1 = this.ad3f5d;
    	      localContestant2 = this.f21c3;
    	    }
    	    else
    	    {
    	      localContestant1 = this.f21c3;
    	      localContestant2 = this.ad3f5d;
    	    }
     
    	    int i = localContestant1.takeTurn();
    	    int j = calcPiecesCaptured(localContestant1, localContestant2);
    	    localContestant2.losePieces(j);
    	    CheckersTable tmp57_56 = this; tmp57_56.b3e342 = tmp57_56.b3e342 + localContestant1.getName() + "\t\t" + j + " captured in " + i + " seconds" + (i < 10 ? "\t\t\t" : "\t\t") + localContestant1.getClockTime() + " seconds\t" + localContestant1.getNumPieces() + " pieces\n";
    	  }
     
    	  private int calcPiecesCaptured(Contestant paramContestant1, Contestant paramContestant2)
    	  {
    	    int j;
    	    int i = (paramContestant1.getSkillValue() - paramContestant2.getSkillValue()) * 4;
    	    double d = Math.random() * 100.0D + i;
     
    	    if ((d < 45.0D) || (paramContestant1.getClockTime() <= 0))
    	      j = 0;
    	    else
    	    {
    	      d -= 45.0D;
     
    	      if (d < 45.0D)
    	        j = 1;
    	      else
    	        j = 2;
     
    	    }
     
    	    if (paramContestant2.getNumPieces() < j)
    	      j = paramContestant2.getNumPieces();
     
    	    return j;
    	  }
     
    	  private boolean isGameOver()
    	  {
    	    boolean i = true;
     
    	    if (this.ad3f5d.getClockTime() <= 0)
    	      this.da329f = this.f21c3;
    	    else if (this.f21c3.getClockTime() <= 0)
    	      this.da329f = this.ad3f5d;
    	    else if (this.ad3f5d.getNumPieces() <= 0)
    	      this.da329f = this.f21c3;
    	    else if (this.f21c3.getNumPieces() <= 0)
    	      this.da329f = this.ad3f5d;
    	    else
    	      i = false;
     
    	    return i;
    	  }
     
    	  /**
    	   * Gets the move history for the game after the game has been simulated.
    	   * 
    	   * @return A string containing the whole move history for the game
    	   */
     
    	  public String getMoveHistory()
    	  {
    	    return this.b3e342;
    	  }
     
    	  /**
    	   * Gets the winner of the game after the game has been simulated.
    	   * 
    	   * @return A contestant object corresponding to the winner of the game
    	   */
     
    	  public Contestant getWinner()
    	  {
    	    return this.da329f;
    	  }
    }
    Also, this is the link to the API which we were supposed to design the Contestant class around and by which we were supposed to utilize the CheckersTable class.

    Generated Documentation (Untitled)


  2. #2
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: Java beginner is confused....

    Your method takeTurn() of Contenstant class is making this to happen.

    Whats happening is Your Random.nextInt(int) is getting passed a negative value. Which means that

    turnDuration = turnTime.nextInt(maxTurnTime);
    Here, the "maxTurnTime" is negative. So verify that value before entering the method.

    Have a look at the method API here : nexiInt(int)

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  3. The Following 3 Users Say Thank You to goldest For This Useful Post:

    javapenguin (January 21st, 2011), JavaPF (January 21st, 2011), truebluecougarman (January 21st, 2011)

  4. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java beginner is confused....

    Quote Originally Posted by goldest View Post
    Your method takeTurn() of Contenstant class is making this to happen.

    Whats happening is Your Random.nextInt(int) is getting passed a negative value. Which means that

    turnDuration = turnTime.nextInt(maxTurnTime);
    Here, the "maxTurnTime" is negative. So verify that value before entering the method.

    Have a look at the method API here : nexiInt(int)

    Hope that helps,

    Goldest
    Thank you! That was exactly what I was looking for. One more question though. If I understand right, 'maxTurnTime' must be positive otherwise it throws an error. Is there any other way to ensure a positive value other than the use of an 'if' statement?

  5. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java beginner is confused....

    Nevermind, I used the Math.abs(int) method and now it works nicely. Thank you for your help!

  6. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java beginner is confused....

    Hey there, I'm pretty new to Java and was trying to figure out your code. Where exactly did you add the Math class?

Similar Threads

  1. Java Beginner Here!
    By j3nn42o in forum The Cafe
    Replies: 10
    Last Post: January 10th, 2011, 04:57 AM
  2. Java File IO question :confused:
    By byebyebye in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: August 17th, 2010, 06:45 AM
  3. A java beginner needs a lot of help and ideas
    By vesa in forum Java Theory & Questions
    Replies: 1
    Last Post: May 24th, 2010, 09:46 PM
  4. Java Beginner
    By rannoune in forum Java Theory & Questions
    Replies: 3
    Last Post: December 25th, 2009, 03:30 AM
  5. Confusion about Java development IDES
    By neo_2010 in forum The Cafe
    Replies: 4
    Last Post: July 7th, 2009, 03:14 PM