Question: I need some help with a project I have. The subparts I'm struggling with my sameValues method, my append method, my merge and mergeSorted method,
I need some help with a project I have. The subparts I'm struggling with my sameValues method, my append method, my merge and mergeSorted method, my remove method, and my toString method. The biggest problem I have is how to get the solution of the method into either the class "Sequence" or to a string (in the case of toString) while the rest (merge) is a problem of figuring out the code. Can you help me? The code can't be too complicated because I am not too knowledgable in Java. Thanks!
import java.util.Arrays; import java.util.Scanner;
public class Sequence { Scanner keyboard = new Scanner(System.in); private int[] values; public Sequence(int size) { values = new int[size]; }
public void set(int[] v) { values = v; }
public void set(int i, int n) { values[i] = n; }
/** * Returns true if the current sequence and other sequence have the same * values in the same order Returns false otherwise. */ public boolean equals(Sequence other) { if (this.values.length == other.values.length) { for (int i = 0; i < this.values.length; i++) if (!(this.values[i] == other.values[i])) { return false; } return true; } return false; } /** * Returns true if the current sequence and other sequence have the same * values in some order, ignoring duplicates, return false otherwise. * * For example, [1 4 9 16 9 7 4 9 11] and [11 11 7 9 16 4 1] would * considered identical. */ public boolean sameValues(Sequence other) { int j = 0; for (int i = 0; i < this.values.length; i++) { //WRONG. for (j = 0; j < this.values.length; j++){ if (!(this.values[i] == other.values[j])){ return true; } } } return false; } /** * Returns true if the current sequence and the other sequence have the same * values in some order with the same multiplicities, return false otherwise * For example [1 4 9 16 9 7 4 9 11] is a permutation of [11 1 4 9 16 9 7 4 * 9] but [1 4 9 16 9 7 4 9 11] is NOT a permutation of [11 11 7 9 16 4 1 4 * 9] as 11 occurs once in the first sequence, but twice in the second * sequence. */ public boolean isPermutationOf(Sequence other) { Arrays.sort(this.values); Arrays.sort(other.values); if (this.values.length == other.values.length) { for (int i = 0; i < this.values.length; i++) if (!(this.values[i] == other.values[i])) { return false; } return true; } return false; }
/** * Returns a new Sequence object by appending the other sequence to the end * of the current sequence. * * For example if Sequence a is [1 4 9 16] and Sequence b is [9 7 4 9 11] * then a.append(b) would return [1 4 9 16 9 7 4 9 11] */ public Sequence append(Sequence other) {
int []d = new int[values.length+other.values.length]; for(int i = 0; i d[i]=this.values[i]; } int j=0; for(int i=this.values.length; i d[i]=other.values[j]; j++; } for(int i = 0; i System.out.print(d[i]+" "); } return null; }
/** * Returns a new Sequence by merging the current sequence and the other * sequence, alternating elements from both sequences. If one sequence is * shorter than the other, then alternate as long as you can and then append * the remaining elements from the longer sequence. For example a = [1 4 9 * 16] b = [9 7 4 9 11] then a.merge(b) would return [1 9 4 7 9 4 16 9 11] */ public Sequence merge(Sequence other) { return mergeArray; }
/** * Merges the current and other sequences and returns a new sorted sequence. * Example a=[1 4 9 16] and b=[9 7 4 9 11] a.mergeSorted(b) would return [1 * 4 4 7 9 9 9 11 16]. If the current or other sequence is not sorted, then * mergeSort should sort them before attempting the merge. */ public Sequence mergeSorted(Sequence other) { return other; }
/** * Returns a new Sequence after removing all occurrences of the integer n in * the current Sequence * * Example a=[1 4 9 16 4] a.remove(4) would return [1 9 16] * */ public Sequence remove(int n) { for (int i = 0; i < this.values.length; i++){ if (!(this.values[i] == n)){ System.out.print(this.values[i] + " "); //don't know how to deal with sequence as return type } } return null; }
/** * Inserts integer n into position pos of the current sequence. Example a=[1 * 4 9 16 4] a.insert(5, 3) would modify a to [1 4 9 5 16 4] */ public void insert(int number, int pos){ int[] newArray = new int[this.values.length+1]; for(int i = 0; i if(i newArray[i]=this.values[i]; else if (i==pos) newArray[i]=number; else if(i>pos){ newArray[i]=this.values[i-1]; } } for(int i = 0; i< newArray.length;i++){ System.out.print(newArray[i]+" "); } }
/** * Returns the string representation of the sequence as [first_value * second_value ...] where each value is separated by space. */ public String toString() { String stringVersion = " "; for (int i = 0; i < this.values.length; i++){ stringVersion = Integer.toString(this.values[i]);//don't know how to deal with string as return type System.out.print(stringVersion + " "); } return stringVersion; }
public static void main(String[] args) {
// demonstrate that each method works as expected Sequence array1 = new Sequence(3); Sequence array2 = new Sequence(3); int[] a = {1,2,3}; int[] b = {1,2,3}; array1.set(a); array2.set(b); System.out.println(array1.equals(array2)); //DONE // System.out.println(" "); // System.out.println(array1.sameValues(array2)); // System.out.println(" "); System.out.println(array1.isPermutationOf(array2)); //DONE // System.out.println(" "); // System.out.print(array1.append(array2)); // System.out.println(" "); // System.out.println(array1.merge(array2)): // System.out.println(" "); // System.out.println(array1.mergeSorted(array2)); // System.out.println(" "); System.out.println(array1.remove(2)); //ALMOST DONE System.out.println(" "); array1.insert(1, 2); //DONE System.out.println(" "); System.out.print(array1.toString()); //NOT QUITE
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
