Problem when calculating average

Asked

Viewed 2,398 times

0

I’m trying to make a program, which gives the grade of two tests, P1 and P2. You sum and divide by 2 to get the average. If it is larger than 6, the program returns the following sentence: Approved. If the average is less than 4, the program returns the following sentence: Flunked. Now, if the grade is between 4 and 6, the program asks for the proof of recovery (VS) grade, and if it is greater than 6, returns: Approved, or if the note is less than 6 it returns: Flunked.

I wanted to ask for help, where I got the problem wrong.

Note: I cannot use Scanner. I have to keep it like this, only I cannot run the program.

class Media {
    public static double Media( float P1, float P2) {
        double Media = (P1 + P2) / 2;
    } 

    public static double VS( float nota) {
        double VS = nota;
    }

    public static void main(String [] args) { 
        if (Media >= 6) { 
            System.out.println("Aprovado.");
        } 
        else if (Media < 4) {
            System.out.println("Reprovado."); 
        } 
        else { 
            if( nota >= 6) {
                System.out.println("Aprovado."); 
            } 
            else { 
                System.out.println("Reprovado."); 
            } 
        } 
    } 
}
  • 1

    How notes are passed to your program?

  • Voce has two problems: 1) your program does not run; 2)vc do not read the input data at any time, as Voce intends to pass this information to your program?

  • Media and VS should return a value double, but instead you create a new variable that does nothing within the function.

  • if (Media >= 6) { mean is a class, you can’t make this comparison with classes, only with variables

1 answer

1

Your code presents some problems:

  • Media is already the name of the class, therefore it should not be used to designate the name of a method because in this way it is confused with a possible constructor of the same;
  • Define if you will use float or double to work properly with the method that returns the average of notes;
  • His method VS is initiating a local variable that has no functionality;
  • Your methods are not returning any value;
  • When calling the method that calculates the average within main, you are forgetting to pass the arguments regarding the grades of proofs P1 and P2;
  • You need to get the notes of all proofs (P1, P2 and Recovery) in any way, whether by requesting input of data by the user or not.

A possible solution would be:

class Media {

    public static float media(float P1, float P2) {
        return (P1 + P2) / 2;
    } 

    public static void main(String [] args) { 

        float notaP1 = 10, notaP2 = 5, notaRecuperacao = 6;

        if ( media(notaP1, notaP2) >= 6) { 
            System.out.println("Aprovado.");
        } 
        else if (media(notaP1, notaP2) < 4) {
            System.out.println("Reprovado."); 
        } 
        else { 
            if( notaRecuperacao >= 6) {
                System.out.println("Aprovado."); 
            } 
            else { 
                System.out.println("Reprovado."); 
            } 
        } 
    } 
}
  • 2

    Media já é o nome da classe, portanto ela não pode ser usada para designar o nome de uma função; Yes, do the test. PS: preferably say method instead of function, there is a difference of concept

  • 1

    Oh yes, in saying that she can not I referred to the fact that generate a confusion with a possible builder of the class. Thanks for the reminder about methods, it’s really convenient when it comes to OOP to designate them that way because they always belong to an object (or class). (:

Browser other questions tagged

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