boolean allAreEqual(int a, int b, int c);
boolean twoAreEqual(int a, int b, int c); // false if all three are equal
boolean noneAreEqual(int a, int b, int c);
boolean areAscending(int a, int b, int c); // true if a <= b <= c
boolean areDescending(int a, int b, int c); // true if a >= b >= c
boolean strictlyAscending(int a, int b, int c); // true if a < b < c
boolean strictlyDescending(int a, int b, int c); // true if a > b > c
boolean isNegative = x < 0;rather than:
boolean isNegative; if (x < 0) isNegtive = true; else isNegative = false;
Sample Test Run
nextLine for your input>. You
should also not be using an array.
Sample Test Run
For example if the file numbers.text contains:
3 1 2 3
5 12 14 6
4 0
10 1 2 3 4 5
6 7 8 9 10
1 17
2 90 80
the program should produce the following output:
The average of the 3 integers 1 2 3 is 2.0 The average of the 5 integers 12 14 6 4 0 is 7.2 The average of the 10 integers 1 2 3 4 5 6 7 8 9 10 is 5.5 The average of the 1 integers 17 is 17.0 The average of the 2 integers 90 80 is 85.0 5 sets of numbers processed
You may not use an array or any other data structure.
Sample Test Run
For example if the file numbers.text contains:
10 23 43 5 12 23 9 8 10 1 16 9execution of the program should look like:
int findLast(int val), that accepts the value to be searched, creates a Scanner
object for the file numbers.text and searches the file
Scanner).
main method, have a Scanner for the keyboard, prompt the user for values, call the
above method with the value read in, and use the returned value to determine what to print.
numbers.text contains a header value, followed by that many double values. Create an array of the
appropriate size, populate the array with the doubles, and print out various information about the array (see below).
Sample Test Run
For example if the file numbers.text contains:
8 12.3 2.5 9.4 3.14 22.15 17 54.3 7.6the program should produce the following output:
The array: {12.3, 2.5, 9.4, 3.14, 22.15, 17.0, 54.3, 7.6} contains 8 elements
The first element of the array is 12.3
The last element of the array is 7.6 and is at position 7
The middle element of the array is 3.14 and is at position 3
The largest element of the array is 54.3 and is at position 6
The smallest element of the array is 2.5 and is at position 1
Notes:
first,
last, or middle methods — — feel free to simply use the proper subscripts).
max method presented inthe lecture notes is useful, it is not enough; you are not just being
asked to print out the largest and smallest value, you are also required to print out their positions (i.e., indexes in the
array). You thus should consider writing a maxPos method that returns the index (rather than the actual value)
of the largest element in the array (same for minimu).
max method; this also emphasizes the distinction between the value of the element
and its index (position/location within the array).
roster.text, containing pairs of ids (integers) and gpa's (doubles), read the file into a pair of arrays, and perform two sorts on them, the first by name in
ascending order, the second by gpa in descending order. Print out the original and sorted arrays.
read method … accepts either the filename or a Scanner object created by main, together
with the arrays and the capacity. The method loads up the arrays returns the number of entries read in.
size). Since the two methods have the exact same signature,
they cannot be overload (i.e., use the exact same name) … I used the named sortById and sortByGPA
swap accepts an array and a pair of indexes and swaps the values at those two indices (look at Lecture 12 for details on swapping elements of an array).
You need two of these methods, one for swapping the elements of the id (int) array and the other for the gpa (double) array. Since these two methods will have different
signatures (i.e., their paraeter lists are not identcal), you can use the same name for both (I used swap).
print accepting bot arrays and the size and prints them out in the appropriate manner
swap code from Lecture 12
read methods from the notes or the solutions to the sample finals
sortBy and swap. Write one and copy to the other.
A word of caution, make sure you also understand the code you are copying; it's often too easy t copy without understanding the code, and that sort of defeats the purpose of doing these exercises.
For example if the file roster.text contains:
10050 3.1 10721 2.3 10010 3.7 30921 2.5 23462 4.0 12345 2.9the program should produce the following output:
Original data: 10050: 3.1 10721: 2.3 10010: 3.7 30921: 2.5 23462: 4.0 12345: 2.9 Sorted by Id: 10010: 3.7 10050: 3.1 10721: 2.3 12345: 2.9 23462: 4.0 30921: 2.5 Reverse Sorted by GPA: 23462: 4.0 10010: 3.7 10050: 3.1 12345: 2.9 30921: 2.5 10721: 2.3