Avatar of gudii9
gudii9Flag for United States of America

asked on 

withoutDoubles challenge

Hi,

I am working on below challenge
http://codingbat.com/prob/p115233

I tried my code as below
public int withoutDoubles(int die1, int die2, boolean noDoubles) {

if(noDoubles==false){
return die1+die2;
}
  
  else if(die1==die2){
  return die1+die2+1;
  
  }
  else
  return 1;
}

Open in new window


I am getting below result
Expected      Run            
withoutDoubles(2, 3, true) → 5      1      X         
withoutDoubles(3, 3, true) → 7      7      OK         
withoutDoubles(3, 3, false) → 6      6      OK         
withoutDoubles(2, 3, false) → 5      5      OK         
withoutDoubles(5, 4, true) → 9      1      X         
withoutDoubles(5, 4, false) → 9      9      OK         
withoutDoubles(5, 5, true) → 11      11      OK         
withoutDoubles(5, 5, false) → 10      10      OK         
withoutDoubles(6, 6, true) → 7      13      X         
withoutDoubles(6, 6, false) → 12      12      OK         
withoutDoubles(1, 6, true) → 7      1      X         
withoutDoubles(6, 1, false) → 7      7      OK         
other tests
OK         

how to  improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?
 Please advise
JavaProgrammingJava EEProgramming Languages-Other

Avatar of undefined
Last Comment
gudii9
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

iirc, you already did this challenge quite a while ago.

?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

public int withoutDoubles(int die1, int die2, boolean noDoubles) {

if(noDoubles && die1==die2){
  return die1+die2%6+1;
}
  
 
  
  
  else
  return die1+die2; 
}

Open in new window



above passed all cases as you mentioned.

My question is can i write as below as well?

public int withoutDoubles(int die1, int die2, boolean noDoubles) {

if(noDoubles && die1==die2){
  return die1%6+die2+1;
}
  
 
  
  
  else
  return die1+die2; 
}

Open in new window



which is best approach ?

die1%6 approach or die1%6 approach?
does it vary results if i change approach to  die1%6
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

public int withoutDoubles(int die1, int die2, boolean noDoubles) {

if(noDoubles && die1==die2){
  return die1%6+die2+1;
}
  
 
  
  
  else
  return die1+die2; 
}

Open in new window


apparently above also passed all tests??
Avatar of ozo
ozo
Flag of United States of America image

die1%6 approach or die1%6 approach?
looks the same to me
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

wrapping around to 1 if its value was 6.

but challenge did not mention die1 or die2? i wonder which one to take ideally?
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

withoutDoubles(6, 5, true) →

withoutDoubles(6, 5, false) →

withoutDoubles(5, 6, true) →

withoutDoubles(5, 6, false) →

what results we get in above 4 cases ?

please advise
Avatar of ozo
ozo
Flag of United States of America image

In above 4 cases, the two dice do not show the same value, so just return the sum.
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

oops i missed the point that both dies needs to be equal to do
die1%6+die2+1
then it really does not matter die1%6 or die2%6.

How to read and comprehend challenge correctly first time itself like you people?
it is some times taking many times of going over to understand the depth and width of the challenge?
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo