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

Can I get help on this code. package projectb; import java.util.Scanner; import

ID: 3919754 • Letter: C

Question

Can I get help on this code.

package projectb;

import java.util.Scanner;
import java.util.Arrays;

class ProjectB
  
{

public static double median (int[] arr, int size)
{
int middle = size/2;

if (size % 2 == 1)
{
return arr[middle];
}
else
{
return (arr[middle] + arr[middle-1]) / 2.0;
}
}

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int sum = 0;
int count = 0;
int[] arr = new int[5];

while (true)
{

if (count > 5)
{

System.out.println("Error! cannot enter more than 5 numbers.");
System.exit(0);

}

System.out.print("Enter an integer : ");

int n = sc.nextInt();

if (n == 999)
{
break;
}

arr[count] = n;
sum += n;
count++;

}

// calculate average

double average = (double) sum / (double) count;


// sort array
Arrays.sort(arr, 0, count);


System.out.println(" Average : "

+ average);
System.out.println("Median : " + median(arr, count));

}

}

Current OUTPUT

run:

Enter an integer : 1

Enter an integer : 2

Enter an integer : 3

Enter an integer : 4

Enter an integer : 5

Enter an integer : 6

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5

at projectb.ProjectB.main(ProjectB.java:56)

/Users/IvanFlores/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1

BUILD FAILED (total time: 14 seconds)

This is compiling well up until the 6th number is entered. When a 6th number is entered It should give an error message saying you cannot enter more than 5 digits. Why is this occurring, did I leave something out?

This is what it should do when more than 5 digits are entered:

Correct Output when more than 5 digits are entered

run:
Enter an integer : 1
Enter an integer : 2
Enter an integer : 3
Enter an integer : 4
Enter an integer : 5
Enter an integer : 6
Error! can't enter more than 5 numbers.
BUILD SUCCESSFUL (total time: 13 seconds)

Explanation / Answer

package projectb; import java.util.Scanner; import java.util.Arrays; class ProjectB { public static double median (int[] arr, int size) { int middle = size/2; if (size % 2 == 1) { return arr[middle]; } else { return (arr[middle] + arr[middle-1]) / 2.0; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int sum = 0; int count = 0; int[] arr = new int[5]; while (true) { System.out.print("Enter an integer : "); int n = sc.nextInt(); if (count == 5) { System.out.println("Error! cannot enter more than 5 numbers."); System.exit(0); } if (n == 999) { break; } arr[count] = n; sum += n; count++; } // calculate average double average = (double) sum / (double) count; // sort array Arrays.sort(arr, 0, count); System.out.println(" Average : " + average); System.out.println("Median : " + median(arr, count)); } }

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