In: Computer Science
This code works fine. In java, can you:
change the variable and method names to make logical sense
add method header comments to describe what the method does and what the params/returns are
add a class header comment to describe what the code does
import java.util.Arrays;
import java.util.Scanner;
public class MethodLibrary {
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("]");
}
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; }
}
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;
}
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;
}
}
}
}
}
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;
}
}
The requirement of the question was to make code more meaningful to read and assign proper method and variable names to make it more readable. The below code is well commented and names of variables and methods are meaningful and easy to understand
//Code starts here
import java.util.Arrays;
import java.util.Scanner;
//This class has methods for int and char. It has mainly 5 methods
: to display number list,move last elements from char list to
start, to sort number list, to find greatest number in list and to
check if elements in a list lie betweem a given range
public class MethodLibrary {
public static void main(String args[]) {
System.out.println("MethodLibrary");
System.out.println("Example use of methods");
//integer list of small numbers
int[] smallNumbers = {2,1,7,4,3};
//Call display method to print list of numbers
display("Display list: ", smallNumbers);
//list of characters
char[] chars = {'a','b','c','d','e','f'};
//Shift last x characters to the starting of array by passing
number of characters to be shifted in the second parameter of
shiftLastChars method
shiftLastChars(chars, 2);
//Display result returned by shiftLastChars method
System.out.print("After shifting: ");
System.out.println(chars);
//integer list of big numbers
int[] bigNumsList = {1234,20001,3764,20947,9099};
//Call findGreatest method to find the largest number in the
list
System.out.println("Largest number: " +
findGreatest(bigNumsList));
//integer list of numbers
int[] sortingList = {8,7,3,4,5,0,1};
//Call sort method to sort the given int array in ascending or
descending array based on second parameter true for ascending and
false for descending
sort(sortingList, true);
//Display number list after sorting
System.out.println("After sorting: " +
Arrays.toString(sortingList));
//Get list of values to test if those values lie between 1 and
20
Scanner in = new Scanner("-5 0 error 25 happy 20 14 end");
//Call checkRange method to check if values lie between 1 and
20
int result = checkRange(in, "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 shifts last chars to start of list.
* Move character one by one from last to start of list
* @param list - The given list of characters.
* @param nums - The numbers of characters to be moved from last
to
* first.
*/
public static void shiftLastChars(char[] list, int nums) {
for (int i = 0; i < nums; i++) {
//Last char to move
char last = list[list.length - 1];
for (int j = list.length - 1; j > 0; j--) {
list[j] = list[j - 1]; //Move all elements one step towards end and
vacate first place
}
list[0] = last; //Move last element to first position
}
}
/**
* This method finds the largest number in a list.
* Compare all numbers and update the greatest if number is
largest
* @param list - The given list of integers.
*/
public static int findGreatest(int[] list) {
int greatest;
greatest = list[0];//Initialise greatest with first value in
list
for (int i = 0; i < list.length; i++) { //Iterate over
list
if (list[i] > greatest) { //If current element in list >
greatest , update the greatest with current element value
greatest = list[i];
}
}
return greatest; //return greatest value
}
/**
* This method sorts the list in ascending or descending.
* Based on second parameter sort the list in increasing or
decreasing
* order.
* @param list - The given list of integers.
* @param increasing - Sort in increasing if true and
decreasing
* if false.
*/
public static void sort(int[] list, boolean increasing) {
int temp; //temp variable to temporary store values
for (int i = 0; i < list.length; i++) { //Iterate over
list/array
for (int j = i + 1; j < list.length; j++) {
if (increasing) {
if (list[i] > list[j]) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
} else {
if (list[i] < list[j]) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
}
/**
* This method checks if values are in a given range
* It has a list and it displays message if value is not in
range
* @param list - Scanner has a list of values
* @param description - It tells description of values to
enter
* @param start - initial value of range
* @param end - last value of range
*/
public static int checkRange(Scanner list, String description, int
start, int end) {
System.out.println(description); //Print description
boolean inRange = false; //True if value in range
int value = 0; //variable to store each value
do {
if (list.hasNextInt()) { //Till list is empty
value = list.nextInt(); //Store list element in value
if (value >= start && value <= end) { //If value in
range
inRange = true;
} else {
//Print message if value not in range
System.out.println("Value must be between " + start + " and " + end
+ ".");
}
} else {
list.next();
}
} while (!inRange); //Repeat until in range
return value; //return value
}
}
//Code ends here