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

    Default Need Help with Java and Junit

    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 like this
    ?
    1
    Java Code:
    http://programmingpraxis.com/2009/08/11/uncle-bobs-bowling-game-ka
    ta/

    and I have writen the code for it

    Here is my Bowling 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){
            if(pinsDown>10){
                throw new IllegalArgumentException();
            }else{
            roll(pinsDown);
            }
     
        }
     
    }
     
     
    public void roll(int pinsDown){
        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 Test Class



    Java Code:
    public class TestBowling {
    private BowlingGame game;
     
    @Before
    public void setingUp(){
        game = new BowlingGame();
    }
     
    @Test
    public void canScore60(){
        game.roll(3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3);
        assertThat(game.score(), is(60));
     
    }
     
     
     
    @Test
    public void canScorePerfect(){
        game.roll(10,10,10,10,10,10,10,10,10,10,10,10);
        assertThat(game.score(), is(300));
     
    }
     
     
    @Test
    public void canScore67(){
        game.roll(3,3,3,3,3,3,3,3,4,6,3,3,3,3,3,3, 3,3, 3,3);
        assertThat(game.score(), is(67));
     
    }
     
     
    @Test
    public void canScore75(){
        game.roll(3,3,3,3,3,3,3,3,4,6,4,6,3,3,3,3, 3,3, 3,3);
        assertThat(game.score(), is(75));
     
    }
     
     
    @Test
    public void canScore70(){
        game.roll(3,3,3,3,3,3,3,3,10,3,3,3,3,3,3, 3,3, 3,3);
        assertThat(game.score(), is(70));
     
    }
     
     
    @Test
    public void canScore87(){
        game.roll(3,3,3,3,3,3,3,3,10,10,3,3,3,3,3, 3,3, 3,3);
        assertThat(game.score(), is(87));
     
    }
     
     
    @Test
    public void canScore70with10(){
        game.roll(3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,10,3,3);
        assertThat(game.score(), is(70));
     
    }
     
     
     
     
    @Test
    public void canScore84(){
        game.roll(3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,10,10,10);
        assertThat(game.score(), is(84));
     
    }
     
     
    @Test
    public void canScoreWith3and7(){
        game.roll(3,7,3,7,3,7,3,7,3,7,3,7,3,7,3,7,3,7,3,7,3);
        assertThat(game.score(), is(130));
     
    }
     
     
     
     
     
     
     
     
     
    //this tests should fail
     
    @Test (expected = IllegalArgumentException.class)
    public void testing15() {
        int a = 15;
        game.roll(a);
     
    }
     
     
     
    @Test (expected = IllegalArgumentException.class)
    public void testingLetter() {
        char c = 'c';
        game.roll(c);
     
    }
    }


    My Question is how can I test this
    I need to test with wrong inputs

    Test wrong input with 15 for one throw
    with two throws in the same frame bigger than 10
    with a letter input

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Need Help with Java and Junit

    Why are either of those wrong inputs?
    Both translate to an int[1].
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Need Help with Java and Junit

    Ah, sorry.
    Missed the check for > 10.

    What is the issue with those tests?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Need Help with Java and Junit

    Those are wrong inputs because you cannot
    throw 15 ,it cannot be number bigger that 10
    if i understand you

    sorry for my bad english

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

    Default Re: Need Help with Java and Junit

    im having trouble with writing the test in bowling test class
    for those inputs
    how do i write that???

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

    Default Re: Need Help with Java and Junit

    because it cannot be bigger than 10

    sorry for my bad english

    and how do i write test in test Class to

    test that condition ???

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Need Help with Java and Junit

    So what happens with those tests?
    The look OK to me and should pass.

    One way to check is to set the int to something < 10 and the test should fail as it won't throw the expected exception.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    camel-man is offline Member
    Join Date
    Jan 2016
    Posts
    19
    Rep Power
    0

    Default Re: Need Help with Java and Junit

    It has to do with the fact that your methods are both named roll, and only one roll method has the exception handling needed if the int is over 15. Don't use the same name for your methods unless the overloading is distinct.

    int blah(int ...) and int blah(int) are NOT distinct. When you are calling game.roll(a) it is resorting to the roll that does not throw the illegal argument.
    Last edited by camel-man; 01-29-2016 at 09:53 PM.

  9. #9
    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 Java and Junit

    They are both distinct. One takes an array of ints and the other a single int.

    Edit: and the reason it failed is because the OP never called the array version. Calling it with a character
    invoked the int version which had no validity test.

    Regards,
    Jim
    Last edited by jim829; 01-29-2016 at 10:29 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Need Help with Java and Junit

    I never even noticed there were two...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Java Testing (Junit - general question)
    By NoobieCode in forum New To Java
    Replies: 1
    Last Post: 02-25-2014, 11:14 PM
  2. New to Java 2: Simple game and a Junit question.
    By tzzt123 in forum New To Java
    Replies: 1
    Last Post: 05-13-2013, 07:44 AM
  3. Replies: 7
    Last Post: 03-06-2013, 04:14 AM
  4. Junit Automation with JAVA Parser
    By ashu_i20 in forum New To Java
    Replies: 2
    Last Post: 01-25-2013, 02:36 PM
  5. how to use junit for a java class in netbeans
    By venkatakrishna.chaithanya in forum New To Java
    Replies: 0
    Last Post: 07-15-2009, 06:41 AM

Posting Permissions

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