Consider a grading system where numbers are turned into letters. Fill in the blanks in the following code to complete the Boolean expression.

float grade = random(0,100);

if (_________________) {
  println("Assign letter grade A.");
} else if (_________________) {
  println(_____________________);
} else if (_________________) {
  println(_____________________);
} else if (_________________) {
  println(_____________________);
} else {
  println(_____________________);
}

Answer:

float grade = random(0,100);

if (grade > 90) {
  println("Assign letter grade A.");
} else if (grade > 80) {
  println("Assign letter grade B.");
} else if (grade > 70) {
  println("Assign letter grade C.");
} else if (grade > 60) {
  println("Assign letter grade D.");
} else {
  println("Assign letter grade F.");
}
  • http://yahoo.com Zaira

    Shouldn’t we write it as
    if (grade>=90){
    println(“Assign Letter grade A.”);
    }else if (grade>=80){
    println(“Assign letter grade B.”);

    and so forth.
    or does the code already set that limit so in which the second conditional “If grade is greater than 80 assign grade B” will only go from 80-89. I feel like since 91 is greater than 80 it will still assign a B letter grade.

  • admin

    Yes, technically speaking this would be more precise, in terms of grading!

  • http://blog.gabrielmathews.com Gabe

    Is there another way of defining ranges of numbers in the conditionals block?

    else if(grade = 70-79){
    printIn(“You just earned a C, genius”);
    }

    I return an error in this way. I know that I should use <=79. Is it that Processing is reading "70-79" as 70 minus 79 versus 70 through 79? I tried a comma, like in the random() function, but failed. Just wondering.

  • http://www.learningprocessing.com admin

    You have to use a logical “AND” represented by ‘&&’, i.e.

    if (grade >= 70 && grade < 80) {
      println("Grade is greater than or equal to 70 and less than 80.");
    }
    
  • http://blog.gabrielmathews.com Gabe

    that is why you’re the teacher…*bows

  • Andre

    // what if you wrote in this code so that the code is run dynamic instead of static
    // You notice that the same results occurs 
    // How to you fix this so that different println comments are shown instead of assign letter B all the time.

    float grade = random(100);

    void setup() {
      size(200, 200);
    }

    void draw() {
      
      //grade = random(100);

    if (grade =71) {
      println(” Assign letter B.”);
    }

    else if (grade =61) {
      println(“Assign letter C.”);
    }

    else if (grade =51) {
      println(“Assign letter D.”);
    }
      
      else {
        println(“Failed.”);
      }
      
    }

  • Anonymous

    I would suggest doing something like when you click the mouse a new grade is picked.

    void mousePressed() {
      grade = random(100);
    }