Call method with defined vector elements parameter

Asked

Viewed 260 times

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());            
    }    
}

1 answer

3

I did not find the error in your code, but I leave some tips.

Usually class name starts with uppercase letter.

The functions and variables of your class media are all static then you don’t need to create an instance of it new media() you can access directly by the Ex class. media.getMediatotal().

In the main you are printing on the return of getMediatotal which at present is still null.

You defined private static double[] notasAlunos as private then you won’t be able to access it outside the class media and apparently you want to pass it on to the function mediat, options:

  1. Change the private for public or leave without either of the two, without either of the two will work by being in the same package.

  2. Not receive notasAlunos per parameter in function mediat access from within it.

I made some changes to your code and put it formatted to run on Ideone see him running

class Exe4main
{
    public static void main (String[] args) // throws java.lang.Exception
    {
        double mediavar;

        Media.mediat( Media.notasAlunos );
        System.out.println( Media.getMediatotal() ); 
    }
}

class Media {

    private static double mediatotal;
    public 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;
    }               
}

A way to access or change a variable private would create methods get and set 'public' for this variable, that is, the same way you did for the variable mediatotal, see another example below

class Media{

    private static double[] minhaVar;

    public static void setMinhaVar( double[] novoValor ){

        minhaVar = novoValor;

    }

    public static double[] getMinhaVar( ) {

        return minhaVar;

    }
}
  • 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...

  • If you take the parentheses from this calculation (soma + notasAlunos[i]) / 15; should solve, but I would make the sum within the for and out of it would split by 15

  • 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.

  • I added an example of get and set, that a way to access and change variables private.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.