0

I am having difficulty with the getCategoryWeights method (at the bottom of my code) I am writing. It is supposed to change the values of the weight column based on the user's input, but I can't figure out why it isn't working. Here is my program so far:

package gradeprojector;

    //Done

    // Print a welcome message
    // printWelcomeMessage();
    // Get the number of gradebook categories.
    // int categories = getGradebookCategories();
    // Get the maximum number of assignments in a category.
    // int maxAssignments = getMaximumAssignments();
    // Create the 2D array to hold assignment scores
    // double[][] scoreArray = getScoreArray(categories, maxAssignments);

    //Not Done

    // Get the category weights
    // getCategoryWeights(scoreArray);
    // Get the category scores
    // getScores(scoreArray);
    // Compute the category averages
    // computeCategoryAverages(scoreArray);
    // Compute the overall average
    // double overallAverage = computeOverallAverage(scoreArray);
    // Compute the final exam scores needed to get a certain grade (just do A,B,C,D)
    // double[] targetFinalScores = getTargetFinalScores(overallAverage);
    // Print target final exam scores
    // printTargetFinalExamScoes(targetFinalScores);

import java.util.*;
public class GradeProjector {

    public static void main(String[] args) {
        // TODO code application logic here
        printWelcomeMessage();
        int categories = getGradebookCategories();
        int maxAssignments = getMaximumAssignments();
        double[][] scoreArray = getScoreArray(categories, maxAssignments);
        printScoreArray(scoreArray, maxAssignments);
        scoreArray = getCategoryWeights(scoreArray);
        printScoreArray(scoreArray, maxAssignments);
    }

    // printWelcomeMessage();
    public static void printWelcomeMessage(){
        System.out.println("Welcome!");
    }

    // Get the number of gradebook categories.
    public static int getGradebookCategories(){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number of gradebook categories: ");
        int categories = in.nextInt();
        return categories;
    }

    // Get the maximum number of assignments in a category.
    public static int getMaximumAssignments(){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the maximum number of assignments: ");
        int maxAssignments = in.nextInt();
        return maxAssignments;
    }

    // Create the 2D array to hold assignment scores
    public static double[][] getScoreArray(int categories, int maxAssignments){
        double[][] scoreArray = new double [categories] [maxAssignments+2];

        for(int i = 0; i < scoreArray.length; i++){
            for(int j = 0; j < scoreArray[i].length; j++){
                scoreArray[i][j] = -999;  
            }
        }
        return scoreArray;
    }

    public static void printScoreArray(double[][] scoreArray, int maxAssignments){
        System.out.println("");
        int compensate = -1;
        for(int i = 1; i<=scoreArray[0].length;i++ ) {
            if(i == scoreArray[0].length){
                System.out.print(" Average");
            }
            else if(i == 1)
                System.out.print("   Weight ");
            }         
            else{
                System.out.print(" Grade"+(i+compensate)+" ");
            }
        }
        System.out.println();
        if(maxAssignments == 1)
            System.out.println("  ------------------------");
        else if(maxAssignments == 2)
            System.out.println("  ---------------------------------");
        else if(maxAssignments == 3)
            System.out.println("  ----------------------------------------");
        else if(maxAssignments == 4)
            System.out.println("  ------------------------------------------------");
        else if(maxAssignments == 5)
            System.out.println("  --------------------------------------------------------");
        else if(maxAssignments == 6)
            System.out.println("  ----------------------------------------------------------------");
        else if(maxAssignments == 7)
            System.out.println("  ------------------------------------------------------------------------");
        else if(maxAssignments == 8)
            System.out.println("  --------------------------------------------------------------------------------");
        else if(maxAssignments == 9)
            System.out.println("  ----------------------------------------------------------------------------------------");
        else if(maxAssignments == 10)
            System.out.println("  -------------------------------------------------------------------------------------------------");
        else if(maxAssignments == 11)
            System.out.println("  ----------------------------------------------------------------------------------------------------------");
        else if(maxAssignments >= 12)
            System.out.println("  -------------------------------------------------------------------------------------------------------------------");

        for(int i = 0; i < scoreArray.length; i++){
            for(int j = 0; j < scoreArray[i].length; j++){
                double tempDouble = scoreArray[i][j];
                if(j==0){
                    System.out.format("%1d| ",i+1);
                    System.out.print(tempDouble+"  ");
                }
                else{
                    System.out.print(tempDouble+"  ");
                }

            }
            System.out.println();
        }
    }

    // Get the category weights
    public static double[][] getCategoryWeights(double[][] scoreArray){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the category weights: ");

        for(int i = 0; i < scoreArray.length; i++){
            for(int j = 0; j < scoreArray[i].length; j++){
                double temp = in.nextDouble();
                scoreArray[0][j] = temp;  
            }
        }
        return scoreArray;
    }
}
2
  • 1
    Inside the double loop should scoreArray[0][j] = temp; be scoreArray[i][j] = temp;? Commented Nov 20, 2014 at 5:08
  • That helps me with populating the 2d array, but what I need to do is first populate the first column "weight", and then populate each row, obviously skipping the first column that was just populated. Commented Nov 20, 2014 at 5:14

3 Answers 3

1

if you want to change the values of weights column only then you should change the like

 for(int i = 0; i < scoreArray.length; i++){
        //for(int j = 0; j < scoreArray[i].length; j++){
            double temp = in.nextDouble();
            scoreArray[i][0] = temp;  
        //}
    }

this will take input for weights column only...

Sign up to request clarification or add additional context in comments.

1 Comment

My fix that I came up with works too, but yours is more efficient and less code. I realize now how simple of a mistake it was, probably need some sleep. Thanks for the help.
0

As mdnghtblue's comment says, change it too

for(int i = 0; i < scoreArray.length; i++){
     for(int j = 0; j < scoreArray[i].length; j++){
         double temp = in.nextDouble();
         scoreArray[i][j] = temp;  
     }
 }

Also missing a little {' :P

if(i == scoreArray[0].length){
    System.out.print(" Average");
}
else if(i == 1) { // { was missing
   System.out.print("   Weight ");
}         
else{
   System.out.print(" Grade"+(i+compensate)+" ");
}

Comments

0

I just figured out the problem. Thank you mdnghtblue for the input. Changed it to:

scoreArray[i][j] = temp

and changed the nested for loop to:

for(int j = 0; j < 1; j++){

Working method:

public static double[][] getCategoryWeights(double[][] scoreArray){
    Scanner in = new Scanner(System.in);
    System.out.print("Enter the category weights: ");

    for(int i = 0; i < scoreArray.length; i++){
        for(int j = 0; j < 1; j++){
            double temp = in.nextDouble();
            scoreArray[i][j] = temp;  
        }
    }
    return scoreArray;
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.