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!
Code :
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");
}
}
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.
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
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.
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.
Code :
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));
}
}
Re: Hello Everyone. Need Help if possible
I am taking snippets from your code that are relavant.
Code Java:
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
you're creating grade again, new. You've already said grade is a String, so you don't have to say it again.
Code Java:
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.
Re: Hello Everyone. Need Help if possible
Quote:
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:
Code :
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.
Quote:
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.