Java > AP-1 > scoreUp (CodingBat Solution)

Problem:

The "key" array is an array containing the correct answers to an exam, like {"a", "a", "b", "b"}. the "answers" array contains a student's answers, with "?" representing a question left blank. The two arrays are not empty and are the same length. Return the score for this array of answers, giving +4 for each correct answer, -1 for each incorrect answer, and +0 for each blank answer.

scoreUp({"a", "a", "b", "b"}, {"a", "c", "b", "c"}) → 6
scoreUp({"a", "a", "b", "b"}, {"a", "a", "b", "c"}) → 11
scoreUp({"a", "a", "b", "b"}, {"a", "a", "b", "b"}) → 16


Solution:

public int scoreUp(String[] key, String[] answers) {
  int score = 0;
  for (int i = 0; i < key.length; i++) {
    if (key[i] == answers[i])
      score += 4;
    else if (answers[i] != "?" && answers[i] != key[i])
      score--;
  }
  return score;
}


6 comments :

  1. public int scoreUp(String[] key, String[] answers) {

    int score = 0;

    for( int x = 0; x < key.length; x++){
    if (answers[x].equals(key[x])) score +=4;
    else
    if (!answers[x].equals("?")) score -=1;
    }

    return score;
    }

    ReplyDelete
  2. public int scoreUp(String[] key, String[] answers) {

    int res = 0;

    for (int i = 0; i < key.length; i++) {
    if (answers[i] != "?") {
    if (key[i].equals(answers[i])) {
    res += 4;
    } else {
    res--;
    }
    }
    }

    return res;
    }

    ReplyDelete
  3. public int scoreUp(String[] key, String[] answers) {
    int sum=0;
    for (int i = 0; i < answers.length; i++) {
    if(answers[i].charAt(0)!='?'){
    if(key[i].charAt(0)==answers[i].charAt(0)){
    sum+=4;
    }
    else sum-=1;
    }
    }

    return sum;
    }

    ReplyDelete
  4. Solution Using ternary operator:

    public int scoreUp(String[] key, String[] answers) {

    int score = 0;

    for(int i=0; i<answers.length; i++){

    String tmp1 = key[i];
    String tmp2 = answers[i];


    //Using ternary operator for the solution
    score = tmp2=="?" ? (score+0) : tmp1.equals(tmp2) ? (score+4) : (!(tmp1.equals(tmp2))) ? (score-1) : score;

    }
    return score;
    }

    ReplyDelete
  5. public int scoreUp(String[] key, String[] answers) {

    int count=0;
    for(int i=0;i<key.length;i++){

    if(answers[i]=="?"){
    count=count+0;
    }
    else if(key[i]==answers[i]){
    count=count+4;
    }
    else {
    count=count-1;
    }

    }
    return count;
    }

    ReplyDelete
  6. public int scoreUp(String[] key, String[] answers) {
    int score = 0;
    for( int i = 0; i < key.length; i++){
    if (Objects.equals(answers[i], key[i])) {
    score +=4;
    } else if (!Objects.equals(answers[i], "?")) {
    score -=1;
    }
    }
    return score;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact