Consider the following class: public class Sequence { private int[] values; publ
ID: 3566326 • Letter: C
Question
Consider the following class:
public class Sequence
{
private int[] values;
public Sequence(int size) { values = new int[size]; }
public void set(int i, int n) { values[i] =n; }
}
Add a method
public boolean equals(Sequence other)
that checks whether the two sequences have the same values in the same order.
Add a method
public boolean sameValues(Sequence other)
to the Sequence class of the previous excericise that checks whether two sequences have the same values in some order, ignoring duplicates.
For example, the two sequences 1 4 9 16 9 7 4 9 11 and 11 11 7 9 16 4 1 would be considered identical. You will probably need one or more helper methods.
Explanation / Answer
public class Sequence
{
private int[] values;
public Sequence (int size) { values = new int[size]; }
public void set(int i, int n) { values[i] =n; }
public boolean equals(Sequence other)
{
// checks if Current Sequence and other Sequnce or Same from first element to last element in Array
for(int i = 0; i < values.length; i++)
{
if( values[i] != other.values[i])
return false;
}
return true;
}
public boolean sameValues(Sequence other)
{
boolean found = false;
for(int i = 0; i < values.length; i++)
{
for(int j = 0; j < values.length; j++)
{
if(values[i] == other.values[j] )
{
found = true;
// if number found break and check for other number of First Sequence
break;
}
}
// if First Sequence number is not found in the Other Sequence then return false
if(found == false)
return false;
}
// all the values of First sequence are present in other Sequence also
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.