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

Please tell me what the syntax and/or logic errors are in these snippets of code

ID: 3605756 • Letter: P

Question

Please tell me what the syntax and/or logic errors are in these snippets of code, and provide the correction:

public static void main(String[] args) {

   int[] num = { 7, 2, 6, 4, 12, 6, 1, 8, 49, 3 };

   int lowest = num[0];

   int highest = num[0];

   for (int i = 0; i < num.length; i++) {

      if (num[i] < lowest) {

         lowest = num[i];

      } else {

         highest = num[i];

      }

   }

}

_________________________________________________________________

public static boolean doesArrayContain(int [] arr, int target) {

   boolean found = false;

   for (int i = 0; i < arr.length; ++i) {

      if (arr[i] == target) {

         found = true;

      } else {

         found = false;

      }

   }

   return found;

}

____________________________________________________________________

public static void main(String[] args) {

   ArrayList<String> coins = new ArrayList<String>();

   coins.add("quarter");

   coins.add("dime");

   coins.add("nickel");

   coins.add("penny");

   System.out.println("The coins are: ");

   for (int i = coins.size() - 1; i > 1; i--) {

      System.out.println(coins.get(i) + " ");

   }

}

Explanation / Answer

I have corrected tand tested the code , please find the inline comment in code only.

public static void main(String[] args) {

int[] num = { 7, 2, 6, 4, 12, 6, 1, 8, 49, 3 };

int lowest = num[0];

int highest = num[0];

for (int i = 0; i < num.length; i++) {

if (num[i] < lowest) {

lowest = num[i];

}

if(highest<num[i])//logic for highest was not correct

//it need to compare with array elements in order

highest=num[i];//to get highest no

}

}

public static boolean doesArrayContain(int [] arr, int target) {

boolean found = false;

for (int i = 0; i < arr.length; ++i) {

if (arr[i] == target) {

return true;//when target found no need to compare

//rest element it it good to return result

//or either break the loop because once you found and

//continue to loop may be changes for changed in result

}

}

return found;//or return false will work found is always false

}

public static void main(String[] args) {

ArrayList<String> coins = new ArrayList<String>();

coins.add("quarter");

coins.add("dime");

coins.add("nickel");

coins.add("penny");

System.out.println("The coins are: ");

//the logic for loop terminate was not correct

//as arraylist index start from 0 and loop terminate condition was

//i>1 means loop gonna skip 2 value at index 1 and 0 so after changing to

//i>=0 it will loop until the 0th index

for (int i = coins.size() - 1; i >=0; i--) {

System.out.println(coins.get(i) + " ");

}

}

//Please do let me know if u have any concern...or want me to expalin more