Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a JAVA question. the code that already exists below must remain unchange

ID: 3790276 • Letter: T

Question

This is a JAVA question. the code that already exists below must remain unchanged, but i need additional code where the comments are.

import java.util.ArrayList;
import java.util.Arrays;

public class homework4 {
  
   /**
   * Shifts all elements in an array one position to the right so last element becomes first
   * i.e. [3, 5, 7, 9, 12] becomes [12, 3, 5, 7, 9]
   * @param a integer array to be shifted
   * @return array of integers shifted right one position
   */
   public static int[] rotateRight(int[] a) {
  

   }
  
  
  
   /**
   * Replaces all even elements in an array with 0
   * @param a integer array to replaces even values
   * @return array with all even elements set to 0
   */
   public static int[] evens(int[] a) {
      
   }
  
  
  
   /**
   * Tests whether or not an array is sorted
   * @param a the array to test
   * @return true if sorted, false otherwise
   */
   public static boolean isSorted(int[] a) {
      
   }
  
  
  
   /**
   * Retrieves the second largest value stored in an array
   * @param a the array to search
   * @return second largest element in a
   */
   public static int secondLargest(int[] a) {
      
   }
  
  
  
   /**
   * Tests whether or not an array contains duplicate values
   * @param a the array to test for duplicates
   * @return true if duplicate(s) found, false otherwise
   */
   public static boolean hasDuplicates(int[] a) {
      
   }
  
  
  
   /**
   * Shifts all elements in an ArrayList one position to the right so last element becomes first
   * i.e. [3, 5, 7, 9, 12] becomes [12, 3, 5, 7, 9]
   * @param a integer ArrayList to be shifted
   * @return ArrayList of integers shifted right one position
   */
   public static ArrayList<Integer> rotateRight(ArrayList<Integer> a) {
      
   }
  
  
  
   /**
   * Replaces all even elements in an ArrayList with 0
   * @param a integer ArrayList to replaces even values
   * @return ArrayList with all even elements set to 0
   */
   public static ArrayList<Integer> evens(ArrayList<Integer> a) {
      
   }
  
  
  
   /**
   * Tests whether or not an ArrayList is sorted
   * @param a the ArrayList to test
   * @return true if sorted, false otherwise
   */
   public static boolean isSorted(ArrayList<Integer> a) {
      
   }
  
  
  
   /**
   * Retrieves the second largest value stored in an ArrayList
   * @param a the ArrayList to search
   * @return second largest element in a
   */
   public static int secondLargest(ArrayList<Integer> a) {
      
   }
  
  
  
   /**
   * Tests whether or not an ArrayList contains duplicate values
   * @param a the ArrayList to test for duplicates
   * @return true if duplicate(s) found, false otherwise
   */
   public static boolean hasDuplicates(ArrayList<Integer> a) {
      
   }
  
  
  
   /**
   * Main method to test array and ArrayList methods
   * @param args NA
   */
   public static void main(String[] args) {
       //Create arrays to test
       int[] values = {1, 2, 5, 3, 7, 6, 10, 6, 9, 4};
       int[] dupValues = {2, 5, 6, 8, 2, 8};
       int[] dupValues2 = {1, 7, 3, 4, 7};
       int[] sortedValues = {1, 2, 4, 7, 9, 20};
      
       // Create ArrayLists to test
       ArrayList<Integer> list = new ArrayList<Integer>();
       list.add(1);
       list.add(2);
       list.add(8);
       list.add(3);
       list.add(5);
       list.add(7);
      
       ArrayList<Integer> dupList = new ArrayList<Integer>();
       dupList.add(1);
       dupList.add(2);
       dupList.add(8);
       dupList.add(3);
       dupList.add(1);
       dupList.add(1);
      
       ArrayList<Integer> dupList2 = new ArrayList<Integer>();
       dupList2.add(1);
       dupList2.add(2);
       dupList2.add(8);
       dupList2.add(3);
       dupList2.add(6);
       dupList2.add(1);
      
       ArrayList<Integer> sortedList = new ArrayList<Integer>();
       sortedList.add(1);
       sortedList.add(2);
       sortedList.add(8);
       sortedList.add(13);
       sortedList.add(5);
       sortedList.add(7);
      
       // Test rotateRight() methods
       System.out.println("Rotate Right:");
       System.out.println(Arrays.toString(values) + " shifted right is " + Arrays.toString(rotateRight(values)));
       System.out.println(list + " list shifted right is " + rotateRight(list));
      
      
       // Test evens() methods
       System.out.println(" Replace Evens:");
       System.out.println(Arrays.toString(values) + " evens to 0 is " + Arrays.toString(evens(values)));
       System.out.println(list + " list with evens to 0 is " + evens(list));
      
       // Test isSorted() methods
       System.out.println(" Has Duplicates?");
       if (hasDuplicates(values)) {
           System.out.println(Arrays.toString(values) + " has duplicates");
       }
       else {
           System.out.println(Arrays.toString(values) + " does NOT have duplicates");
       }
       if (hasDuplicates(dupValues)) {
           System.out.println(Arrays.toString(values) + " has duplicates");
       }
       else {
           System.out.println(Arrays.toString(values) + " does NOT have duplicates");
       }
       if (hasDuplicates(dupValues2)) {
           System.out.println(Arrays.toString(values) + " has duplicates");
       }
       else {
           System.out.println(Arrays.toString(values) + " does NOT have duplicates");
       }
      
       if (hasDuplicates(list)) {
           System.out.println(Arrays.toString(values) + " has duplicates");
       }
       else {
           System.out.println(Arrays.toString(values) + " does NOT have duplicates");
       }
       if (hasDuplicates(dupList)) {
           System.out.println(Arrays.toString(values) + " has duplicates");
       }
       else {
           System.out.println(Arrays.toString(values) + " does NOT have duplicates");
       }
       if (hasDuplicates(dupList2)) {
           System.out.println(Arrays.toString(values) + " has duplicates");
       }
       else {
           System.out.println(Arrays.toString(values) + " does NOT have duplicates");
       }
      
       // Test secondLargest() methods
       System.out.println(" Second Largest:");
       System.out.println(Arrays.toString(values) + "'s second largerst value is " + secondLargest(values));
       System.out.println(list + "'s second largest value is " + secondLargest(list));
      
       // Test isSorted() methods
       System.out.println(" Is Sorted?");
       if (isSorted(values)) {
           System.out.println(Arrays.toString(values) + " is sorted");
       }
       else {
           System.out.println(Arrays.toString(values) + " is NOT sorted");
       }
       if (isSorted(sortedValues)) {
           System.out.println(Arrays.toString(sortedValues) + " is sorted");
       }
       else {
           System.out.println(Arrays.toString(sortedValues) + " is NOT sorted");
       }
      
       if (isSorted(list)) {
           System.out.println(list + " is sorted");
       }
       else {
           System.out.println(list + " is NOT sorted");
       }
       if (isSorted(sortedList)) {
           System.out.println(sortedList + " is sorted");
       }
       else {
           System.out.println(sortedList + " is NOT sorted");
       }
      

   }

}

Explanation / Answer

Hi Buddy, Please find the below java program. I've added comments for your better understanding.

import java.util.*;
import java.lang.*;
import java.io.*;

class Main {
  
/**
* Shifts all elements in an array one position to the right so last element becomes first
* i.e. [3, 5, 7, 9, 12] becomes [12, 3, 5, 7, 9]
* @param a integer array to be shifted
* @return array of integers shifted right one position
*/
public static int[] rotateRight(int[] a) {
//Initialize a new array
int ans[] = new int[a.length];
//Store the last element in the first position of the new array
ans[0] = a[a.length-1];
//Keep shifting all the elements to the right
for(int i=1;i<a.length;i++){
ans[i] = a[i-1];
}
return ans;
}
  
  
  
/**
* Replaces all even elements in an array with 0
* @param a integer array to replaces even values
* @return array with all even elements set to 0
*/
public static int[] evens(int[] a) {
//Initialize a new array
int ans[] = new int[a.length];
//Iterate through all the elements and check for even and odd and store it in the new array if the element
//is odd
for(int i=0;i<a.length;i++){
if(a[i]%2!=0){
ans[i] = a[i];
}
}
return ans;
}
  
  
  
/**
* Tests whether or not an array is sorted
* @param a the array to test
* @return true if sorted, false otherwise
*/
public static boolean isSorted(int[] a) {
//Iterate through all the elements in the array and compare with the previous
//element. If previous element is greater return false;
for(int i=1;i<a.length;i++){
if(a[i]<a[i-1]){
return false;
}
}
return true;
}
  
  
  
/**
* Retrieves the second largest value stored in an array
* @param a the array to search
* @return second largest element in a
*/
public static int secondLargest(int[] a) {
//Initialize 2 variables
int max=-1, max2=-1;
//Iterating through all the elements in the array
for(int i=0;i<a.length;i++){
//If an element is greater than the maximum, update the maximum and make previous maximum
//as the second maximum
if(a[i]>=max){
max2 = max;
max = a[i];
}
//Else check if this element is greater than the second max and update
else if(a[i]>=max2){
max2 = a[i];
}
}
return max2;
}
  
  
  
/**
* Tests whether or not an array contains duplicate values
* @param a the array to test for duplicates
* @return true if duplicate(s) found, false otherwise
*/
public static boolean hasDuplicates(int[] a) {
//Initialize a hashset
HashSet<Integer> hs = new HashSet();
  
//Iterate through all the elements in the hashset
for(int i=0;i<a.length;i++){
//If this element already exists in the hashset return true
if(hs.contains(a[i])){
return true;
}
//If element is not present in the hashset , add it to the hashset
hs.add(a[i]);
}
return false;
}
  
  
  
/**
* Shifts all elements in an ArrayList one position to the right so last element becomes first
* i.e. [3, 5, 7, 9, 12] becomes [12, 3, 5, 7, 9]
* @param a integer ArrayList to be shifted
* @return ArrayList of integers shifted right one position
*/
public static ArrayList<Integer> rotateRight(ArrayList<Integer> a) {
ArrayList<Integer> ans = new ArrayList();
ans.add(a.get(a.size()-1));
for(int i=0;i<a.size()-1;i++){
ans.add(a.get(i));
}
return ans;
}
  
  
  
/**
* Replaces all even elements in an ArrayList with 0
* @param a integer ArrayList to replaces even values
* @return ArrayList with all even elements set to 0
*/
public static ArrayList<Integer> evens(ArrayList<Integer> a) {
ArrayList<Integer> ans = new ArrayList();
for(int i=0;i<a.size();i++){
if(a.get(i)%2==0){
ans.add(0);
}
else{
ans.add(a.get(i));
}
}
return ans;
}
  
  
  
/**
* Tests whether or not an ArrayList is sorted
* @param a the ArrayList to test
* @return true if sorted, false otherwise
*/
public static boolean isSorted(ArrayList<Integer> a) {
for(int i=1;i<a.size();i++){
if(a.get(i)<a.get(i-1)){
return false;
}
}
return true;
}
  
  
  
/**
* Retrieves the second largest value stored in an ArrayList
* @param a the ArrayList to search
* @return second largest element in a
*/
public static int secondLargest(ArrayList<Integer> a) {
int max=-1, max2=-1;
for(int i=0;i<a.size();i++){
if(a.get(i)>=max){
max2 = max;
max = a.get(i);
}
else if(a.get(i)>=max2){
max2 = a.get(i);
}
}
return max2;
}
  
  
  
/**
* Tests whether or not an ArrayList contains duplicate values
* @param a the ArrayList to test for duplicates
* @return true if duplicate(s) found, false otherwise
*/
public static boolean hasDuplicates(ArrayList<Integer> a) {
HashSet<Integer> hs = new HashSet();
for(int i=0;i<a.size();i++){
if(hs.contains(a.get(i))){
return true;
}
hs.add(a.get(i));
}
return false;
}
  
/**
* Main method to test array and ArrayList methods
* @param args NA
*/
public static void main(String[] args) {
//Create arrays to test
int[] values = {1, 2, 5, 3, 7, 6, 10, 6, 9, 4};
int[] dupValues = {2, 5, 6, 8, 2, 8};
int[] dupValues2 = {1, 7, 3, 4, 7};
int[] sortedValues = {1, 2, 4, 7, 9, 20};
  
// Create ArrayLists to test
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(8);
list.add(3);
list.add(5);
list.add(7);
  
ArrayList<Integer> dupList = new ArrayList<Integer>();
dupList.add(1);
dupList.add(2);
dupList.add(8);
dupList.add(3);
dupList.add(1);
dupList.add(1);
  
ArrayList<Integer> dupList2 = new ArrayList<Integer>();
dupList2.add(1);
dupList2.add(2);
dupList2.add(8);
dupList2.add(3);
dupList2.add(6);
dupList2.add(1);
  
ArrayList<Integer> sortedList = new ArrayList<Integer>();
sortedList.add(1);
sortedList.add(2);
sortedList.add(8);
sortedList.add(13);
sortedList.add(5);
sortedList.add(7);
  
// Test rotateRight() methods
System.out.println("Rotate Right:");
System.out.println(Arrays.toString(values) + " shifted right is " + Arrays.toString(rotateRight(values)));
System.out.println(list + " list shifted right is " + rotateRight(list));
  
  
// Test evens() methods
System.out.println(" Replace Evens:");
System.out.println(Arrays.toString(values) + " evens to 0 is " + Arrays.toString(evens(values)));
System.out.println(list + " list with evens to 0 is " + evens(list));
  
// Test isSorted() methods
System.out.println(" Has Duplicates? "+Arrays.toString(values));
if (hasDuplicates(values)) {
System.out.println(Arrays.toString(values) + " has duplicates");
}
else {
System.out.println(Arrays.toString(values) + " does NOT have duplicates");
}
if (hasDuplicates(dupValues)) {
System.out.println(Arrays.toString(dupValues) + " has duplicates");
}
else {
System.out.println(Arrays.toString(dupValues) + " does NOT have duplicates");
}
if (hasDuplicates(dupValues2)) {
System.out.println(Arrays.toString(dupValues2) + " has duplicates");
}
else {
System.out.println(Arrays.toString(dupValues2) + " does NOT have duplicates");
}
  
if (hasDuplicates(list)) {
System.out.println(list + " has duplicates");
}
else {
System.out.println(list + " does NOT have duplicates");
}
if (hasDuplicates(dupList)) {
System.out.println(dupList + " has duplicates");
}
else {
System.out.println(dupList + " does NOT have duplicates");
}
if (hasDuplicates(dupList2)) {
System.out.println(dupList2 + " has duplicates");
}
else {
System.out.println(dupList2 + " does NOT have duplicates");
}
  
// Test secondLargest() methods
System.out.println(" Second Largest:");
System.out.println(Arrays.toString(values) + "'s second largerst value is " + secondLargest(values));
System.out.println(list + "'s second largest value is " + secondLargest(list));
  
// Test isSorted() methods
System.out.println(" Is Sorted?");
if (isSorted(values)) {
System.out.println(Arrays.toString(values) + " is sorted");
}
else {
System.out.println(Arrays.toString(values) + " is NOT sorted");
}
if (isSorted(sortedValues)) {
System.out.println(Arrays.toString(sortedValues) + " is sorted");
}
else {
System.out.println(Arrays.toString(sortedValues) + " is NOT sorted");
}
  
if (isSorted(list)) {
System.out.println(list + " is sorted");
}
else {
System.out.println(list + " is NOT sorted");
}
if (isSorted(sortedList)) {
System.out.println(sortedList + " is sorted");
}
else {
System.out.println(sortedList + " is NOT sorted");
}
  
}
}

OUTPUT :

Rotate Right:
[1, 2, 5, 3, 7, 6, 10, 6, 9, 4] shifted right is [4, 1, 2, 5, 3, 7, 6, 10, 6, 9]
[1, 2, 8, 3, 5, 7] list shifted right is [7, 1, 2, 8, 3, 5]

Replace Evens:
[1, 2, 5, 3, 7, 6, 10, 6, 9, 4] evens to 0 is [1, 0, 5, 3, 7, 0, 0, 0, 9, 0]
[1, 2, 8, 3, 5, 7] list with evens to 0 is [1, 0, 0, 3, 5, 7]

Has Duplicates? [1, 2, 5, 3, 7, 6, 10, 6, 9, 4]
[1, 2, 5, 3, 7, 6, 10, 6, 9, 4] has duplicates
[2, 5, 6, 8, 2, 8] has duplicates
[1, 7, 3, 4, 7] has duplicates
[1, 2, 8, 3, 5, 7] does NOT have duplicates
[1, 2, 8, 3, 1, 1] has duplicates
[1, 2, 8, 3, 6, 1] has duplicates

Second Largest:
[1, 2, 5, 3, 7, 6, 10, 6, 9, 4]'s second largerst value is 9
[1, 2, 8, 3, 5, 7]'s second largest value is 7

Is Sorted?
[1, 2, 5, 3, 7, 6, 10, 6, 9, 4] is NOT sorted
[1, 2, 4, 7, 9, 20] is sorted
[1, 2, 8, 3, 5, 7] is NOT sorted
[1, 2, 8, 13, 5, 7] is NOT sorted

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote