Tuesday 13 June 2017

Java Code to find out the student pass count , pass percentage and total number of marks where the data comes from a file

Problem:


Given a set of n student marks in a file. For this set of marks determine the total number of marks, the number of passes, and the percentage pass rate.

Algorithm:

1. Read the number of marks to be processed  from a file.
2. Initialize the count to zero.
3. While there are still marks to be processed repeatedly do
     a. if it is a pass (i.e >= 50) then add one to count.
4. To find the pass percentage divide the counter value with number of marks.
5. To find the total number of marks
    a. Initialize the sum to zero.
    b. loop the marks to add sum value to each mark.
6. Print the result of passed count, pass percentage and total number of marks

Solution:

package com.myprograms;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class StudentMarksOperations {
static int counter = 0;
List<Integer> numbers = new ArrayList<Integer>();

public StudentMarksOperations(){
}

private void findPassedStudents(){
for(Integer n: numbers){
if(n >= 50){
counter = counter + 1;
}
}
}

private  void  getTheNumbersFromFile() throws IOException{
File file = new File("D:\\MarksList.txt");
BufferedReader bufferedReader = null;
FileReader fileReader = null;
try {
int number = 0;
fileReader = new FileReader(file);
bufferedReader= new BufferedReader(fileReader);
while((number = bufferedReader.read()) != -1){
numbers.add(number);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if(bufferedReader != null){
bufferedReader.close();
}
if(fileReader != null){
fileReader.close();
}
}

}

private double findThePassPercentage(){

return (counter * 100 )/numbers.size();
}

private int findTheTotalNumberOfMarks(){
int sum = 0;
for(Integer n: numbers){
sum = sum + n;
}
return sum;
}

public static void main(String[] args) throws IOException {
StudentMarksOperations studentMarksOperations = new StudentMarksOperations();
studentMarksOperations.getTheNumbersFromFile();;
studentMarksOperations.findPassedStudents();
System.out.println("the count of passed students is: "+counter);
System.out.println("the percentage of pass rate is: " + studentMarksOperations.findThePassPercentage());
System.out.println("the total number of marks is: " + studentMarksOperations.findTheTotalNumberOfMarks());
}
}


Output:

the count of passed students is: 9
the percentage of pass rate is: 64.0
the total number of marks is: 701

No comments:

Post a Comment

Program for primality test in JAVA

Problem: ============= Program for primality test in JAVA What is primality test? A primality test is an algorithm for determining wh...