2
Good night!
I have created a method that calculates the elements of a vector and makes the general mean, but I am not able to call it in the main method. PS: I am a beginner.
Thanks for your help!
package proj04;
public class media {
private static double mediatotal;
private static double[] notasAlunos = {10, 5, 7, 8, 6, 8, 10, 9, 7, 6, 7, 6, 8, 4, 6};
public static double getMediatotal() {
return mediatotal;
}
public static void setMediatotal(double mediatotal) {
media.mediatotal = mediatotal;
}
public static double mediat(double notasAlunos[]) {
double soma = 0;
for (int i = 0; i < 15; i++) {
soma = (soma + notasAlunos[i]) / 15;
mediatotal = soma;
}
return soma;
}
}
package proj04;
public class exe4main {
public static void main(String[] args) {
double mediavar;
media mediaturma = new media();
System.out.println( mediaturma.getMediatotal());
}
}
Thank you so much for the tips, I will apply in the next exercises!!! What I am realizing is that there is a logic error in this code. My intention is to add the elements of the vector notasAlunos and divide by 15 (average). The code as it is compiles, but returns the wrong result...
– Gerson Bueno
If you take the parentheses from this calculation
(soma + notasAlunos[i]) / 15;
should solve, but I would make the sum within thefor
and out of it would split by 15– Icaro Martins
Now I managed by applying your tips!!! I just wanted to know what it would be like if I wanted to access the array notesAlunos as private? By the way, I thank you very much for your teaching and even for improving the code, you prove to be a great professional.
– Gerson Bueno
I added an example of get and set, that a way to access and change variables private.
– Icaro Martins