Advertisement
Guest User

Question attempt

a guest
Jan 23rd, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. //Q3_reverseSortDemo
  2. public class Q3_ReverseSortDemo {
  3.     public static void main(String[] args){
  4.         char[] unorderedLetters;
  5.         unorderedLetters = new char[]{'b', 'm', 'z', 'a', 'u'};
  6.         reverseSort(unorderedLetters);
  7.         for (int i = 0 ; i < unorderedLetters.length; i++ )
  8.             System.out.print(unorderedLetters[i]);
  9.     }
  10.  
  11.     //method that sorts a char valuesay into its reverse alphabetical order
  12.     public static void reverseSort(char[] values){
  13.         //your code here
  14.         int n = values.length;  
  15.         char temp;  
  16.          for(int i=0; i < n; i++){  
  17.                  for(int j=1; j < (n-i); j++){  
  18.                           if(values[j-1] > values[j]){  
  19.                                  //swapping elements
  20.                                  temp = values[j-1];  
  21.                                  values[j-1] = values[j];  
  22.                                  values[j] = temp;  
  23.                          }  
  24.                          
  25.                  }  
  26.          }
  27.     }      
  28. }
  29. //Q6
  30. import java.util.Scanner;
  31. public class Q6{
  32.     public static void main(String[] args){
  33.  
  34.         //your code here
  35.     System.out.print("Input 10 students grades: ");
  36.     Scanner scanName = new Scanner(System.in);
  37.     double[] myDouble = scanName.nextDouble();
  38.     System.out.print(calculateAverage(myDouble));
  39.     System.out.print(calculateMedian(myDouble));
  40.     System.out.print(calculateNumberFailed(myDouble));
  41.     System.out.print(calculateNumberPassed(myDouble));
  42.  
  43.     }
  44.  
  45.     public static double calculateAverage(double[] notes){
  46.         //your code here
  47.         double result;
  48.         double total = 0;
  49.         for(int i = 0; i <= notes.length-1; i++){
  50.             total = total + notes[i];
  51.         }
  52.         result = total/notes.length;
  53.         return result;
  54.     }
  55.  
  56.     public static double calculateMedian(double[] notes){
  57.         //your code here
  58.         double median;
  59.         double medianPosition=notes.length/2;
  60.         median = notes[medianPosition];
  61.         return median;
  62.     }
  63.  
  64.     public static int calculateNumberFailed(double[] notes){
  65.         //your code here
  66.         double failed = 0;
  67.         for(int i = 0; i <= notes.length-1; i++){
  68.             if(notes[i]<=50){
  69.                 failed=failed+1;
  70.             }
  71.         return failed;
  72.         }
  73.     }
  74.  
  75.     public static int calculateNumberPassed(double[] notes){
  76.         //your code here
  77.         double passed = 0;
  78.         for(int i = 0; i <= notes.length-1; i++){
  79.             if(notes[i]>=50){
  80.                 passed=passed+1;
  81.             }
  82.         return passed;
  83.         }
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement