Wednesday 14 June 2017

Java Code to find the Student Pass count when the variable counter initialized to the total number of students n

Problem:

Given a set of n students examination marks (in the range 0 to 100) make a count of the number of students that passed the examination. A pass is awarded for all marks of 50 and above.

Condition is the variable counter should be initialized to the total number of students n.

Algorithm:

1. Prompt then read the number of marks to be processed.
2. Initialize count to the total number of students n.
3. While there are still marks to be processed repeatedly do
     a. read next mark
     b. if it is a pass (i.e >=50) then subtract one from count.
4. Write out total number of passes

Solution:

package com.myprograms;

import java.util.Scanner;

public class StudentPassCountWithCounterAsNValue {
int a[];
static Scanner s = null;
static int counter = 0;

public StudentPassCountWithCounterAsNValue(int n){
a = new int[n];
counter = n;
}

private void initializeMarks(){
for(int i = 0; i< a.length; i++){
System.out.println("enter the marks");
a[i] = s.nextInt();
}
}

private void findPassedStudents(){
for(int i = 0; i< a.length; i++){
if(a[i] >= 50){
counter = counter - 1;
}
}
}


public static void main(String[] args) {
s = new Scanner(System.in);
System.out.println("How many students marks you want to verify??");
StudentPassCountWithCounterAsNValue sp = new StudentPassCountWithCounterAsNValue(s.nextInt());
sp.initializeMarks();
sp.findPassedStudents();
System.out.println("the count of passed students is: "+counter);
}

}


Output:

How many students marks you want to verify??
5
enter the marks
12
enter the marks
56
enter the marks
99
enter the marks
45
enter the marks
6
the count of passed students is: 3


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...