This question is 3 parts, and I have no clue where to even begin. Here is some m
ID: 3904509 • Letter: T
Question
This question is 3 parts, and I have no clue where to even begin. Here is some more information:
Here are the notes referred to in part 1:
If you figure out each part, it would be very helpful if you could insert some comments into your code, so that I can try to understand what you're doing and why. Also, I don't know what the output should look like, so if you could upload screenshots of the output for each, I would appreciate it so so much! Thank you! Also, I have posted this before and it was only answered briefly for one part in the wrong language- I need this to be done in JAVA. My biggest concerns are parts 1 and 2. Thanks :)
1. Implement a solution to the Missionaries and Cannibals problem as described in the notes Your solution must not only find if a solution exists, but if a solution is found, output it using an ArrayList the current element. For instance, the array {1, 2, 4, 8. 16, 32. 64, 128, is increasingly additive because each value > sum of all previous values (8> 1+2+4, 64> 1 +2+4+8 16+32). The method should return false if any element is not >the sum of previous elements and true if the end of the array was reached without violating the increasingly additive property b. Is array 1 strictly greater than array 2 - recursively compare each element of array 1 to every element of array 2 to make the value in array 1 > all elements of array 2. If any element violates this, return false. If you have gone through all of both arrays without violating this, return true Let's define a set as a list of values that has no duplicates such that the values are in ascending order. Write a recursive method that receives an array and determines if it is a valid set or not. If the recursive method either finds that a value is out of order or is duplicated, it is not a set and returns false. If the method is able to work through the entire array without finding a duplicate or out of order values, it returns c. true d. Write a method which is given two arrays which are sets as per the definition in part c (no duplicates, all items in ascending order), the method will return the number of items found in common in both sets. For instance, {1, 3. 6, 7; and , 2, 4, 5, 6,7 would have a value of 3 (1, 6, 7). Assume for this method that the two parameters are sets; the method does not have to test this, it only returns the values that both sets have in common. Implement a program to generate a figure recursively. Select any one of the figures on the next page to attempt, or try to do the RecursiveTree from figure 18.20 on page 750 of the textbook. Your image does not have to precisely match the one you select but should come reasonably close. Your drawing should only be in single color, don't worry about changing colors as shown in the pentagon image. Use setFill(Color.TRANSPARENT) to make the internal portion transparent (if desired). You may do a different image if you do not like any of those here but if you select a different image to draw, explain why you selected it in your comments 3.Explanation / Answer
Hello there, I should say, you can’t get answers for all questions in a single thread, as it is not possible to complete everything within 2 hours. So I kindly suggest you to split the parts into different questions. Here is the completed code for part 2 ONLY which contain 4 recursive methods for solving 4 sub problems. Comments are included, go through it, learn how things work and let me know if you have any doubts. Also when you post the remaining parts as different questions, you should post the sample figures for part 3. Thanks
// RecursiveMethods.java (contain 4 recursive methods for part 2)
public class RecursiveMethods {
/**
* method to check if all elements in array are additively increasing
* (greater than all previous values combined)
*
* @param array
* - array to be tested
* @param sum
* - sum of all previous values
* @param index
* - starting index
* @return true if additively increasing, else false
*/
public static boolean isAdditivelyIncreasing(int[] array, int sum, int index) {
if (index == array.length) {
// reached the end of array, so the array is additively increasing
return true;
}
if (index == 0) {
/**
* first item, so setting sum as the current value, and moving to
* next item
*/
sum = array[0];
return isAdditivelyIncreasing(array, sum, index + 1);
} else {
// subsequent items
/**
* checking if current item is greater than sum of all previous
* values
*/
if (array[index] > sum) {
// so far, additively increasing, moving to next index
return isAdditivelyIncreasing(array, sum + array[index],
index + 1);
} else {
// mismatch found, not additively increasing
return false;
}
}
}
/**
* method to check if all values of array1 is greater than all values of
* array2
*
* @param array1
* - first array
* @param array2
* - second array
* @param index1
* - first array index
* @param index2
* - second array index
* @return true if array1 is strictly greater than array2
*/
public static boolean isStrictlyGreater(int[] array1, int[] array2,
int index1, int index2) {
if (index1 == array1.length) {
/**
* reached the end of array1, so the array1 is strictly greater than
* array2
*/
return true;
}
if (index2 == array2.length) {
/**
* reached the end of array2, so incrementing array1 index and
* resetting array2 index to 0
*/
index2 = 0;
index1++;
return isStrictlyGreater(array1, array2, index1, index2);
}
/**
* checking if current value in array1 is greater than current value in
* array2
*/
if (array1[index1] > array2[index2]) {
// moving to next value in array2
return isStrictlyGreater(array1, array2, index1, index2 + 1);
}
// mismatch found, returning false
return false;
}
/**
* method to check if array is a valid set containing no duplicates and
* elements arranged in ascending order
*
* @param array
* - array to be checked
* @param previousValue
* - previous value to compare
* @param index
* - starting index
* @return true if set, else false
*/
public static boolean isValidSet(int[] array, int previousValue, int index) {
if (index == array.length) {
// end of array without violating the condition, so array is set
return true;
}
if (index == 0) {
// first item, so setting this item as previous value
previousValue = array[0];
// moving to next item
return isValidSet(array, previousValue, index + 1);
}
// subsequent items
// checking if current item is greater than previous item
if (array[index] > previousValue) {
// setting current value as previous item and moving to next itm
previousValue = array[index];
return isValidSet(array, previousValue, index + 1);
}
// mismatch found, not a set
return false;
}
/**
* method to return the number of common elements in array1 and array2
*
* @param array1
* - set 1
* @param array2
* - set 2
* @param index1
* - first array index
* @param index2
* - second array index
* @return the count of common items
*/
public static int numberOfCommonItems(int[] array1, int[] array2,
int index1, int index2) {
/**
* using similar technique used in isStrictlyGreater() method, so that
* the method will work easily even if array1 and array2 are not sets or
* not in ascending order
*/
if (index1 == array1.length) {
/**
* checked all values in array1 and array2, returning 0 to end the
* recursion
*/
return 0;
}
if (index2 == array2.length) {
/**
* end of array2, resetting array2 index to 0 and moving to next
* index of array1
*/
index2 = 0;
index1++;
return numberOfCommonItems(array1, array2, index1, index2);
}
//checking if current value in array1 is equal to current value in array2
if (array1[index1] == array2[index2]) {
//adding 1 to the return value, and moving to next item in array2
return 1 + numberOfCommonItems(array1, array2, index1, index2 + 1);
} else {
//not same, moving to next item in array2
return numberOfCommonItems(array1, array2, index1, index2 + 1);
}
}
public static void main(String[] args) {
/**
* Testing isAdditivelyIncreasing()
*/
int array[] = new int[] { 1, 2, 4, 8, 16, 32, 64, 128 };
// should print true
System.out.println(isAdditivelyIncreasing(array, 0, 0));
array = new int[] { 1, 2, 4, 3 };
// should print false
System.out.println(isAdditivelyIncreasing(array, 0, 0));
/**
* Testing isStrictlyGreater()
*/
int array1[] = new int[] { 10, 20, 30 };
int array2[] = new int[] { 1, 2, 3, 5, 7, 9 };
// should print true
System.out.println(isStrictlyGreater(array1, array2, 0, 0));
// should print false
System.out.println(isStrictlyGreater(array2, array1, 0, 0));
/**
* Testing isValidSet()
*/
array = new int[] { 1, 2, 3, 5, 7, 8, 11, 24 };
// should print true
System.out.println(isValidSet(array, 0, 0));
array = new int[] { 1, 2, 2, 3, 5, 7, 8, 11, 24 };
// should print false
System.out.println(isValidSet(array, 0, 0));
/**
* Testing numberOfCommonItems()
*/
array1 = new int[] { 1, 3, 6, 7 };
array2 = new int[] { 1, 2, 4, 5, 6, 7 };
// should print 3
System.out.println(numberOfCommonItems(array1, array2, 0, 0));
}
}
/*OUTPUT*/
true
false
true
false
true
false
3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.