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

Java Question 1: You are given two int variables j and k, an int array zipcodeLi

ID: 3935169 • Letter: J

Question

Java

Question 1:

You are given two int  variables  j and k, an int  array  zipcodeList that has been declared and initialized , and an boolean  variable  duplicates.

Write some code that assigns  true  to duplicates if any two elements in the array have the same value , and that assigns  false  to duplicates otherwise. Use only j, k, zipcodeList, and duplicates.

Question 2:

You are given an int  variable  k, an int  array  zipcodeList that has been declared and initialized , and an boolean  variable  duplicates.

Write some code that assigns  true  to duplicates if there are two adjacent elements in the array that have the same value , and that assigns  false  to duplicates otherwise. Use only k, zipcodeList, and duplicates.

Explanation / Answer


// Question 1
public static boolean checkDuplicates(int[] zipcodeList)
{
   boolean duplicates = false;
   int k = 0;
   int j = 0;

   // check for duplicates
   for( k = 0; k < zipcodeList.length; k++)
   {
      for( j = 0; j < zipcodeList.length; j++)
       {
           if (zipcodeList(k) == zipcodeList(j) && j!=k )
              {
                  duplicates = true;
                  // break when duplicates are found in array
                  break;
              }
          }
   }
   return duplicates;
}

// Question 2
public static boolean checkAdjacentDuplicates(int[] zipcodeList)
{
   boolean duplicates = false;
   int k = 0;

   // check for adjacent duplicates
   for( k = 1; k < zipcodeList.length; k++)
   {
       if (zipcodeList(k) == zipcodeList(k-1))
        {
            duplicates = true;
            // break when adjacent duplicates are found in array
            break;
        }
  
   }
   return duplicates;
}

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