Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Hello Everyone. Need Help if possible

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hello Everyone. Need Help if possible

    Hi guys, im new to the forums and was wondering if anyone could help me. Ive been working on this project for a few hours now and im starting to pull my hair out. Basically its the program that uses methods to calculate GPA. I dont know how to pass values from my main method into my other methods. any help would be appreciated!


    import javax.swing.JOptionPane;
     
    public class proj2a{
        public static void main(String[] args){
     
              int totalQualityPoints = 0, totalCredits = 0;
              int credits, gradeNumber;
              String Name, semester, course, Exit, grade;
              double GPA;
     
               PrintExplanation();
               Name();
               semesterName();
               do{
               String courseName = courseName();
               String courseGrade = courseGrade();
               GetValidNumberInput("How many credit Hours is the class Worth?" ,1,4);
               ConvertGradeToPoints(grade);
               CalculateGPA();
     
               JOptionPane.showInputDialog("Do you want to exit? 'yes/no'");
              }while (!Exit.equals("yes"));
               GenerateGPAReport();
     
        }
            public static void PrintExplanation(){
                JOptionPane.showMessageDialog(null,"This Program will help you calculate your GPA"+"\n"+"In order for this program to work correctly, you need to follow exactly what the pop up dialogs say.");
        }
        public static String Name(){
            String Name;
            Name = JOptionPane.showInputDialog("So, What is your name?");
     
            return Name;
        }
     
     
         public static String semesterName(){
                String semester;
     
                semester = JOptionPane.showInputDialog("What semester are you currently in?");
     
                return semester;
        }
     
         public static String courseName(){
               String course;
     
               course = JOptionPane.showInputDialog("What class are you currently in?");
     
               return course;
     
        }
        public static String courseGrade(){
               String grade;
     
               grade = JOptionPane.showInputDialog(",What grade did you get int this class?");
     
               return grade;
     
        }
         public static int GetValidNumberInput(String promptstr, int lowerNum, int upperNum){
     
              int credits;
     
              do{
     
              credits = Integer.parseInt(JOptionPane.showInputDialog(promptstr));
     
                         }while(credits < lowerNum || credits > upperNum);
     
              return credits;
     
         }
         public static int ConvertGradeToPoints(String grade){
                    int gradenumber;
     
                    if (grade.equalsIgnoreCase("A")){
                    gradeNumber = 4 ;
                    }
                    else if (grade.equalsIgnoreCase("B")){
                     gradeNumber = 3;
                    }
                    else if (grade.equalsIgnoreCase("C")) {
                     gradeNumber = 2;
                    }
                    else if (grade.equalsIgnoreCase("D")){
                     gradeNumber = 1;
                    }
                    else{
                     gradeNumber = 0;
                    }
                    return gradeNumber;
         }
        public static double CalculateGPA (int totalCredits, int totalQualityPoints){
     
           totalQualityPoints = credits * gradeNumber;
           GPA =   totalQualityPoints / totalCredits;
     
          return GPA;
     
       }
        public static void GenerateGPAReport(int totalcredits, int totalQualityPoints, String Name, String semester){
     
            System.out.println("Name:"+Name);
            System.out.println("Semester:"+semester);
            System.out.println("\n");
     
     
    }
    }

  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Hello Everyone. Need Help if possible

    You are passing values from your main method to other methods. What is the problem exactly? Where is it, which line of code? Which method are you calling, if any compile time rrors are being made, what are they? Same with runtime errors/exceptions.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hello Everyone. Need Help if possible

    When i send them from my main method to my "sub methods(?)" it say's that the variables are missing. When i define them, it say's they might have not initialized this is mostly happening on my convertGradeToPoints() method

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Hello Everyone. Need Help if possible

    Look at my post about initializing variables, there is a link below. It applies to do-while statements as well

    Initalizing Variables by TJStretch

    EDIT

    Looking at your code, you never initialize grade at all.
    Last edited by Tjstretch; November 15th, 2011 at 03:47 AM.

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Hello Everyone. Need Help if possible

    Additional to Tjstretch post. A small example demonstrating you passing values. Well, you also can read through internet, but i don't know why many people hesitate googling things.
    public class Test{
         public int aFunc(int a, int b){
                          return a+b;
               }
     
         public static void main(String args[]){
                       int a = 5;
                       int b = 1;
                       System.out.println(aFunc(a,b));
                   }
       }

  6. #6
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Hello Everyone. Need Help if possible

    I am taking snippets from your code that are relavant.

    String grade; //initialize variable in main method...but what are you initializing it to? It's null...
    ConvertGradeToPoints(grade); // you're calling method and passing in value grade correctly
     
    public static String courseGrade(){
               String grade; }
    you're creating grade again, new. You've already said grade is a String, so you don't have to say it again.

     
    String courseGrade = courseGrade();

    This is where you call the courseGrade() method from main.
    I assume you meant to assign this value to grade, not create another variable that is never used.
    Last edited by Herah; November 15th, 2011 at 09:23 PM.

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Hello Everyone. Need Help if possible

    you're creating grade again, new. You've already said grade is a String, so you don't have to say it again.
    Do you understand the difference between scope and life time of variables?
    Example:
    public int func(int a, int b){
              int x=0;
              int y=9;
              return (x+y);
              }
    public static void main(String args[]){
            int x=3;
            int y=5;
            System.out.println(func(x,y));
     }
    What do you think, above code will do?
    In the main function, x and y has their own scope. These x and y are only known by main function here.
    While in func(int a, int b), the x and y are only known by func(). So, don't mistreat them.
    Hope it might help.
    This is where you call the courseGrade() method from main.
    I assume you meant to assign this value to grade, not create another variable that is never used.
    He just meant to demonstrate everything. It depends upon your problem description and your solution towards it.