USING DOUBLE ARRAYS TO STORE TEST SCORES AND USING JOPTIONPANE TO ASK FOR SCORES
ID: 3542069 • Letter: U
Question
USING DOUBLE ARRAYS TO STORE TEST SCORES
AND
USING JOPTIONPANE TO ASK FOR SCORES.
The calcAvarage method calculates the average score. This method must accept a double
array as the sole parameter. It loops through the array elements to add up their values. It
then calculates the average and returns it as a double.
? The scores must be formatted as two places following the decimal, using a DecimalFormat
object.
? The display of grade determination and calculation results may be done in a console
IF U KNOW WHA CHU DOING IT SHOULD TAKE LESS THAN 15 MINUTES TO DO IT AND I WILL BE GREATLY APPRECIATIVE MEOWW
Explanation / Answer
import javax.swing.*;
import java.text.DecimalFormat;
class averageScore
{
static double calcAverage(double value[])
{
int len = value.length;
double sumScore = 0;
for(int i = 0; i < len; i++)
sumScore += value[i];
sumScore = sumScore / len;
return sumScore;
}
public static void main(String args[])
{
String values = JOptionPane.showInputDialog(
null, "Please enter the scores separated by commas: ");
int i,j, sum = 0,k = 0;
for(i=0; i<values.length(); i++)
if(values.charAt(i) == ',')
sum++;
double value[] = new double[sum+1];
values = values + ",";
i=0;
while(i < values.length())
{
if(values.charAt(i) != ',')
{
for(j = i+1; j<values.length(); j++)
{
if(values.charAt(j) == ',')
break;
}
value[k++] = Double.parseDouble(values.substring(i,j));
i = j+1;
}
}
double res = calcAverage(value);
DecimalFormat tmp = new DecimalFormat("0.00");
System.out.println("The average score is "+tmp.format(res));
}
}
Code runs smoothly!
Cheers! Don't forget to rate!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.