Advertisement
thotfrnk

quiz3.java

Jan 3rd, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. import java.util.Scanner; //importing scanner
  2. public class itec133_quiz3 { //program begins
  3.     public static void main(String[] args) { //main method begins
  4.  
  5.         Scanner input = new Scanner (System.in); //scanner declaration
  6.  
  7.         double [] num = new double [8]; //double array declaration
  8.  
  9.         for(int i=0; i<num.length; i++) { //for loop being used to populate the array num
  10.             System.out.println("Enter a decimal number: ");
  11.             num[i] = input.nextDouble();
  12.         } //for loop ends
  13.  
  14.         //output statement showing the smallest value of the array
  15.         System.out.println("The smallest number of the array is: " +getSmallest(num));
  16.  
  17.         double marks; //variable declaration
  18.  
  19.         //input statement for the user to enter the student's mark
  20.         System.out.println("Please enter a mark from 0 - 100: ");
  21.         marks = input.nextDouble();
  22.  
  23.         System.out.println(+getGrade(marks));
  24.     } //main method ends
  25.  
  26.     //method called getSmallest, takes on the argument which is an array. this method sorts the array in ascending order and returns the smallest value ONLY of the array.
  27.     public static double getSmallest(double [] array) {
  28.         double smallest = 0; //variable declaration
  29.  
  30.         //for loop 'j' used to go through the array.length elements
  31.         for(int j=0; j<array.length; j++) {
  32.             //for loop 'k' used to sort the array in ascending order
  33.             for(int k=j+1; k<array.length; k++) {
  34.                 //if statement to sort the array
  35.                 if (array[j] < array[k]) {
  36.                     smallest = array[j]; //stores the first element in smallest
  37.                     array[j] = array[k]; //replaces array[j] with array[k]
  38.                     array[k] = smallest; //puts smallest in array[k]
  39.                 } //if statement ends
  40.             } //for loop 'k' ends
  41.         } //for loop 'j' ends
  42.         return smallest; //returns the smallest value when method is called
  43.     } //method getSmallest ends
  44.  
  45.     //method called getGrade, takes on the double argument mark. this method goes through a series of if statements to assign and return a letter grade to the mark value.
  46.     public static double getGrade(double mark) {
  47.         //if statements to determine the letter grade the mark value should be assigned to
  48.         if(mark >= 90 && mark <= 100) {
  49.             System.out.println("A");
  50.         }
  51.         else
  52.         if(mark >= 85 && mark <= 89) {
  53.             System.out.println("B+");
  54.         }
  55.         else
  56.         if(mark >= 80 && mark <= 85) {
  57.             System.out.println("B");
  58.         }
  59.         else
  60.         if(mark >= 75 && mark <= 80) {
  61.             System.out.println("C+");
  62.         }
  63.         else
  64.         if(mark >= 70 && mark <= 75) {
  65.             System.out.println("C");
  66.         }
  67.         else
  68.         if(mark >= 65 && mark <= 70) {
  69.             System.out.println("D+");
  70.         }
  71.         else
  72.         if(mark >= 60 && mark <= 65) {
  73.             System.out.println("D");
  74.         }
  75.         else
  76.         if(mark < 60) {
  77.             System.out.println("F");
  78.         }
  79.  
  80.         return mark; //returns the letter grade when method is called
  81.     } //method getGrade ends
  82. } //program ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement