Java program For research purposes and to assist students th

Java program

For research purposes and to assist students, the admissions office of your local university wants to determine the performance of female and male students in certain courses. You receive a file that contains female and male student\'s GPAs for those courses. in the file, the letter code f is used for female students and m is used for male students. Every file entry consists of a letter code followed by a GPA. Each line has one entry. The number of entries in the file is unknown. Write a program that computes and outputs the average GPAs for both female and male students. Format your results to two decimal places.

Type the following data into a Notepad file and use it for your input.

f 3.40

m 4.00

m 2.75

f 3.00

f 3.75

f 2.00

m 1.88

f 3.00

m 3.60

f 3.80

Solution

import java.io.*;
import java.util.*;

public class GPA
{
public static void main (String args[]) throws FileNotFoundException
{
Scanner inFile=new Scanner(new FileReader (\"inputfile.txt\")); // it creates a new scanner object & inputfile is the input filename.
{ //varibale decalration : here 1 is used for male , 2 use for female. i.e count1 is countmale.
double count1=0;
double count2=0;
double gpa1=0;
double gpa2=0;
double sum1=0;
double sum2=0;
String gender;
double avg1=0;
double avg2=0;
  
while(inFile.hasNext()) // loop contuies untill data inside the text file
{
gender = inFile.next();
  
if(gender.charAt(0)==\'m\') // claculation for male gpa
{
gpa1=inFile.nextDouble();
sum1=sum1+gpa1;
count1++;
gender=inFile.next();
}
else
if(gender.charAt(0)==\'f\') // claculation for female gpa
{
gpa2=inFile.nextDouble();
sum2=sum2+gpa2;
count2++;
gender=inFile.next();
}
}
avg1=sum1/count1;
avg2=sum2/count2;
System.out.printf(\"Male gpa average is : \" + \"%.2f\", avg1);
System.out.println();
System.out.printf(\"Female gpa average is : \" + \"%.2f\", avg2);
}
}
}

Java program For research purposes and to assist students, the admissions office of your local university wants to determine the performance of female and male
Java program For research purposes and to assist students, the admissions office of your local university wants to determine the performance of female and male

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site