Results 1 to 5 of 5
  1. #1
    legenda008 is offline Member
    Join Date
    Jan 2016
    Posts
    6
    Rep Power
    0

    Unhappy Need help with my Java Code

    Ok guys I neeed quick help with my assigment I think it is not something difficult You see I have to write Bowling Code kata
    Java Code:
    http://programmingpraxis.com/2009/08/11/uncle-bobs-bowling-game-kata/
    I have written my class

    Java Code:
    public class BowlingGame {
    
    
    
        private int roll = 0;
        private int [] rolls = new int[21];
    
    
        public void roll(int...rolls){
            for(int pinsDown:rolls){
    
                roll(pinsDown);
    
            }
    
        }
    
    
        public void roll(int pinsDown){
             if(pinsDown > 10 ){
                    throw new IllegalArgumentException("illegal argument ");
                }
            rolls[roll++] =pinsDown;
        }
    
    
        public int score(){
            int score = 0;
            int cursor = 0;
    
            for(int frame = 0;frame<10;frame++){
    
    
    
                 if(rolls[cursor]==10){ // check if it is strike
                    score+=10 + rolls[cursor+1] + rolls[cursor+2];
                    cursor++;
                }else if(rolls[cursor] + rolls[cursor+1]==10){ // check if it is spare
                    score+=10 + rolls[cursor+2];
                    cursor+=2;
                }
                else {
                    score+=rolls[cursor] + rolls[cursor+1];
                    cursor+=2;
                }
    
            }
            return score;
    
    
        }
    and here is my problem

    I need to make that two throws in the same frame are not bigger than 10 and if that happen it has to throw exception

    I have tried to write it like this

    Java Code:
     if(rolls[cursor]+rolls[cursor+1]>10){
    throw new InvalidArgumentException;
    }
    but when i test it with JUnit

    it doesnt give correct output

    SO what should be the problem

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Need help with my Java Code

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help with my Java Code

    Looks like its also related to this local post - http://www.java-forums.org/new-java/...ava-junit.html

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    legenda008 is offline Member
    Join Date
    Jan 2016
    Posts
    6
    Rep Power
    0

    Default Re: Need help with my Java Code

    SO can somebody help me :)

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help with my Java Code

    Quote Originally Posted by legenda008 View Post
    SO can somebody help me :)
    I already explained what the problem might be in the other post. You are calling the version of roll which takes an integer. The version of roll which throws the exception is the array version.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Compiling another java code from our code
    By madhav.dubey9 in forum New To Java
    Replies: 18
    Last Post: 03-11-2013, 04:54 PM
  2. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  5. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 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
  •