-4

I am trying to write a program that calculates my math and English GPA. I can't get the main to recognize my two floats, mathGpa and englishGpa. It tells me to make them static but making them static means that they become strings and I need them to remain doubles.

import java.util.Scanner;
public class GPA {

    public static void main(String[] args)
    {
        double finalGpa=0;

        mathGpa();
        englishGpa();

        finalGpa= (mathGpa + englishGpa)/2;


    }

    public double mathGpa() {//Begin mathGpa
        int Math;
        double mathGpa = 0;
        System.out.println("Math = ");
        Scanner math = new Scanner(System.in);
        Math= math.nextInt();
        math.close();

        if (Math >100){
            System.out.println("You have mistyped something");
        }
        if (Math >= 94){
            System.out.println("You have an A");
            mathGpa = 4.0;
            System.out.println(mathGpa);
        }
        if (Math < 94 && Math >=90){
            System.out.println("You have an A-");
            mathGpa = 3.7;
            System.out.println(mathGpa);
        }
        if (Math < 90 && Math >=87){
            System.out.println("You have a B+");
            mathGpa = 3.3;
            System.out.println(mathGpa);
        }
        if (Math < 87 && Math >=80){
            System.out.println("You have a B");
            mathGpa = 3.0;
            System.out.println(mathGpa);
        }
        if (Math < 80 && Math >=77){
            System.out.println("You have a B-");
            mathGpa = 2.7;
            System.out.println(mathGpa);
        }
        if (Math < 77 && Math >=73){
            System.out.println("You have a C+");
            mathGpa = 2.3;
            System.out.println(mathGpa);
        }
        if (Math < 73 && Math >=70){
            System.out.println("You have a C");
            mathGpa = 2.0;
            System.out.println(mathGpa);
        }
        if (Math < 70 && Math >=67){
            System.out.println("You have a C-");
            mathGpa = 1.7;
            System.out.println(mathGpa);
        }
        if (Math < 67 && Math >=67){
            System.out.println("You have a D+");
            mathGpa = 1.3;
            System.out.println(mathGpa);
        }
        if (Math < 67 && Math >=63){
            System.out.println("You have a D");
            mathGpa = 1.0;
            System.out.println(mathGpa);
        }
        if (Math < 63 && Math >=60){
            System.out.println("You have a D-");
            mathGpa = 0.7;
            System.out.println(mathGpa);
        }
        if (Math < 60){
            System.out.println("You have a F");
            mathGpa = 1.7;
            System.out.println(mathGpa);
        }

        return mathGpa;
    }//End mathGpa

    public double englishGpa() {//Begin englishGpa
        int English;
        double englishGpa = 0;
        System.out.println("English = ");
        Scanner english = new Scanner(System.in);
        English= english.nextInt();
        english.close();

        if (English >100){
            System.out.println("You have mistyped something");
        }
        if (English >= 94){
            englishGpa = 4.0;
        }
        if (English < 94 && English >=90){
            englishGpa = 3.7;
        }
        if (English < 90 && English >=87){
            englishGpa = 3.3;
        }
        if (English < 87 && English >=80){
            englishGpa = 3.0;
        }
        if (English < 80 && English >=77){
            englishGpa = 2.7;
        }
        if (English < 77 && English >=73){
            englishGpa = 2.3;
        }
        if (English < 73 && English >=70){
            englishGpa = 2.0;
        }
        if (English < 70 && English >=67){
            englishGpa = 1.7;
        }
        if (English < 67 && English >=67){
            englishGpa = 1.3;
        }
        if (English < 67 && English >=63){
            englishGpa = 1.0;
        }
        if (English < 63 && English >=60){
            englishGpa = 0.7;
        }
        if (English < 60){
            englishGpa = 1.7;
        }

        return englishGpa;
    }//End englishGpa

}//End Class
ZygD
  • 22,092
  • 39
  • 79
  • 102
Barn Cats
  • 5
  • 1
  • 3
    but making them static means that they become strings??? How do they become String? static double is very much valid – Juned Ahsan Jun 30 '15 at 01:15
  • This may or may not be your immediate problem, but you have a function named `mathGpa` and a local variable named `mathGpa` (the same name). Choose different names. – Greg Hewgill Jun 30 '15 at 01:16
  • `static` only means they're accessible without any instance of the class existing. the return-type won't change –  Jun 30 '15 at 01:17

2 Answers2

0

Making a method static does not mean that they become strings, it simply means that the method can be called. When you declared 'double' in mathGpa and englishGpa you set the return type to double, that, will not change unless you switch 'double' with 'String'. Also, as said in the comments (@Greg Hewgill) you should change the local variable name.

For more on 'static' click here.

For more on methods click here

UnknownOctopus
  • 2,057
  • 1
  • 15
  • 26
0

Static variable

Static variable's value is same for all object(or instances) of the class. In other words, you can think that all instances(objects) of the same class share a single copy of static variables.
Example #1:

class StaticVariable
{
   static int counter=0;
   public void increment()
   {
       counter++;
   }
   public static void main(String args[])
   {
       StaticVariable objectA = new StaticVariable();
       StaticVariable objectB = new StaticVariable();
       objectA .increment();
       objectB .increment();
       System.out.println("objectA- counter is: "+ objectA.count);
       System.out.println("objectB- counter is: "+ objectA.count);
   }
}

Output:

objectA- counter is: 2
objectB- counter is: 2

Both the objects of class share the same copy of static variable counter; that’s why they contain the same value of counter.

And I don't think declaring static variables will change your variable type as you mentioned - from double to String.

Example #2: The static variable is used to refer the common property of all objects (that is not unique for each object). For instance, college name, student names, ect ... It takes memory only one time in class area at the moment of class loading. The benefit of using static variable is to make program memory efficient to a kind of saving memory.

 class Person{  
       int id; 
       String studentName;  
       // a static variable 
       static String collegeName ="MBA";  

       Person(int i,String n)
       {  
        id = i;  
        studentName = n;  
       }  

        void output ()
        {
         System.out.println(id +" "+ studentName +" "+ collegeName);
        }  

     public static void main(String args[])
       {  
        Person student1 = new Person(100,"Joe");  
        Person student2 = new Person(200,"Jef");  

        student1.output();
        student2.output(); 
       }  
    }  

Display:
     100 Joe MBA
     200 Jef MBA

Notice that the same static value MBA is assigned for all instantiated objects of the class.

Linux Le
  • 446
  • 1
  • 4
  • 8