39a-pop-quiz-solutions

39a-pop-quiz-solutions - //problem 6a public void addGrade(...

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

View Full Document Right Arrow Icon
//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++; } } } println(numBs); }
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: //problem 6a public void addGrade( char grade) { grade = Character. toUpperCase (grade); if ( numCourses &lt; 20 &amp;&amp; isAGrade(grade)) { grades [ numCourses ] = grade; //i incremented numCourses after so that I could put the //first course in slot 0 of the grades array. numCourses ++; GPA = reCalculateGPA(); } } private boolean isAGrade( char letter) { return (letter &gt;= 'A' &amp;&amp; letter &lt;= 'D' || letter == 'F' ); } private void problem6b() { Student brandon = new Student( &quot;Brandon&quot; ); Student osvaldo = new Student( &quot;Osvaldo&quot; ); brandon.addGrade( 'A' ); brandon.addGrade( 'B' ); osvaldo.addGrade( 'D' ); osvaldo.addGrade( 'F' ); println( &quot;Brandon's GPA: &quot; + brandon.getGPA()); println( &quot;Osvaldo's GPA: &quot; + osvaldo.getGPA()); }...
View Full Document

This note was uploaded on 10/14/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 2

39a-pop-quiz-solutions - //problem 6a public void addGrade(...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online