Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Midterm {
- //a global scanner for you
- static Scanner input;
- //sample test class
- public static void main(String[] args) {
- input = new Scanner(System.in);
- compareNumericalInput();
- findAverage();
- System.out.println(makeMeAllCaps("i used to have small letters"));
- watchMeLoop();
- System.out.printf("%b\n%b\n", isEven(2), isEven(3));
- yayForArrays();
- input.close();
- }
- //prints information about the two given numbers
- public static void compareNumericalInput() {
- int num1, num2;
- //ask user for numbers
- System.out.print("Enter two integers: ");
- num1 = input.nextInt();
- num2 = input.nextInt();
- //check which is greater and by how much
- if(num1 > num2)
- System.out.printf("%d is larger than %d by %d.\n", num1, num2, num1 - num2);
- else if (num1 < num2)
- System.out.printf("%d is larger than %d by %d.\n", num2, num1, num2 - num1);
- else
- System.out.printf("%d and %d are equal.\n", num1, num2);
- //test if num1 is even
- if(isEven(num1))
- System.out.printf("%d is even.\n", num1);
- else
- System.out.printf("%d is odd.\n", num1);
- //test if num2 is even
- if(isEven(num2))
- System.out.printf("%d is even.\n", num2);
- else
- System.out.printf("%d is odd.\n", num2);
- }
- //generate two random ints and take average
- public static void findAverage() {
- int rand1 = (int)(Math.random() * 98.0) + 1;
- int rand2 = (int)(Math.random() * 98.0) + 1;
- System.out.printf("The average of %d and %d is %f.\n", rand1, rand2, (double)(rand1 + rand2) / 2.0);
- }
- //turn String into all uppercase
- public static String makeMeAllCaps(String str) {
- return str.toUpperCase();
- }
- //run some loops
- public static void watchMeLoop() {
- int i;
- int num;
- //do findAverage 50 times, with a for loop
- for(i = 0; i < 50; i++) {
- findAverage();
- }
- //print 50 odd numbers, with a while loop
- i = 0;
- while(i < 50) {
- num = 0;
- while(isEven(num))
- num = (int)(Math.random() * 10);
- System.out.printf("%d\n", num);
- i++;
- }
- }
- //test if an int is even
- public static boolean isEven(int number) {
- return number % 2 == 0;
- }
- //do array stuff
- public static void yayForArrays() {
- int i;
- int sum = 0;
- int evencount = 0;
- int maximum = Integer.MIN_VALUE;
- int minimum = Integer.MAX_VALUE;
- int[] array = new int[50];
- //fill with random ints
- for(i = 0; i < array.length; i++)
- array[i] = (int)(Math.random() * 100);
- //array tests
- for(i = 0; i < array.length; i++) {
- //evenness
- if(isEven(array[i]))
- evencount++;
- //maximum
- if(array[i] > maximum)
- maximum = array[i];
- //minimum
- if(array[i] < minimum)
- minimum = array[i];
- //get sum
- sum += array[i];
- }
- //print info
- System.out.printf("Minimum: %2d\nMaximum: %2d\nAverage: %2f\nAmount Even: %d", minimum, maximum, (double)sum / (double)array.length, evencount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement