The purpose of this first assignment is to give you experience with if and while
ID: 3830681 • Letter: T
Question
The purpose of this first assignment is to give you experience with if and while constructs in Pep/9.
Input: the scores a student achieves on each of 10 assignments. Each score is in the range 0..100.
The student passes the course if
The average of the 10 scores is > 50
Or the average of the 9 highest scores is >55
Or the average of the 8 highest scores is > 60
Write a Pep/9 program that inputs the scores and outputs a message indicating whether the students
passed. Note that it is not necessary to use arrays to solve this problem.
Pep 9 language
Explanation / Answer
import java.util.Scanner;
public class StudentResult {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
double a[] = new double[10];
int i = 0;
String result = "";
double tempVar = 0;
double sumOfTen = 0;
double sumOfHighestNine = 0;
double sumOfHighestEight = 0;
System.out.println("Enter the marks of 10 assignments: ");
// Taking input from the user
while (i < 10) {
a[i] = sc.nextDouble();
if (a[i] < 0 || a[i] > 100) {
System.out.println("Please enter marks in the range 0...100");
System.exit(0);
}
i++;
}
// sorting the array in ascending order
for (int index = 0; index < a.length; index++) {
for (int j = index + 1; j < a.length; j++) {
if (a[index] > a[j]) {
tempVar = a[index];
a[index] = a[j];
a[j] = tempVar;
}
}
}
// Calculating the sum of all ten assignments marks, Highest nine
// assignment marks and Eight Highest marks
i = 0;
while (i < a.length) {
System.out.println(a[i]);
sumOfTen = sumOfTen + a[i];
if (i > 0) {
sumOfHighestNine = sumOfHighestNine + a[i];
}
if (i > 1) {
sumOfHighestEight = sumOfHighestEight + a[i];
}
i++;
}
//Implementing the business logic
if ((sumOfTen / 10) > 50) {
result = "Passed";
} else if ((sumOfHighestNine / 9) > 55) {
result = "Passed";
} else if ((sumOfHighestEight / 8) > 60) {
result = "Passed";
} else {
result = "Not Passed";
}
System.out.println("Result Of the Student " + result);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.