{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

39a-pop-quiz-solutions

This preview shows pages 1–2. Sign up to view the full content.

//PopQuiz Solutions //problem 1 public void multiplyNumbers() { int result = 1; while ( true ) { int n1 = readInt( "Gimme a number" ); if (n1 == 0) { break ; } result *= n1; } println( "Your result is: " + result); } //problem 2 public int maxOfTwoNumbers( int n1, int n2) { if (n1 > n2) { return n1; } else { return n2; } } //problem 3 - tracing code //answers are 5, 5, and "Brandon" //problem4 - ivars or locals? //paddle, ball, ballSpeeds, bricksRemaining and rgen //should be ivars, everything else should be locals //problem 5 public void countBs() { String[] names = { "brandon" , "osvaldo" , "buffy" , "jimbo" , "scooby doo" , "bobby" }; int numBs = 0; for ( int i = 0; i < names. length ; i++) { for ( int j = 0; j < names[i].length(); j++) { char ch = names[i].charAt(j); if (ch == 'b' ){ numBs++;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.