10 Replies - 700 Views - Last Post: 30 January 2016 - 02:25 AM Rate Topic: -----

#1 legenda008   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-January 16

Problem with my Java Code

Posted 29 January 2016 - 05:41 PM

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

http://programmingpraxis.com/2009/08/11/uncle-bobs-bowling-game-kata/

I have written my class

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

 if(rolls[cursor]+rolls[cursor+1]>10){
throw new InvalidArgumentException;
}

but when i test it with JUnit
@Test (expected = IllegalArgumentException.class)
	public void testFrames(){
		game.roll(8,8);
	}


it doesnt give correct output

SO what should be the problem

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with my Java Code

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,302
  • Joined: 25-December 13

Re: Problem with my Java Code

Posted 29 January 2016 - 05:45 PM

Quote

it doesnt give correct output

What output does it give?
What is the correct output?

Also posted at: http://www.java-foru...-java-code.html

This post has been edited by NormR: 29 January 2016 - 05:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 legenda008   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-January 16

Re: Problem with my Java Code

Posted 29 January 2016 - 05:48 PM

it should throw IllegalArgumentException

like this

@Test (expected = IllegalArgumentException.class)
public void testFrames(){
game.roll(8,8);
}


but it doesnt ,it says test fails
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,302
  • Joined: 25-December 13

Re: Problem with my Java Code

Posted 29 January 2016 - 05:53 PM

Can you debug the code to see why line 21 with the throw statement is not executed?
Is the roll() method called? What is the value of pinsDown when it is called?


Quote

it says test fails

What does "fails" mean? Are there any error messages?
Have you tried compiling and executing to code to see what it does?

This post has been edited by NormR: 29 January 2016 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2537
  • View blog
  • Posts: 10,169
  • Joined: 03-December 12

Re: Problem with my Java Code

Posted 29 January 2016 - 06:02 PM

Is there more to this? I am seeing roll passed with 2 parameters, but the definition takes a single integer argument.
Was This Post Helpful? 0
  • +
  • -

#6 legenda008   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-January 16

Re: Problem with my Java Code

Posted 29 January 2016 - 06:14 PM

Here is my Bowling Game class


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 Test Class

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));
		
	}
	
	
	
	
	
	
	
	
	
	
	
	@Test(expected = IllegalArgumentException.class)
	public void cantScoreMoreThan10Points() {
	    game.roll(15,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3);
	}
	
	
	@Test(expected = IllegalArgumentException.class)
	public void cantScoreMorewithTwo15() {
	    game.roll(15,15,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3);
	    
	    
	}
	

	@Test (expected = IllegalArgumentException.class)
	public void testingLetter() {
	    char c = 'c';
	    game.roll(c);
	   
	}
	
	
	@Test (expected = IllegalArgumentException.class)
	public void testFrames(){
		game.roll(8,8);
	}
	
	
}

Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,302
  • Joined: 25-December 13

Re: Problem with my Java Code

Posted 29 January 2016 - 06:28 PM

Have you tried executing the code to see what the roll() method with the throw statement does?
Was This Post Helpful? 0
  • +
  • -

#8 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2537
  • View blog
  • Posts: 10,169
  • Joined: 03-December 12

Re: Problem with my Java Code

Posted 29 January 2016 - 06:30 PM

Or adding a test like this:

@Test(expected = IllegalArgumentException.class)
	public void rollException() {
		game.roll(15);
	}

Was This Post Helpful? 0
  • +
  • -

#9 legenda008   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-January 16

Re: Problem with my Java Code

Posted 29 January 2016 - 06:36 PM

View Postastonecipher, on 29 January 2016 - 06:30 PM, said:

Or adding a test like this:

@Test(expected = IllegalArgumentException.class)
	public void rollException() {
		game.roll(15);
	}



I have a method like that and it is working

@Test(expected = IllegalArgumentException.class)

public void cantScoreMoreThan10Points() {

game.roll(15,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3);
}

but i need method that will sum throws in frame and if it greater than 10

it should throw a error

like

game.roll(8,8);

this should throw error because the sum in this one frame is 16 and it is bigger than 10
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,302
  • Joined: 25-December 13

Re: Problem with my Java Code

Posted 29 January 2016 - 06:40 PM

Quote

this should throw error because the sum in this one frame is 16

Where is the sum computed that will cause the exception? Have you tried debugging the code to see if the sum is created and that value is passes to the method that throws the exception?

This post has been edited by NormR: 29 January 2016 - 06:40 PM

Was This Post Helpful? 0
  • +
  • -

#11 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: Problem with my Java Code

Posted 30 January 2016 - 02:25 AM

The code that checks for two consecutive rolls going over 10 looks fine. The problem must be with the way you incorporated it into your code. If you put it back in then we try to see what went wrong.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1