Question

These are my instructions: Your data should have been read in from the data file and stored into ...

These are my instructions:

Your data should have been read in from the data file and stored into an array. Next you need to calculate the following and display in a single Message box:

  • Average score
  • Highest score
  • Lowest score
  • Mode of the scores

Your program should be written using methods and should be well documented internally and externally. Your output should be displayed using Message boxes.

This is the .text file to use with the instructions:

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 20 21 22 23 24 25 20 21 22 23 24 25 26 27 28 29 30 26 27 28 29 30 26 27 28 29 30 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 20 21 22 23 24 25 20 21 22 23 24 25 26 27 28 29 30 26 27 28 29 30 26 27 28 29 30 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 20 21 22 23 24 25 20 21 22 23 24 25 26 27 28 29 30 26 27 28 29 30 26 27 28 29 30 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 20 21 22 23 24 25 20 21 22 23 24 25 26 27 28 29 30 26 27 28 29 30 26 27 28 29 30 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 48 49 50

This is my code that I have so far:

public static double getAverage(int[] scores)
{
double sum = 0;

for(int i=0; i<scores.length; i++)
sum += scores[i];
return sum/scores.length;
}

public static int highestScore(int []scores)
{
int highest = scores[0];

for(int i=1; i<scores.length; i++)
if(highest < scores[i])
highest = scores[i];
return highest;
}

public static int lowestScore(int []scores)
{
int lowest = scores[0];

for(int i=1; i<scores.length; i++)
if(lowest > scores[i])
lowest = scores[i];
return lowest;
}

public static int getMode(int[] count)
{
int mode = 1;

for(int i=1; i<count.length; i++)
{
if(count[i] > mode)
mode = count[i];
}

return mode;
}

public static void main(String args[])
{
String filename = "scores.txt";
File file = new File(filename);
  
int perfectScoresCounter = 0; // counts the number of perfect scores in the file
int count=0;
  
//Counts the number of elements in the file.
try
{
Scanner inputFile = new Scanner(file);
  
// reads elements from the file
while(inputFile.hasNextInt())
{
count++;
inputFile.nextInt();
}
  
inputFile.close();
  
}
catch(Exception e)
{
System.out.println("Error in opening file.....");
}
  
// Initializes array.
int[] scores = new int[count];
  
try
{
int arrIndex = 0; // initializes array index
  
Scanner inputFile = new Scanner(file);
  
while(inputFile.hasNextInt())
{
scores[arrIndex] = inputFile.nextInt();
arrIndex++;
}
  
inputFile.close();
}
catch(Exception e)
{
System.out.println("Error in opening file.....");
}

// Displays the result.
JOptionPane.showMessageDialog(null, "Average score is " + getAverage(scores));
JOptionPane.showMessageDialog(null,"Max Score is " + lowestScore(scores));
JOptionPane.showMessageDialog(null,"Min Score is " + highestScore(scores));
JOptionPane.showMessageDialog(null,"Mode Score is " + getMode(count));

System.exit(0);
}
}

My Question is:

I am getting this message "java:113: error: incompatible types: int cannot be converted to int[]
JOptionPane.showMessageDialog(null,"Mode Score is " + getMode(count));" when I run my code. What am I doing wrong with my code concerning getting the mode?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You should be passing the scores instead of count in the getMode() method

JOptionPane.showMessageDialog(nullMode Score isgetMode(count));

I ran the program and its working fine.

JOptionPane.showMessageDialog(null,"Mode Score is " + getMode(scores));

=========================================================================================

, Average score is + , message: etAverage (scores) , message: Max Score is +lovestScore (scores)) message: Min Score is

Average score is + 1, message: getAverage (scores)) 1, message: Max Score islovestScore (scores)) 1, message: Min Score

null, message: Average score isgetAverage (scores)) null, message: Max Score is + lovestscore (scores)) null, message:

Average score is + nent: null, message: Average score is + getAverage (scores)) nent: nul1, message: Max Score islovestS

Add a comment
Know the answer?
Add Answer to:
These are my instructions: Your data should have been read in from the data file and stored into ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT