Java programming homework: Method #1: Write a method named swapPairs that accepts an array of strings as a parameter and switches the order of values in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. For example, if the array initially stores these values: String[] a = {"four", "score", "and", "seven", "years", "ago"}; swapPairs(a); Your method should switch the first pair ("four", "score"), the second pair ("and", "seven") and the third pair ("years", "ago"), to yield this array: {"score", "four", "seven", "and", "ago", "years"} If there are an odd number of values, the final element is not moved. For example, if the original list had been: {"to", "be", "or", "not", "to", "be", "hamlet"} It would again switch pairs of values, but the final value ("hamlet") would not be moved, yielding this list: {"be", "to", "not", "or", "be", "to", "hamlet"} Method #2: Write a method named longestSortedSequence that accepts an array of integers as a parameter and that returns the length of the longest sorted (nondecreasing) sequence of integers in the array. For example, if a variable named array stores the following values: int[] array = {3, 8, 10, 1, 9, 14, -3, 0, 14, 207, 56, 98, 12}; then the call of longestSortedSequence(array) should return 4 because the longest sorted sequence in the array has four values in it (the sequence -3, 0, 14, 207). Notice that sorted means nondecreasing, which means that the sequence could contain duplicates. For example, if the array stores the following values: int[] array2 = {17, 42, 3, 5, 5, 5, 8, 2, 4, 6, 1, 19} Then the method would return 5 for the length of the longest sequence (the sequence 3, 5, 5, 5, 8). Your method should return 0 if passed an empty array. - Your code should have two classes ( Drive , Methods ) - Methods class should include longestSortedSequence(array) and swapPairs(array) methods - Drive class should have : 1. Main method 2. One object of class Methods ( object_one ) 3. Test longestSortedSequence(array) and swapPairs(array) method by object_one object - longestSortedSequence(array) and swapPairs(array) methods can accept an array with any different length - you can deliver your code by moodle assignment #1 - your code should be fully commented - copy the classes ( Drive , Methods ) from src file from your project file then submit it Notes: - you should be ready to discuss your code by the first lecture after assignment due date - deadline for submitting your code is 03/03/2021 – 11:59pm - all copies from your classmates or internet will be evaluated by ZER
Java programming homework:
Method #1:
Write a method named swapPairs that accepts an array of strings as a parameter and switches the order of values in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. For example, if the array initially stores these values:
String[] a = {"four", "score", "and", "seven", "years", "ago"}; swapPairs(a);
Your method should switch the first pair ("four", "score"), the second pair ("and", "seven") and the third pair ("years", "ago"), to yield this array:
{"score", "four", "seven", "and", "ago", "years"}
If there are an odd number of values, the final element is not moved. For example, if the original list had been:
{"to", "be", "or", "not", "to", "be", "hamlet"}
It would again switch pairs of values, but the final value ("hamlet") would not be moved, yielding this list:
{"be", "to", "not", "or", "be", "to", "hamlet"}
Method #2:
Write a method named longestSortedSequence that accepts an array of integers as a parameter and that returns the length of the longest sorted (nondecreasing) sequence of integers in the array. For example, if a variable named array stores the following values:
int[] array = {3, 8, 10, 1, 9, 14, -3, 0, 14, 207, 56, 98, 12};
then the call of longestSortedSequence(array) should return 4 because the longest sorted sequence in the array has four values in it (the sequence -3, 0, 14, 207). Notice that sorted means nondecreasing, which means that the sequence could contain duplicates. For example, if the array stores the following values:
int[] array2 = {17, 42, 3, 5, 5, 5, 8, 2, 4, 6, 1, 19}
Then the method would return 5 for the length of the longest sequence (the sequence 3, 5, 5, 5, 8). Your method should return 0 if passed an empty array.
- Your code should have two classes ( Drive , Methods )
- Methods class should include longestSortedSequence(array) and swapPairs(array) methods - Drive class should have :
1. Main method
2. One object of class Methods ( object_one )
3. Test longestSortedSequence(array) and swapPairs(array) method by object_one object
- longestSortedSequence(array) and swapPairs(array) methods can accept an array with any different length
- you can deliver your code by moodle assignment #1
- your code should be fully commented
- copy the classes ( Drive , Methods ) from src file from your project file then submit it
Notes:
- you should be ready to discuss your code by the first lecture after assignment due
date
- deadline for submitting your code is 03/03/2021 – 11:59pm
- all copies from your classmates or internet will be evaluated by ZERO
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images