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.
Related
idk if i have to get the standard while in the loop or out of the loop.also can you help me understand why it is i do it inside the loop or out and what the difference is. also i know the standard deviation formula in this situation would be something like (input - average)^2 for the first then ++ for every value after then add all that up and divide by the count then square root that. im just not fully sure how to write it and where to put it
import java.util.Scanner;
public class readFromKeyboard {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inStr = input.next();
int n;
int count=0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
double average=0;
int sum;
double deviation = 0;
while (!inStr.equals("EOL")) {
count++;
n = Integer.parseInt(inStr);
min = Math.min(min, n);
max = Math.max(max, n);
System.out.printf("%d ", n);
inStr = input.next();
average += n;
}
average = average/count;
System.out.println("\n The average of these numbers is " + average);
System.out.printf("The list has %d numbers\n", count);
System.out.printf("The minimum of the list is %d\n", min);
System.out.printf("The maximum of the list is %d\n", max);
input.close();
}
}
Given that you already calculated average, now you can calculate the standard deviation for each number.
Create array sd[] to store the standard deviation.
For each number, sd[i] = (average - input_i) ^ 2
Calculate variance:
For each standard deviation in sd[], add to a variable temp
Divide temp by total number of inputs
Calculate population standard deviation:
Square root variance
This is what I have:
double[] miles = new double [10];
double milesPerWeek = 26.0 / 10;
double totalMiles = 0;
double sum = 0;
double average = 0;
System.out.println("Week\tMiles");
for (int i = 0; i < miles.length; i++){
miles[i] += milesPerWeek;
totalMiles += miles[i];
System.out.printf("Week %d\t%.1f%n", i + 1, totalMiles);
}
for (int i = 0; i < miles.length; i++)
{
sum = sum + miles[i];
average = sum / miles.length;
}
System.out.printf("The total of miles run is: %.1f%n" + sum + "\n");
System.out.printf("The average of miles run is: %.1f%n" + average);
This is the problem I'm having with sum and average. I cannot seem to format the decimal places using print f without this error message:
The total of miles run is: Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%.1f'
Can you please direct me as I need one decimal place.
You need to pass the values as arguments, not concat them:
System.out.printf("The total of miles run is: %.1f%n\n", sum);
System.out.printf("The average of miles run is: %.1f%n", average);
System.out.println("Blah %.1f\n", sum);
You know that moment where there is an Ah Ha!!! I just had it...plus signs are not to be used in formating your output...commas are!
Corrected syntax:
System.out.printf("The total of miles run is: %.1f%n", sum, "\n");
System.out.printf("The average of miles run is: %.1f%n", average);
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
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;
}
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;
}