Advertisement
TheFastFish

my midterm

Jan 22nd, 2016
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Midterm {
  4.    
  5.     //a global scanner for you
  6.     static Scanner input;
  7.    
  8.     //sample test class
  9.     public static void main(String[] args) {
  10.         input = new Scanner(System.in);
  11.        
  12.         compareNumericalInput();
  13.         findAverage();
  14.         System.out.println(makeMeAllCaps("i used to have small letters"));
  15.         watchMeLoop();
  16.         System.out.printf("%b\n%b\n", isEven(2), isEven(3));
  17.         yayForArrays();
  18.        
  19.         input.close();
  20.     }
  21.    
  22.     //prints information about the two given numbers
  23.     public static void compareNumericalInput() {
  24.         int num1, num2;
  25.        
  26.         //ask user for numbers
  27.         System.out.print("Enter two integers: ");
  28.         num1 = input.nextInt();
  29.         num2 = input.nextInt();
  30.        
  31.         //check which is greater and by how much
  32.         if(num1 > num2)
  33.             System.out.printf("%d is larger than %d by %d.\n", num1, num2, num1 - num2);
  34.         else if (num1 < num2)
  35.             System.out.printf("%d is larger than %d by %d.\n", num2, num1, num2 - num1);
  36.         else
  37.             System.out.printf("%d and %d are equal.\n", num1, num2);
  38.        
  39.         //test if num1 is even
  40.         if(isEven(num1))
  41.             System.out.printf("%d is even.\n", num1);
  42.         else
  43.             System.out.printf("%d is odd.\n", num1);
  44.        
  45.         //test if num2 is even
  46.         if(isEven(num2))
  47.             System.out.printf("%d is even.\n", num2);
  48.         else
  49.             System.out.printf("%d is odd.\n", num2);
  50.        
  51.     }
  52.    
  53.     //generate two random ints and take average
  54.     public static void findAverage() {
  55.         int rand1 = (int)(Math.random() * 98.0) + 1;
  56.         int rand2 = (int)(Math.random() * 98.0) + 1;
  57.        
  58.         System.out.printf("The average of %d and %d is %f.\n", rand1, rand2, (double)(rand1 + rand2) / 2.0);
  59.     }
  60.    
  61.     //turn String into all uppercase
  62.     public static String makeMeAllCaps(String str) {
  63.         return str.toUpperCase();
  64.     }
  65.    
  66.     //run some loops
  67.     public static void watchMeLoop() {
  68.         int i;
  69.         int num;
  70.        
  71.         //do findAverage 50 times, with a for loop
  72.         for(i = 0; i < 50; i++) {
  73.             findAverage();
  74.         }
  75.        
  76.         //print 50 odd numbers, with a while loop
  77.         i = 0;
  78.         while(i < 50) {
  79.             num = 0;
  80.             while(isEven(num))
  81.                 num = (int)(Math.random() * 10);
  82.            
  83.             System.out.printf("%d\n", num);
  84.             i++;
  85.         }
  86.     }
  87.    
  88.     //test if an int is even
  89.     public static boolean isEven(int number) {
  90.         return number % 2 == 0;
  91.     }
  92.    
  93.     //do array stuff
  94.     public static void yayForArrays() {
  95.         int i;
  96.         int sum = 0;
  97.         int evencount = 0;
  98.         int maximum = Integer.MIN_VALUE;
  99.         int minimum = Integer.MAX_VALUE;
  100.         int[] array = new int[50];
  101.        
  102.         //fill with random ints
  103.         for(i = 0; i < array.length; i++)
  104.             array[i] = (int)(Math.random() * 100);
  105.        
  106.         //array tests
  107.         for(i = 0; i < array.length; i++) {
  108.             //evenness
  109.             if(isEven(array[i]))
  110.                 evencount++;
  111.            
  112.             //maximum
  113.             if(array[i] > maximum)
  114.                 maximum = array[i];
  115.            
  116.             //minimum
  117.             if(array[i] < minimum)
  118.                 minimum = array[i];
  119.            
  120.             //get sum
  121.             sum += array[i];
  122.         }
  123.        
  124.         //print info
  125.         System.out.printf("Minimum: %2d\nMaximum: %2d\nAverage: %2f\nAmount Even: %d", minimum, maximum, (double)sum / (double)array.length, evencount);
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement