import java.util.Scanner;

public class SwitchStatement {
    private String courseName; 
    private int total;
    private int gradeCounter;
    private int aCount;
    private int bCount;
    private int cCount;
    private int dCount;
    private int fCount; 

    public SwitchStatement(String name){
        courseName = name;
    }
    public String getCourseName(){
        return courseName;
    }
    public void displayMessage(){
        System.out.println("Welcome to the grade book for " + getCourseName());
    }
    public void displayMessage1(){
        System.out.println("Enter The ineger grades in the range of 0 - 100\n"
                +"Please Enter <Ctrl> z then press enter to stop entering grades.");
        }
    public void counter(){
        int grade; 
        Scanner input = new Scanner(System.in);
        System.out.println("Please Enter Your Grades or -1 to quit: ");
        grade = input.nextInt();
    if (grade != -1){   
        while (grade != -1){
            grade = input.nextInt();
            gradeCounter = gradeCounter + 1;
            total = total + grade; 
            }//while statement ends
        }else System.out.println("You have not entered any grades.");
    }//method ends
    public void counterofletters(int grade){

        switch (grade / 10){
        case 9:
        case 10:
            ++aCount;
            break;
        case 8:
            ++bCount;
            break; 
        case 7: 
            ++cCount;
            break;
        case 6: 
            ++dCount;
            break;
        default: 
            ++fCount;
            break; 
        }//switch end
    }//method end
    public void displayValues(){
        double average; 
        average = (double) total / gradeCounter; 
        System.out.println("The number of grades entered is " + gradeCounter);
        System.out.println("The total of the grades is " + total);
        System.out.println("The Average of the grades is " + average);
        System.out.println("The number of students who received each grade:\n"
                            + "Grade A:" + aCount + "\nGrade B: " + bCount +
                            "\nGrade C: " + cCount + "\nGrade D:" + dCount + 
                            "\nGrade F: " + fCount);
    }
}//Class end

    //new Class 

public class SwitchStatementTest {
    public static void main(String[] args){

        SwitchStatement myStatement = new SwitchStatement("Biology");
        myStatement.displayMessage();
        myStatement.displayMessage1();
        myStatement.counter();
        myStatement.counterofletters(0);
        myStatement.displayValues();




    }
}

Here, i could not understand how to call counterofLetters method in the driver class.

Recommended Answers

All 6 Replies

By the way, i don't get any error message, but all the grades in a,b,c,d,f are 0.

Try debugging the code by adding println statements to print out the values of the variables as they are changed and to show the execution flow. Put a println in each method.

Try debugging the code by adding println statements to print out the values of the variables as they are changed and to show the execution flow. Put a println in each method.

Actually, that is not the point. i could not understand how to call the method counterofLetters, because it has a parameter "grade" and when i write grade as the parameter in the driver class, i gives an error message. Im asking how to call the method properly.

Where is the data that is supposed to be passed to the method counterofLetters()?
That would be the place to call it.

just out of curiosity, why is your switch statement set up the way it is with 9 first then 10, 8, ... and so on?

just out of curiosity, why is your switch statement set up the way it is with 9 first then 10, 8, ... and so on?

@corby: That is because he wants to increment the grade first. So, 9 and 10 belongs to A grade and he have a break at 9 so it will fall over to grade 10 which is still A grade. Then every 1 point down (10 if you did not devide by 10) you drop the grade and increment its value,

You can do it in order starting from 6 onwards but I guess that is his preference.

@GeekTool
I have look at your code. I think I found your problem. You do not need to do the increment of grade in your driver class. It would make more sense if you increment it after you have a grade input. Which means I think you should input it at line 35. You do know that a method can call another method from the same class right?

Also, I think you can improve your counter method (no offence). You have an input at line 30 and another at line 33. This means that your first input will be overwritten by your second input. This would give you a more accurate reading.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.