Question 2
A. A module is required to enable lecturers to easily identify the highest and lowest performing students in their courses. As the lead programmer, write a java program to achieve this. Your program should have the following features:
i. The program should make use of a sentinel to control the number of grades entered.
ii. Total Number of grades entered
iii. The average of the grades
iv. Number of students that achieved a grade of 80 and above
v. Number of students that achieved a grade of 50 and below
[15 marks]
B. An algorithm is a procedure for solving a problem in terms of the actions to execute and the order in which these actions execute. Write an algorithm for the implementation of the sentinel control in Question A Sub Question I above. [5 marks]
A, B.
import java.util.Scanner;
class Grade_record{
private String courseName;
// constructor
public Grade_record(String name){
courseName = name;
courseName = name;
// setters
public void setCourseName(String name){
courseName = name;
}
//getters
public String getCourseName(){
return courseName;
}
// determine class average based on 10 user-entered grades
public void determineClassAverage(){
Scanner sc = new Scanner(System.in);
nt total=0, gradeCounter=0, grade, above_eighty=0, below_fifty=0;
double average;
System.out.printf("Enter grade 1 or -1 to quit: ");
grade = sc.nextInt();
while(grade != -1)
{
if(grade >= 80) above_eighty++;
if(grade <= 50) below_fifty++;
total += grade;
gradeCounter++;
System.out.printf("Enter grade %d or -1 to quit: ", gradeCounter+1);
grade = sc.nextInt();
}
if(gradeCounter != 0){
average = (double)total / gradeCounter;
//display grades average
System.out.printf("\nTotal of the %d grades: %d\n", gradeCounter, total);
System.out.printf("Class average: %.2f\n", average);
System.out.printf("\nNumber of students below or equal to 50: %d", below_fifty);
System.out.printf("\nNumber of students above or equal to 80: %d", above_eighty);
}
}
}
public class My_class{
public static void main(String[] args){ Grade_record myGradeBook = new Grade_record("CS101 Introduction to Java Programming");
myGradeBook.determineClassAverage();
}
}
C. Linear search, to search for an element can be inefficient for a very large dataset, as it is an O(n) algorithm. But, we can not search for an element in less than O(n) unless the array given to us is sorted. If the given array is sorted, we can apply the binary search algorithm to search for an element in O(log n), because it gives us a pattern by which we can reduce our search space in half at every step.
To search the maximum element in an array:
case 1: if the array is sorted, we can give the maximum element in O(1), because the maximum is the last element (if sorted in ascending) or the first element (if sorted in descending).
case 2: if the array is unsorted, we will need to search the whole array to find the maximum element, so will require O(n).
Comments
Leave a comment