Directions: Your programs must be written using Object Oriented Programming, and
ID: 3547481 • Letter: D
Question
Directions: Your programs must be written using Object Oriented Programming, and they must implement arrays in some way. No credit will be given otherwise.
Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. For each method, demonstrate that it works in your tester. NOTE: Methods 1 - 6 should alter the original array, not simply print out what is requested. You should have your methods alter the original array and then in your tester, print out the new configuration of the array after calling the method on it. No credit will be given otherwise.
public class ArrayMethods {
private int[] values;
public ArrayMethods(int[] initialValues) { values = initialValues; }
public void swapFirstAndLast() { ... }
public void shiftRight() { ... }
...
}
Method 1: Swap the first and last elements in the array.
Method 2: Shift all elements by one to the right and move the last element into the first position. For example, 1 4 9 16 25 would be transformed into 25 1 4 9 16.
Method 3: Replace all even numbers with 0.
Method 4: Replace each element except the first and last by the larger of its two neighbors.
Method 5: Remove the middle element if the array length is odd, or the middle two elements if the length is even. You may "remove" the middle element by setting it equal to -1.
Method 6: Move all even elements to the front.
Method 7: Return the second largest element in the array
Method 8: Return true if the array is currently sorted in strict increasing order.
Method 9: Return true if array contains two adjacent duplicate elements.
Here:s a sample output if you want it.
Sample Output:
Original Array: 5 3 4 4 1 5 4 1 1 5
Array After Swap: 5 3 4 4 1 5 4 1 1 5
Array After Shift: 5 5 3 4 4 1 5 4 1 1
Array After Remove Middle: 5 5 3 4 -1 -1 5 4 1 1
Array Afer Moving Evens: 4 4 3 5 -1 -1 5 5 1 1
Second Largest Element: 5
Is Array Sorted?: false
Array Has Adjacent Dups?: true
Also- As description says: Please make it in object oriented. MEaning, one class as a tester class, and the other for the loops, arrays, computations, methods, etc...
Explanation / Answer
class ArrayMethods {
private int[] values;
public ArrayMethods(int [] initvalues){
//initializing the array named values.
values = initvalues;
}
public void swapFirstandLast(){
//swaping first and last value using a temporary variable
int temp = values[0];
values[0]= values[values.length-1];
values[values.length-1] = temp;
// printing the values of array
for(int i:values){
System.out.print(i+" ");
}
}
public void shiftRight(){
// temp store value of last element
int temp= values[values.length-1];
//shifting each element to right
for(int i=values.length-2;i>=0 ;i--){
values[i+1]=values[i];
}
//now putting last value at first place
values[0]=temp;
for(int i:values){
System.out.print(i+" ");
}
}
public void replaceEvenZero(){
for(int i=0;i<values.length;i++){
//checking if element is zero
if(values[i]%2==0){
values[i]=0;
}
}
for(int i:values){
System.out.print(i+" ");
}
}
public void replaceByNeighbors(){
for(int i=1;i<values.length-1;i++){
//checking which neighbors is large i.e left or right
if(values[i-1]>values[i+1]){
//for left neighbor
values[i]=values[i-1];
}else{
//for right neighbor
values[i]=values[i+1];
}
}
for(int i:values){
System.out.print(i+" ");
}
}
public void removeMiddle(){
//checking array length i.e. even or odd
if(values.length%2==0){
values[values.length/2]=-1;
values[(values.length/2)-1]=-1;
}else{
values[values.length/2]=-1;
}
for(int i:values){
System.out.print(i+" ");
}
}
public void moveEven(){
//j is used to keep track of count of even numbers
int j=-1;
for(int i=0;i<values.length;i++){
//checking if number is even or odd,if even then swap it
if(values[i]%2==0){
j++;
int temp = values[i];
values[i] = values[j];
values[j]=temp;
}
}
for(int i:values){
System.out.print(i+" ");
}
}
public int secLargest(){
//lar for largest element
//sec for second largest element
int lar,sec;
lar=sec=values[0];
for(int i=1;i<values.length;i++){
//comparing element with largest
if(values[i]>=lar){
//if new element is laregst than previous than store previous to sec
sec=lar;
lar = values[i];
}
}
return sec ;
}
public boolean checkSortArray() {
for(int i=0;i<values.length-1;i++){
// checking if preceding element is largest element than current
if(values[i+1]<values[i])
return false;
}
return true;
}
public boolean duplicate() {
for(int i=0;i<values.length-1;i++){
//checking adjcent duplicate element
if(values[i+1]==values[i])
return true;
}
return false;
}
}
public class ArrayMethodsTest {
public static void main(String[] args) {
int a[] = {5,3,4,4,1,5,4,1,1,5};
//passing array to ArrayMethods object in constructor
ArrayMethods arr = new ArrayMethods(a);
//printing original array
System.out.print("Original Array:");
for(int i:a){
System.out.print(" "+i);
}
System.out.print(" Array After Swap:");
arr.swapFirstandLast();
System.out.print(" Array After Shift:");
arr.shiftRight();
System.out.print(" Array After Remove Middle:");
arr.removeMiddle();
System.out.print(" Array Afer Moving Evens:");
arr.moveEven();
System.out.print(" Second Largest Element:"+arr.secLargest());
System.out.print(" Is Array Sorted?: "+arr.checkSortArray());
System.out.print(" Array Has Adjacent Dups?:"+arr.duplicate());
System.out.print(" Array After replace even with 0:");
arr.replaceEvenZero();
System.out.print(" Array After replacing element with neighbors:");
arr.replaceByNeighbors();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.