Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Well I am trying to code my arrays into methods. But im getting an error in my output. getTotal . This method should accept a

Well I am trying to code my arrays into methods. But im getting an error in my output.

  • getTotal. This method should accept a one-dimensional array (here it ismyArray) as it's argument and return the total of the values in the array.
  • getAverage. This method should accept a one-dimensional array (here it ismyArray) as it's argument and return the average of the values in the array.
  • getHighest. This method should accept a one-dimensional array (here it ismyArray) as it's argument and return the highest value in the array.
  • getLowest. This method should accept a one-dimensional array (here it ismyArray) as it's argument and return the lowest value in the array.

My code:

public class ArrayOperations2

{

public static int getTotal( int [] myArray ){

int total = 0;

for( int currentNumbersIndex = 0; currentNumbersIndex

total+=myArray[currentNumbersIndex];

}

return total;

}

public static int getAverage( int [] myArray ){

int numbersTotal = getTotal( myArray );

int numberOfItemsInNumbersArray = myArray.length;

int average = numbersTotal / numberOfItemsInNumbersArray;

return average;

}

public static int getHighest( int [] myArray ){

int highestNumber = myArray[0];

for( int currentNumbersIndex = 0; currentNumbersIndex

if( myArray[ currentNumbersIndex ] >highestNumber ){

highestNumber = myArray[ currentNumbersIndex ];

}

}

return highestNumber;

}

public static int getLowest( int [] myArray ){

int lowestNumber = myArray[0];

for (int currentNumbersIndex = 0; currentNumbersIndex

if( myArray[ currentNumbersIndex ]

lowestNumber = myArray[ currentNumbersIndex ];

}

}

return lowestNumber;

}

public static void main(String[] args)

{

int[] myArray = {45, 38, 27, 46, 81, 72, 56, 61, 20, 48, 76, 91, 57, 35, 78};

System.out.println("Total: "+ getTotal( myArray )+" "+

"Average: "+ String.format("%.2f", getAverage( myArray ))+" "+

"Highest: "+ getHighest( myArray )+" "+

"Lowest: "+ getLowest( myArray ));

}

}

image text in transcribed
-jGRASP exec: java ArrayOperations2 Exception in thread "main" java. util. IllegalFormatConversionException: f != java. lang. Integer at java. base/java. util. Formatter$FormatSpecifier . failConversion (Formatter . java : 4426) at java . base/java. util. Formatter$FormatSpecifier . printFloat (Formatter . java : 2951) at java . base/java . util . Formatter$FormatSpecifier . print (Formatter . java : 2898) at java . base/java . util . Formatter . format (Formatter . java : 2673) at java . base/java. util . Formatter . format (Formatter . java : 2609) at java . base/ java. lang . String . format (String . java : 3274) at ArrayOperations2 . main (ArrayOperations2 . java : 41) jGRASP wedge: exit code for process is 1. jGRASP: operation complete

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transport Operations

Authors: Allen Stuart

2nd Edition

978-0470115398, 0470115394

Students also viewed these Programming questions

Question

PowerPoint presentation offering a summary outline of the text

Answered: 1 week ago