What's wrong with this Java?

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

What's wrong with this Java?

Post by Xaos »

Code: Select all

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gpa.calculator;
import java.util.*;

public class GPA {
    
  private static double[] grades = new double[32];
  private static double average;
  private static double fourPoint;
  private static double totalGrade;
  private static int counter;
  private static double fourpointAverage; 
  
    public static double Calc(double grade){
        double totalGrade =+ grade; 
        return totalGrade;
    }
    
    public static double findAverage(double totalGrade){
        double average = totalGrade/counter;
        return average;
    }
    
    public static double fourpointScale(double grade){
       double fourPoint = 0;
        if(grade >= 90){
            fourPoint =+ 4.0;
        }else if(grade >= 80){
            fourPoint =+ 3.0;
        }else if(grade >= 70){
            fourPoint =+ 2.0;
        }else if(grade >= 60){
            fourPoint =+ 1.0;
        }
      
        return fourPoint;
        }
    
    public static double fourpointAverage(double fourPoint){
        double fourpointAverage = fourPoint / counter;
      return fourpointAverage;
      
    }
    
    public static void main(String[] args){
        
        
        Scanner input = new Scanner(System.in);
     
        for(int i=0;i<=31;i++){
            System.out.println("Enter Grade:");
            grades[i] = input.nextDouble();
        }
            for(int i=0;i<=31;i++){
                if(grades[i] != 0){
                   double four = fourpointScale(grades[i]);
                   double overall = Calc(grades[i]);
                   counter =+ 1;
                   
                }
                
            }
           
            findAverage(totalGrade);
            fourpointAverage(fourPoint);
            System.out.println("Your GPA Is:" + fourpointAverage + " And " + average);
        }
        
                }
So I'm learning Java, and this is an example problem I'm working on. Just simple, not optimized GPA calculator. Sorry for no comments, I thought it was gonna work :oops: Basically, it takes 32 (max number of classes at my high school) classes, assigns all of the answers to grades[], sends grades[] through a loop, check if any of them are 0, takes all the grades that don't equal zero and pass them trhough the fourpointScale and Calc methods, then take the totalGrade and fourPoint values from those methods and put them in findAverage and fourpointAverage and it tells you what your number average is (1-100 kind of thing) and 4 point scale average (0.0-4.0). Unfortunately, regardless of what I put in, the output is "Your GPA is 0.0 and 0.0"

Help on the problem and general things I missed/should of done to make it easier.

P.S. if this was for real, I would go through and allow the user an "exit" kind of thing to not have to input 32 classes, or input more if they wanted.
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: What's wrong with this Java?

Post by Jackolantern »

You are shadowing all of your variables in a lower scope. What that means is this:

You have defined your fields here:

Code: Select all

private static double[] grades = new double[32];
  private static double average;
  private static double fourPoint;
  private static double totalGrade;
  private static int counter;
  private static double fourpointAverage; 
But then when you use them, you are including their types again:

Code: Select all

public static double fourpointAverage(double fourPoint){
        double fourpointAverage = fourPoint / counter;
        return fourpointAverage;
      
    }
...this is creating a new variable inside your method that is "hiding" the field. So instead of assigning to your field you defined at the top of the class, you are assigning to a new variable you just created, and then returning that. So far not too bad...

But then here you are creating a new variable to hold the return of that method, and you don't do anything with it:

Code: Select all

for(int i=0;i<=31;i++){
                if(grades[i] != 0){
                   double four = fourpointScale(grades[i]);
                   double overall = Calc(grades[i]);
                   counter =+ 1;
                   
                }
                
}
The way your code is setup, it seems like you want your methods to make direct changes to the class fields.

You may still need some logical reconstruction, but just remember that when you want to assign a value to field, it is:

Code: Select all

fourpointAverage = fourPoint / counter;
Not:

Code: Select all

double fourpointAverage = fourPoint / counter;
...because the 2nd one is creating a new variable in a different scope and is hiding the field (also called "shadowing").
The indelible lord of tl;dr
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

Re: What's wrong with this Java?

Post by Xaos »

Alright so I made a bunch of modifications, but I still get an error. For four 100s, I get "Your GPA is 0.0 and 12.90...." Any help?

Code: Select all

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gpa.calculator;
import java.util.*;

public class GPA {
    
  private static final double[] grades = new double[32];
  private static double totalGrade = 0; 
  
    public static double Calc(double grade){
        totalGrade += grade;
      return totalGrade;
        
    }
    
    public static double fourpointScale(double grade, double fourPoint){
        if(grade >= 90){
            fourPoint += 4.0;
        }else if(grade >= 80){
            fourPoint += 3.0;
        }else if(grade >= 70){
           fourPoint += 2.0;
        }else if(grade >= 60){
            fourPoint += 1.0;
        }
        return fourPoint;
        }
    
    public static void main(String[] args){
        int counter = 0; 
        double fourPoint = 0;
        double fourpointValue = 0;
        double calcValue = 0;
        double count = 0; 
        Scanner input = new Scanner(System.in);
     
        for(int i=0 ;i < grades.length ; i++){
            System.out.println("Enter Grade:");
            grades[i] = input.nextDouble();
            if(grades[i] == 0){
                break;
            }
        }
          for(int i=0; i < grades.length ; i++){
                   fourpointValue = fourpointScale(grades[i],fourPoint);
                   calcValue = Calc(grades[i]);
                   count = counter++; 
                   
            }
          double fourFinal = fourpointValue / count;
          double calcFinal = calcValue / count;
          
            System.out.println("Your GPA Is:" + fourFinal + " And " + calcFinal);
        }
        
                }

User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: What's wrong with this Java?

Post by Jackolantern »

When this iterates, you are over-writing the values you collect every time, so when you divide them by count, you are only working on the last grade:

Code: Select all

for(int i=0; i < grades.length ; i++){
                   fourpointValue = fourpointScale(grades[i],fourPoint);
                   calcValue = Calc(grades[i]);
                   count = counter++;

            }
You should probably turn them into fields so you can keep a running value in them.
The indelible lord of tl;dr
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

Re: What's wrong with this Java?

Post by Xaos »

Code: Select all

package gpa.calculator;
import java.util.*;

public class GPA {
    
  private static final double[] grades = new double[32];
  
    public static double Calc(double grade, double totalGrade){
        totalGrade += grade;
      return totalGrade;
        
    }
    
    public static double fourpointScale(double grade, double fourPoint){
        if(grade >= 90){
            fourPoint += 4.0;
        }else if(grade >= 80){
            fourPoint += 3.0;
        }else if(grade >= 70){
           fourPoint += 2.0;
        }else if(grade >= 60){
            fourPoint += 1.0;
        }
        return fourPoint;
        }
    
    public static void main(String[] args){
        int counter = 1; 
        double fourPoint = 0;
        double fourpointValue = 0;
        double calcValue = 0;
        double count = 1; 
        double totalGrade = 0;
        
        Scanner input = new Scanner(System.in);
        System.out.println("The max amount of classes this calculator holds is 32. To calculate your GPA before going to 32, simply enter '0' as a grade.");
        
        for(int i=0 ;i < grades.length ; i++){
            count = counter++;
            System.out.println("Enter Grade:");
            grades[i] = input.nextDouble();            
            if(grades[i] == 0){
                break;
            }
        }
          for(int i=0; i < grades.length ; i++){
                   fourpointValue += fourpointScale(grades[i],fourPoint);
                   calcValue += Calc(grades[i],totalGrade);
                   
                   
            }
          double fourFinal = fourpointValue / count;
          double calcFinal = calcValue / count;
          
            System.out.println("Your GPA Is:" + fourFinal + " And " + calcFinal);
        }
        
                }
Tis workin ! :D
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: What's wrong with this Java?

Post by Jackolantern »

Awesome! :)
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”