I would make my changes bold and italic for better understanding. Please leave an upvote if you like this answer. Thank you.
Code:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
System.out.println("MethodLibrary");
System.out.println("Example use of methods");
int[] smallNums1 = {
2,
1,
7,
4,
3
};
display("display:", smallNums1);
char[] characters = {
'a',
'b',
'c',
'd',
'e',
'f'
};
first(characters, 2);
System.out.print("after first: ");
System.out.println(characters);
int[] bigNums = {
1234,
20001,
3764,
20947,
9099
};
System.out.println("after second: " + second(bigNums));
int[] smallNums2 = {
8,
7,
3,
4,
5,
0,
1
};
third(smallNums2, true);
System.out.println("after third: " +
Arrays.toString(smallNums2));
Scanner i = new Scanner("-5 0 error 25 happy 20 14 end");
int r = fourth(i, "Enter a number between 1 and 20, inclusive", 1,
20);
}
/**
* This method prints the description followed by the contents of
list.
* The list begins with '[', ends with ']' and each element is
separated
* with ', '.
* Example: display( "a description", new int[]{1,2,3})
* a description [1, 2, 3]
* @param description The text printed out before the list of
integers.
* @param list The array of integers to be printed.
*/
public static void display(String description, int[] list) {
System.out.print(description);
System.out.print(" [");
for (int i = 0; i < list.length; i++) {
if (i > 0) {
System.out.print(", ");
}
System.out.print(list[i]);
}
System.out.println("]");
}
/**
* This method takes a string and rotates it a certain number of
times.
* Example first(abcdef,2)
* The string abcdef would become efabcd
* @param The string in which the characters are to be
rotated.
* @param The number of times for which the characters would be
rotated.
*/
public static void first(char[] r, int b) {
for (int i = 0; i < b; i++) {
char s = r[r.length - 1];
for (int j = r.length - 1; j > 0; j--) {
r[j] = r[j - 1];
}
r[0] = s;
}
}
/**
* This method returns the largest element in a given
list.
* Example second(new int[]{2,3,4,1,55})
* 55
* @param The list from which we want to get the lagrest
element.
*/
public static int second(int[] z) {
int g;
g = z[0];
for (int i = 0; i < z.length; i++) {
if (z[i] > g) {
g = z[i];
}
}
return g;
}
/**
* This method take a list as an argument
* and sorts it in ascending or descending order on the basis a
boolean variable.
* Example third(new int[]{3,1,2,5,4},true)
* [1,2,3,4,5]
* Example third(new int[]{3,1,2,5,4},false)
* [5,4,3,2,1]
* @param the integer array method wants to sort
* @param boolean value to decide the order of sorting.
*/
public static void third(int[] t, boolean stl) {
int m;
for (int i = 0; i < t.length; i++) {
for (int j = i + 1; j < t.length; j++) {
if (stl) {
if (t[i] > t[j]) {
m = t[i];
t[i] = t[j];
t[j] = m;
}
} else {
if (t[i] < t[j]) {
m = t[i];
t[i] = t[j];
t[j] = m;
}
}
}
}
}
/**
* This method take a list of inputs and prints out a error
message
* if any integer in our input is not between the given two
numbers.
* It also prints out required input at the start.
* Example fourth(new Scanner("-5 0 error 25 happy 20 14
end"),
* "Enter a number between 1 and 20, inclusive", 1, 20);
* Enter a number between 1 and 20, inclusive
* Value must be between 1 and 20.
* Value must be between 1 and 20.
* Value must be between 1 and 20.
* @param input values
* @param String to print at beginning
* @param minimum value
* @param maximum value
*/
public static int fourth(Scanner a, String b, int c, int d) {
System.out.println(b);
boolean v = false;
int r = 0;
do {
if (a.hasNextInt()) {
r = a.nextInt();
if (r >= c && r <= d) {
v = true;
} else {
System.out.println("Value must be between " + c + " and " + d +
".");
}
} else {
a.next();
}
} while (!v);
return r;
}
}
Code screenshot:
Solution ends.
Please comment and let me know if you have any further doubts. Please upvote this answer if you like it.
Thank you.
Have a good day.
Get Answers For Free
Most questions answered within 1 hours.