• API
• FAQ
• Tools
• Archive
SHARE
TWEET # a1 a guest Feb 28th, 2020 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /** Challenge Activity 1 - Min and Max
2.  *
3.  * One common programming activity is to find the minimum and maximum values
4.  * within a list. In this challenge activity we will do just that. It will also
5.  * demonstrate how arrays and for loops compliment each other nicely.
6.  *
7.  * First, execute the main() method as is so you can understand how the for loop
8.  * works with the array.  If you must, set a breakpoint and step through the code.
9.  *
10.  * Notice the min and max values are not correct. That's where you come in your
11.  * job is to write these methods. Regardless of min or max, your approach should
12.  * be the same: (Here's the pseudocode for min)
13.  *
14.  * set min to the value of the first element in the array
15.  * for each element in the array
16.  *  if the current element is less than min
17.  *      set min to the current element
18.  * end for
19.  * return min
20.  */
21. package minandmax;
22.
23. import java.util.Scanner;
24.
25. public class MinAndMax {
26.
27.     public static void main(String[] args) {
28.         Scanner input = new Scanner(System.in);
29.         int[] array = new int;
30.
31.         // Read inputs into the array
32.         System.out.println("Enter 10 Integers.");
33.         for(int i=0;i<array.length;i++) {
34.             System.out.printf("Enter Integer %d ==>",i+1);
35.             array[i] = input.nextInt();
36.         }
37.         // Print out the array
38.         System.out.print("You Entered :");
39.         for(int i=0;i<array.length;i++) {
40.             System.out.printf("%d ", array[i]);
41.         }
42.         System.out.println();
43.         // find the min / max and print
44.         System.out.printf("Min = %d\n",getMin(array));
45.         System.out.printf("Max = %d\n",getMax(array));
46.
47.     }
48.
49.     /**
50.      * returns the smallest value in the array
51.      * @param array array of integer
52.      * @return integer representing the smallest
53.      */
54.     public static int getMin(int[] array) {
55.         //TODO: write code here
56.         //find the minimum number
57.         int a1 = array;
58.         for (int i : array) {
59.             a1 = i < a1 ? i: a1;
60.         }
61.         return a1;
62.         }
63.
64.
65.     /**
66.      * returns the largest value in the array
67.      * @param array array of integer
68.      * @return integer representing the largest
69.      */
70.     public static int getMax(int[] array) {
71.         //TODO: write code here
72.         int a2 = array;
73.         for (int i : array) {
74.             a2 = i > a2 ? i: a2; //finds the maximum number
75.         }
76.         return a2;
77. }
78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top