2

This is my first time posting a question here on stack overflow, although I've received help from other answers in the past on here. I am currently working on an assignment to make a gradebook program for my programming class and I have run into some issues.

The prompt can be found here http://bit.ly/1k4osYg

The code that I have so far is:

import java.util.*;


public class temp {

    static Scanner in = new Scanner(System.in);

    public static void main(){
        menu();
    }

    public static void menu(){
        int numStudents = 0;
        int numQuizzes = 0;
        int[][] array = null;
        String choice = null;
        boolean fFlag = false;
        boolean dFlag = false;

        while(choice!="q" || choice!="Q"){
            System.out.println("Please enter a command.");
            choice = in.next();

            if(choice=="h" || choice=="H"){
                help();
            }

            else if(choice=="s" || choice=="S"){
                setParams(numStudents, numQuizzes);
                fFlag = true;
            }

            else if( choice=="f" || choice=="F"){
                if(fFlag){
                    fillArray(numStudents, numQuizzes, array);
                    dFlag = true;
                }
                else{
                    System.out.println("Please set paramaters before filling the array.");
                    clear();
                }
            }

            else if(choice=="d" || choice=="D"){
                if(dFlag){
                displayResults(numStudents, numQuizzes, array);
                }
                else{
                    System.out.println("Please fill the array before displaying");
                    clear();
                }
            }

            else if(choice=="q" | choice=="Q"){

            }
            else{
                System.out.println("Invalid command. Please enter a valid command.");
            }
        }
    }

    public static void help(){
        System.out.println("S sets paramaters for program operation, etc, etc.");
        System.out.println("F fills array, etc, etc.");
        System.out.println("D displays results, etc, etc.");
        System.out.println("H brings up the help menu, etc, etc.");
        System.out.println("Q quits the program, etc, etc.");

        clear();

    }

    public static void setParams(int numStudents, int numQuizzes){
        System.out.println("How many students are in the class?");
        numStudents = in.nextInt();
        while(numStudents > 50 || numStudents < 0){
            System.out.println("Please use a number between 0-50.");
            numStudents = in.nextInt();
        }

        System.out.println("How many quizzes are in the class?");
        numQuizzes = in.nextInt();

        while(numQuizzes > 5 || numQuizzes < 0){
            System.out.println("Please use a number between 0-5.");
            numQuizzes = in.nextInt();
        }

        clear();
    }

    public static void fillArray(int numStudents, int numQuizzes, int[][] array){
        Random gen = new Random();
        array = new int[numStudents][numQuizzes];

        for(int i = 0; i<numStudents; i++){
            for(int j = 0; j<numQuizzes; j++){
                array[i][j] = gen.nextInt(100);
            }
        }

        System.out.println("Data entry complete...");
        clear();    

    }

    public static void displayResults(int numStudents, int numQuizzes, int[][] array){
        int[] tempQuiz = new int[numStudents];

        for(int i = 0; i < numQuizzes; i++){
            for(int j = 0; j < numStudents; j++){
                tempQuiz[j] = array[j][i];
            }

            sort(tempQuiz); //wtf blizz
            float average = 0;
            for(int k = 0; k < numStudents; k++){
                average += tempQuiz[k];
            }
            average = average/numStudents;

            System.out.println("Quiz "+ i+1 +":");
            System.out.println("Lowest Grade: "+ tempQuiz[0]);
            System.out.println("Highest Grade: "+ tempQuiz[numStudents-1]);
            System.out.println("Average Grade: "+ average);
            System.out.println("Medium Grade: "+ tempQuiz[(int) numStudents/2]);
            System.out.println();
        }

        System.out.printf("Student ID\t\t");
        for(int i = 0; i < numQuizzes; i++){
            System.out.printf("Quiz %n\t\t", i+1);
        }
        System.out.println();

        for(int i = 0; i < numStudents; i++){
            System.out.printf("%n\t\t", 75678+i);
            for(int j = 0; j< numQuizzes; j++){
                System.out.printf("%n\t\t", array[i][j]);
            }
            System.out.println();
        }

        clear();

    }

    private static void clear() {
        // TODO Auto-generated method stub

    }




}

I've been working on this for four days, and I'm stuck. So any help that y'all can provide will be greatly appreciated.

Also, as a sort of noob-ish question, would it be better to put the GUI in the same class, or to write another class altogether?

EDIT THE FIRST: My brain is derping all over the place tonight, chock it up to lack of sleep over the past 48 hours. What I need help with specifically are how to sort out the highest grade for each quiz, lowest grade for each quiz, average grade for each quiz, and median grade for each quiz.

My other concern has to do with the variables. I have a distinct suspicion that when I run the program it isn't going to function properly. Should I just move the variables in the menu method as variables of the class?

My final question is whether I should use a separate class for the GUI or just include it as a part of this class.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Alex Hasen
  • 21
  • 4
  • 1
    Just from a brief glance at your code, you might want to consider moving the variables in the menu method as instance variables of the class, so that they can be set by all the methods. For example, your setParams method isn't actually modifying the variables, just local copies of them. – Vineet Kosaraju Dec 03 '13 at 05:03
  • 1
    So what is your question? What exactly is not working? – Flight Odyssey Dec 03 '13 at 05:03
  • 1
    You don't write `main` like that. Instead, you do `public static void main(String[] args) {` – MultiplyByZer0 Dec 03 '13 at 05:03
  • Does it not start? Does it start but not show anything? Does it throw an exception? – MultiplyByZer0 Dec 03 '13 at 05:04
  • Sorry for the vague nature of the OP. Added an edit with my specific issues at hand. And I don't want this to just seem like a homework dump. I'm genuinely stumped on why this isn't working. I don't want to move on to writing in the GUI until I have the back-end working flawlessly – Alex Hasen Dec 03 '13 at 05:19
  • 1
    Don't compare String with `==`. Use `equals()`. See [How Do i compare Strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Paul Samsotha Dec 03 '13 at 05:20

2 Answers2

0

You don't write main like that. Instead, you're supposed to write it like this:

public static void main(String[] args) {

If you do it that way, you end up with a nasty exception. I'm not sure if this was your problem.

MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
0

For starters, your setParams method will not work as expected. Java methods do not pass arguments by reference, so changing the value of a parametet inside of a method will not affect its value where the method was called.

Solution 1:

Return a value from the method and store that value in a variable. Methods can return only one value, so you'll have to break setParams into two methods.

Solution 2:

Pass values by updating instance variables (requires redesign of class structure).

Flight Odyssey
  • 2,267
  • 18
  • 25