Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For research purposes and to assist students, the admissions office of your loca

ID: 3538887 • Letter: F

Question

For research purposes and to assist students, the admissions office of your local university wants to know how well female and male students perform in certain courses. You receive input data that contains female and male students' GPAs for certain courses. Due to confidentiality, the letter code f is used for female students; m is used for male students. Every entry consists of a letter code followed by a GPA (for example, f 3.9). The number of entries is unknown.

Write a program that computes and outputs the average GPA for both female and male students. Format your results to two decimal places. Make your program take all the input from the user, reading in the 'm' or 'f' character followed by the GPA decimal value.

Hint: you will be using a loop that loops an unknown number of times, like until the user enters the character 'q' or some sentinel value you come up with. As you are looping, be computing the overall male and female GPA sums and how many GPAs you have entered (per male and per female). Then after your loop, compute your two averages. Be sure to not allow division by 0 (if no entries are entered)!

Add comments to your program starting with your name at the top, then throughout your program to describe what you are doing. Be sure and declare all the variables you need and make your calculations clear. Give your variables meaningful names. Also be sure to indent consistently throughout your program to make it more readable.

Explanation / Answer

import java.util.*;

class sch{

        public static void main(String[] args) {

        double msum = 0;
        double fsum = 0;
        int mcount = 0;
        int fcount = 0;
        String operation;
        double mavg = 0.0;
        double favg = 0.0;
        double input;

        Scanner sc = new Scanner(System.in);
        System.out.println("Type q to quit");
        operation = sc.next();
        while(!operation.equals("q")){

                input = sc.nextDouble();
                if(operation.equals("m")){
                        msum += input;
                        mcount ++;
                }else {
                        fsum += input;
                        fcount ++;
                }

                operation = sc.next();
        }

        if(mcount !=0){
                mavg = msum/mcount;
                System.out.printf("The average male GPA is %.2f. ",mavg);

        }
        if (fcount !=0){
                favg = fsum/fcount;
                System.out.printf("The average female GPA is %.2f. ",favg);
        }

        }
}