Question

8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems...

8.15 *zyLab: Method Library (Required & Human Graded)
This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards CS 200 Style Guide. This project will be human graded.

This class contains a set of methods. The main method contains some examples of using the methods. Figure out what each method does and style and document it appropriately. The display method is done for you and doesn't need any changes. The methods to improve are named first, second, third, and fourth.
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;
}
}

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
I need to get the Min and Max value of an array when a user inputs...
I need to get the Min and Max value of an array when a user inputs values into the array. problem is, my Max value is right but my min value is ALWAYS 0. how do i fix this? any help please!!! _________________________________________________________________________________________________ import java.util.Scanner; public class ArrayMenu{ static int count; static Scanner kb = new Scanner(System.in);             public static void main(){ int item=0; int[] numArray=new int[100]; count=0;       while (item !=8){ menu(); item = kb.nextInt();...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...