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

FIX THE ERRORS FOR THE FOLLOWING PROGRAMS PLEASE ONE // provides a descendingSor

ID: 3722308 • Letter: F

Question

FIX THE ERRORS FOR THE FOLLOWING PROGRAMS PLEASE

ONE

// provides a descendingSort

public class DescendingSort extends Sorting {

@Override
protected boolean boolNeedExchange(int[] nArray, int first, int second) {
  boolean needExchange = false;
  if(first<second) {
   needExchange = true;
  }
  return needExchange;
}

}

TWO

// provides an ascending sort

public class AscendingSort extends Sorting {

@Override
protected boolean boolNeedEcchange(int[] nArray, int first, int second) {
  boolean needExchange = false;
  if(first<second) {
   needExchange = true;
  }
  return needExchange;
}

}

Explanation / Answer

Here is your solution:-

Actually , in you code your extending Sorting class and overiding methods. That methods should be from that Sorting class only. So, Sorting class is as follows

Sorting.java

public class Sorting {

protected boolean boolNeedEcchange(int[] nArray, int first, int second) {

return false;

}

protected boolean boolNeedExchange(int[] nArray, int first, int second) {

return false;

}

}

DescendingSort.java

import java.util.Scanner;

/**

* @author Lokesh Kumar

*

*/

public class DescendingSort extends Sorting {

@Override

protected boolean boolNeedExchange(int[] nArray, int first, int second) {

boolean needExchange = false;

if (first < second) {

needExchange = true;

}

return needExchange;

}

public static void main(String[] args) {

DescendingSort ds = new DescendingSort();

int n, temp;

Scanner s = null;

try {

s = new Scanner(System.in);

System.out.print("Enter elements size :");

n = s.nextInt();

int a[] = new int[n];

System.out.println("Enter element one by one:");

for (int i = 0; i < n; i++) {

a[i] = s.nextInt();

}

for (int i = 0; i < n; i++) {

for (int j = i + 1; j < n; j++) {

if (ds.boolNeedEcchange(a, a[i], a[j])) {

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}

System.out.print("Descending Order:");

for (int i = 0; i < n - 1; i++) {

System.out.print(a[i] + ",");

}

System.out.print(a[n - 1]);

} catch (Exception exception) {

s.close();

exception.printStackTrace();

}

}

}

AscendingSort.java

import java.util.Scanner;

/**

* @author Lokesh Kumar

*

*/

public class AscendingSort extends Sorting {

@Override

protected boolean boolNeedEcchange(int[] nArray, int first, int second) {

boolean needExchange = false;

if (first > second) {

needExchange = true;

}

return needExchange;

}

public static void main(String[] args) {

AscendingSort ascendingSort = new AscendingSort();

Scanner s = null;

int n, temp;

try {

s = new Scanner(System.in);

System.out.print("Enter elements size :");

n = s.nextInt();

int a[] = new int[n];

System.out.println("Enter element one by one:");

for (int i = 0; i < n; i++) {

a[i] = s.nextInt();

}

for (int i = 0; i < n; i++) {

for (int j = i + 1; j < n; j++) {

if (ascendingSort.boolNeedEcchange(a, a[i], a[j])) {

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}

System.out.print("Ascending Order:");

for (int i = 0; i < n - 1; i++) {

System.out.print(a[i] + ",");

}

System.out.print(a[n - 1]);

} catch (Exception exception) {

s.close();

exception.printStackTrace();

}

}

}

Explanation:-

Here the issue is comparision between two numbers in Ascending order class, it means low to high

In our code we had taken a if condion with greater than symbol suppose numbers like to acheive Ascending order

Example:- 1,2,9,8

By using above numbers our method return false for numbers 1 and 2 and then we keep same order untill and unless we get the return true from our method . Whenever our method return true then the two values will be swapped.

For example firstNumber is 9 and second number is 8 , here we will get true because 9 is greater than 8 , So values will be swapped means 9 willbe 8 & 8 will be 9. Then we will get false for 8 and 9.Then final output is

1,2,8,9

Note : - Error is in ascending order java file you kept it as lessthan symbol but it must be greater than symbol. And i've written main class to understand total operation if you dont want main class please remove from both ascending and descending java files.