Java assist. I have the following code I need to determine the issues and fix it
ID: 3709570 • Letter: J
Question
Java assist.
I have the following code I need to determine the issues and fix it. I have fixed a couple, you can see that below, but I am stuck on the next issues. Can you please help?
////Original.java////
import java.util.Scanner;
class BubbleSort {
public static main(String []args) {
int n, c, swap;
Scanner in = new Scanner(System);
System.out.println("Input number of integers to sort");
n = nextInt();
int array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array = in.nextInt();
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
System.out.println("Sorted list of numbers");
for (c = 0; c = n; c++)
System.out.println(array[c]);
}
}
////Fixed so far/////
//Changed this from java.util.scanner because I wanted a larger range declared
import java.util.*;
//Issue 1: class needs to be changed to Public Class
public class BubbleSort {
//Issue 2: Needs to be public static void main
public static void main(String[]args) {
int n, c, swap;
//Issue 3: Changed from "Scanner in = new Scanner(System);""
Scanner sc = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = nextInt();
int array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array = in.nextInt();
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
System.out.println("Sorted list of numbers");
for (c = 0; c = n; c++)
System.out.println(array[c]);
}
}
Explanation / Answer
BubbleSort.java
//Changed this from java.util.scanner because I wanted a larger range declared
import java.util.*;
//Issue 1: class needs to be changed to Public Class
public class BubbleSort {
//Issue 2: Needs to be public static void main
public static void main(String[]args) {
int n,d, c, swap;
//Issue 3: Changed from "
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
System.out.println("Sorted list of numbers");
for (c = 0; c < n; c++)
System.out.println(array[c]);
}
}
Output:
Input number of integers to sort
5
Enter 5 integers
4
3
5
2
1
Sorted list of numbers
1
2
3
4
5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.