Getting the overall average - java

I'm trying to get the overall average of the grades. I was able to get the average of each individual grade. Now just to get the total I'm not sure how to get it.
My output is:
Quizzes:66.0
Labs:88.0
Lab_atendance: 81.0
Midterms:91.0
public static double average(int[] scoreArray, int numScores,
int maxGrade, String name) {
double sum = 0;
for (int i = 0; i < scoreArray.length; i++) {
sum += scoreArray[i];
}
double average = Math.round((sum / numScores)*100/maxGrade);
System.out.println( name + ":" + average+" %");
return average;
}

What you are doing in not average exactly! but still..
Do the same procedure for Quizzes, Labs, Lab_attendance and Midterms! Isn't it obvious?
Here your numScores will be 4!
So from your given input
Avg = ((66.0 + 88.0 + 81.0 + 91.0) / 4) * 100 / maxGrade)

Your average calculation (and method signature) seem incorrect (averages are not calculated by dividing a "max"), I would use something like
// Note the new method signature - name then a variable number of scores.
public static double average(String name, int... scores) {
double sum = 0;
for (int score : scores) { // <-- for each loop.
sum += score;
}
final double average = (scores != null) ? sum / scores.length : 0;
System.out.println(name + ":"
+ Math.round(average * 100) + " %"); //<-- Display as a percentage
return average;
}

Related

How to fix "double cannot be dereferenced" in Java

Im trying to take doubles and double arrays as parameters for my methods, but when Im calling the methods I come up with the error, "double cannot be dereferenced".
I've tried different syntaxes, such as the var.method(array[]); , var.method(array);
I've tried multiple syntaxes on my parameter sets, (double[] array), (double array[]);
public class Rainfall extends rainfallTest
{
private double total;
private double Average;
//total rainfall for the year
public double totalRain(double[] rain){
for (int index = 0; index < rain.length; index++){
total += rain[index];
}
return total;
}//end totalRain
//calculating the monthly average
public double monthlyAvg(double totalRain){
Average = totalRain / 12.0;
return Average;
}
//calculating the month with the most rain
public double mostRain(double[] rain){
double highest = rain[0];
for (int index = 1; index < rain.length; index++){
if (rain[index] > highest){
highest = rain[index];
}
}
return highest;
}
public double leastRain(double[] rain){
double lowest = rain[0];
for (int index = 1; index < rain.length; index++){
if (rain[index] < lowest){
lowest = rain[index];
}
}
return lowest;
}
}
and the testing program:
public class rainfallTest{
public static void main(String[] args){
double rain[] = {2.2, 5.2, 1.0, 10.2, 3.2, 9.2, 5.2, 0.0, 9.9, 12.2, 5.2, 2.2};
double Average;
double total;
double most;
double least;
System.out.println("Here's the rainfall for this year");
total.totalRain(rain);
Average.monthlyAvg(total);
most.mostRain(rain);
least.leastRain(rain);
System.out.println("The total rainfall for the year is: " + total +
". the monthly average of rain is: " + Average +
". The highest rain in one month: " + most +
". The lowest amount of rain in one month: " + least);
}
}
You're not calling your methods properly. First of all, you need an instance of your class:
Rainfall rainfall = new Rainfall();
Then you can call the methods on that instance, and assign the return value to your variables:
double total = rainfall.totalRain(rain);
double average = rainfall.monthlyAvg(total);
double most = rainfall.mostRain(rain);
double least = rainfall.leastRain(rain);
Also, not a huge issue, but I don't see any reason for Rainfall to extend rainfallTest.

Incorrect variable value for during average calculation for num 1 to 100

public class SumandAverage {
public static void main(String[] args) {
int sum=0;
double average;
int lowerBound = 1;
int upperBound = 100;
while(lowerBound<=upperBound) {
sum = sum+lowerBound;
lowerBound++;
}
System.out.println("The Sum is "+sum);
average=sum/upperBound;
System.out.println("The average is " + average);
}
}
the result i get is "The Sum is 5050 The average is 50.0"..why is my upperBound changing to 101 and resulting in incorrect average value of 50.0 instead of 50.5 when i am not even changing it at all? must be something silly, but i am not able to spot.
You are dividing ints, so the result is int. Divide doubles instead :
average=(double)sum/upperBound;
5050/100 = 50, since int division can only produce an int. After you assign it to a double variable, you get 50.0.
int sum=0;
double average;
int lowerBound = 1;
int upperBound = 100;
while(lowerBound<=upperBound) {
sum = sum+lowerBound;
lowerBound++;
}
System.out.println("The Sum is "+sum);
average=(double)sum/upperBound; //change
System.out.println("The average is " + average);
ie change only average=(double)sum/upperBound; .In your case you are getting int results because sum and upperBound are int.Other option is you can also change the datatypes of these variable to get the desired output.
Demo

Java sum of numbers error

This code seems to run well, but am getting error message regarding calculating the sum of the integers entered.
The point of the exercise is to input a series of numbers, and after the value -1 is entered, calculate the sum of the numbers, how many numbers were entered, the mean value, and the number of odd and even numbers.
The output I get suggests the program is running fine, but still get an eror message.
With input 1 17 2 18 17 -1 should print "sum: 55" expected:<55> but was: <0>
Apologies in advance if my Java syntax is a bit inelegant. I'm fairly new at this! Code below.
import java.util.Scanner;
public class LoopsEndingRemembering {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Type numbers: ");
int n;
double sum = 0.0;
int i = 0;
double average = 0.0;
int odd = 0;
int even = 0;
while (true) {
n = Integer.parseInt(reader.nextLine());
if (n != -1) {
System.out.print("Type numbers: ");
sum += n;
i++;
average = sum / i;
if (n % 2 == 0) {
even++;
} else {
odd++;
}
} else {
System.out.println("Thank you and see you later!");
System.out.println("The sum is " + sum);
System.out.println("How many numbers: " + i);
System.out.println("Average: " + average);
System.out.println("Even numbers: " + even);
System.out.println("Odd numbers: " + odd);
break;
}
}
}
}
You're printing 55.0. It seems you're getting this program tested by another program which you don't have access to the source code of.
Issue 1
You probably want to print 55 specifically.
Instead of:
double sum = 0.0;
Do:
int sum = 0;
Issue 2
Use int over double. Cast to double for the average value.
Then instead of this:
average = sum / i;
Do something like:
average = (double)sum / i;
Issue 3
Also, it seems the error message wants you to print as sum: 55.
So change this:
System.out.println("The sum is " + sum);
To:
System.out.println("sum: " + sum);

Calculating the average in java

double Hg = Double.parseDouble(values2[17]);
for (int i = 0; i < 3; i++) {
double Avg = (Hg + Hg + Hg) / 3;
System.out.println(Avg);
}
My program reads a file and puts everything in a String array called values2. The array element #17 (values2[17]) contains the values I need. I converted that array element into a double so I could then calculate the average. The program should take the first 3 values that are in values[17] and then divide by 3 to calculate the average, but it is just printing out the same values 3 times instead of adding and then dividing.
Any thoughts of what I am doing wrong?
If I understand what you're doing (comma seperated String of values), it could be as simple as...
public static void main(String[] args) {
String[] values = new String[18];
values[17] = "1.0, 2.0, 4.0";
double total = 0;
int count = 0;
for (String v : values[17].split(",")) {
if (v != null) {
total += Double.valueOf(v.trim());
count++;
}
}
double avg = total / ((double) count);
System.out.println("The average of " + values[17] + " is " + avg);
}
Which outputs
The average of 1.0, 2.0, 4.0 is 2.3333333333333335
If values2[17] is a String like "2 5 7", Then you need to parse all them each individually
String[] nums = valus2[17].split(" ");
double d1 = Double.parseDouble(num[0]);
double d2 = Double.parseDouble(num[1]);
double d3 = Double.parseDouble(num[3]);
double average = (d1 + d2 + d3) / 3;
On The other hand if you you have a String like "567" and you want the individual values, can you split into a char array then add the int values
String value = "567" // value2[17]
char[] digits = value.toCharArray();
int total = 0;
for (char c : digits){
total += Integer.parseInt(String.valueOf(c));
}
double average = total / new Double(digits.length);
You don't do anything with the i...
double total= 0.0;
for(int i = 0; i < 3;i++) {
total += values[i];
}
double avarage = total/3.0;
System.out.println(avarage);
Currently you are adding the whole array to itself 3 times then dividing but you need to add the three ideas in the array then divide by three
do
double Avg = values2[0] + values2[1] + values2[2] / 3

How would I total two sets of numbers entered by a user?

I am writing a program where I must ask the user how many assignments they have. Then, I must ask them for their score and the maximum points possible for the assignment. I know how to find the sum of the first set of numbers they entered (their scores) but I am stuck on how I would go about totaling the maximum points possible. Here is what I have so far:
int totalNumber = scan.nextInt();
double sum = 0.0;
for (int i = 1; i <= totalNumber; i++) {
System.out.print("Assignment " + i + " score and max? ");
double score = scan.nextDouble();
double maxScore = scan.nextDouble();
sum += score;
The output looks something like this:
Assignment 1 score and max? 16 17
Assignment 2 score and max? 18 19
I am not sure how I would total the maximum points (17 and 19 in the example) because I must print the total points:
(sum of scores)/(sum of maximum points).
Thanks.
the simple answer is to add another variable for summing the maxScore
int totalNumber = scan.nextInt();
double sum = 0.0;
double maxSum = 0.0;
for (int i = 1; i <= totalNumber; i++) {
System.out.print("Assignment " + i + " score and max? ");
double score = scan.nextDouble();
double maxScore = scan.nextDouble();
sum += score;
maxSum += maxScore;
}

Categories

Resources