0

This homework I've been doing is about asking a user to input their test scores and homework scores, and then assign them a grade using this table:

enter image description here

We haven't learned about arrays yet, and just have basic loops so far. Is there a way to do this using a for or while loop instead of dozens of if statements? This is what I have so far:

import java.util.Scanner;

public class GradeCalculator {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner scanner = new Scanner(System.in);
    int numberOfAssignments = 7;
    int numberOfLabs = 16 ;
    int totalAssignmentScore;
    int totalLabScore;
    int totalTestScore = 0;
    int totalHomeworkScore = 0;
    int zyante;
    int attendance;
    int midterm1;
    int midterm2;
    int finalTest;
    int quizScore;
    int pat;


    for (int i = 1; i < numberOfAssignments; i++) {
        System.out.println("Enter your score obtained in Assignment No."+ i + ":");
        totalAssignmentScore = scanner.nextInt();
        {
            totalHomeworkScore += totalAssignmentScore;
            System.out.println(totalHomeworkScore);
        }

    }
    for (int i = 1; i < numberOfLabs; i++) {
        System.out
                .println("Enter your score obtained in Lab No." + i + ":");
        totalLabScore = scanner.nextInt();
        {
            totalHomeworkScore += totalLabScore;
            System.out.println(totalHomeworkScore);
        }
    }
    System.out.println("Enter your score obtained in Zyante: ");
    zyante = scanner.nextInt();
    totalHomeworkScore +=zyante;

    System.out.println("Enter your total score obtained in Attendance: ");
    attendance = scanner.nextInt();
    totalHomeworkScore += attendance;

    System.out.println("Enter your Midterm no. 1 score: ");
    midterm1 = scanner.nextInt();
    totalTestScore += midterm1;

    System.out.println("Enter your Midterm no. 2 score: ");
    midterm2 = scanner.nextInt();
    totalTestScore += midterm2;

    System.out.println("Enter your Final exam score: ");
    finalTest = scanner.nextInt();
    totalHomeworkScore += finalTest;

    System.out.println("Enter your total score in the in-class Quizzes: ");
    quizScore = scanner.nextInt();
    totalHomeworkScore += quizScore;

    System.out.println("Enter your score for PAT: ");
    pat = scanner.nextInt();
    totalHomeworkScore += pat;


    System.out.println("Your total test score is: " + totalTestScore );
    System.out.println("your total homework score is: " + totalHomeworkScore);





}
}
5
  • Am I correct that you want to be able to output the result of totalTestScore + totalHomeworkScore = GRADE, where GRADE is G, A, or P`? Sep 30, 2013 at 15:12
  • 2
    is that just an arbitrary table, or can you arithmetically calculate a grade based on both scores? Sep 30, 2013 at 15:13
  • We can't add the two together, so it has to be something like "If your test score is X and your homework score is Y, you pass/fail the class" So if your grade is G or A, you pass the class Sep 30, 2013 at 15:16
  • 1
    If the cutoffs are arbitrary then you'll need to learn about arrays, if there is an algorithm then you can use that instead. Sep 30, 2013 at 15:16
  • Have you learned about integer division/modulo? If you have (actually, even if you haven't), you can use those to reduce the problem to a few special cases and linear combinations of test and homework scores. Notice the "G" area is bordered by a line from the upper left to lower right, plus a few more "bulging" to the left. Also notice the grade is "P" if T<150 or H<600. Patterns like these can allow you to simplify the problem. Not sure if this is what your instructor intended or not. Sep 30, 2013 at 15:30

1 Answer 1

1

If you're really not supposed to use arrays then you just need to analyse your array and pull out the logic from it.

Start with something like:

enum Grade {
  P,
  A,
  G;
}

public Grade score ( int tests, int homework ) {
  if ( homework <= 599
          || tests <= 149 ) {
    return Grade.P;
  }
  return Grade.G;
}

enter image description here

Then peel off some more:

public Grade score ( int tests, int homework ) {
  if ( homework <= 599
          || tests <= 149 
          || (homework <= 719 && tests <= 179 )) {
    return Grade.P;
  }
  return Grade.G;
}

and you have:

enter image description here

and so on until you have detailed all of the data.

There are slightly tidier ways of organising your if statements and more logical ways of peeling off the sections of your table but essentially, without arrays or an actual algorithm this is really all that is possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.