Question for anyone who's taken AP Computer Science (or knows any computer science)

<p>Ok, so I just started taking AP Computer Science A online 'cause my school doesn’t offer it, but I’m having a problem with one of the labs and I can’t seem to get a hold of my teacher, soooooo… I’ve come here!!</p>

<p>Ok, so I’m trying to write this program to calculate GPA and when I enter all "A"s, the GPA comes out to 0.0… haha, so evidently I’m doing something wrong… I’m new to this… help?? Thanks</p>

<p>import java.util.*;</p>

<p>public class Lab07 {</p>

<pre><code>static String lGrade1,lGrade2,lGrade3,lGrade4;
static String dummy;
static int cHours1,cHours2,cHours3,cHours4;
static int gVal1;
static int gVal2;
static int gVal3;
static int gVal4;
static int cVal1;
static int cVal2;
static int cVal3;
static int cVal4;
static double gpa;

public static void main (String args) {
System.out.println("
LAB07 BASIC VERSION

");
enterData();
computeGPA();
displayData();
}

public static void enterData()
{

Scanner in = new Scanner(System.in);    
System.out.print("Enter course 1 Grade  ===&gt;&gt;  ");
lGrade1 = in.nextLine();
System.out.print("enter course 1 Hours  ===&gt;&gt;  ");
cHours1 = in.nextInt(); dummy = in.nextLine();
System.out.print("Enter course 2 Grade  ===&gt;&gt;  ");
lGrade2 = in.nextLine(); 
System.out.print("enter course 2 Hours  ===&gt;&gt;  ");
cHours2 = in.nextInt(); dummy = in.nextLine();
System.out.print("Enter course 3 Grade  ===&gt;&gt;  ");
lGrade3 = in.nextLine(); 
System.out.print("enter course 3 Hours  ===&gt;&gt;  ");
cHours3 = in.nextInt(); dummy = in.nextLine();
System.out.print("Enter course 4 Grade  ===&gt;&gt;  ");
lGrade4 = in.nextLine(); 
System.out.print("enter course 4 Hours  ===&gt;&gt;  ");
cHours4 = in.nextInt(); dummy = in.nextLine();

}

public static void gradeValue()
{int value = 0;

char lg1 = lGrade1.charAt(0);
switch(lg1)
{
case ‘A’: value = 4; break;
case ‘B’: value = 3; break;
case ‘C’: value = 2; break;
case ‘D’: value = 1; break;
case ‘F’: value = 0; break;
}
int gVal1 = value;

char lg2 = lGrade2.charAt(0);
switch(lg2)
{
case ‘A’: value = 4; break;
case ‘B’: value = 3; break;
case ‘C’: value = 2; break;
case ‘D’: value = 1; break;
case ‘F’: value = 0; break;
}
int gVal2 = value;

char lg3 = lGrade3.charAt(0);
switch(lg3)
{
case ‘A’: value = 4; break;
case ‘B’: value = 3; break;
case ‘C’: value = 2; break;
case ‘D’: value = 1; break;
case ‘F’: value = 0; break;
}
int gVal3 = value;

char lg4 = lGrade4.charAt(0);
switch(lg4)
{
case ‘A’: value = 4; break;
case ‘B’: value = 3; break;
case ‘C’: value = 2; break;
case ‘D’: value = 1; break;
case ‘F’: value = 0; break;
}
int gVal4 = value;
}
public static void courseValue()
{
int cVal1 = gVal1 * cHours1;
int cVal2 = gVal2 * cHours2;
int cVal3 = gVal3 * cHours3;
int cVal4 = gVal4 * cHours4;
}
public static void getGPA()
{
double totalValue = cVal1 + cVal2 + cVal3 + cVal4;
double totalHours = cHours1 + cHours2 + cHours3 + cHours4;
double gpa = totalValue / totalHours;
}
public static void computeGPA()
{ getGPA();
}
public static void displayData()
{
System.out.println();
System.out.println("Course1 Grade: " + lGrade1 + " Course1 Credit Hours: " + cHours1);
System.out.println("Course2 Grade: " + lGrade2 + " Course2 Credit Hours: " + cHours2);
System.out.println("Course3 Grade: " + lGrade3 + " Course3 Credit Hours: " + cHours3);
System.out.println("Course4 Grade: " + lGrade4 + " Course4 Credit Hours: " + cHours4);
System.out.println();
System.out.println("Current GPA: " + gpa);
}
</code></pre>

<p>}</p>

<p>yeahh…sorry for the long post =P</p>

<p>hmm…this looks interesting. I should self study AP comp. sci. this upcoming year…but i probably won’t have time to.</p>

<p>I wasn’t particularly good at this, so take this with a grain of salt.</p>

<p>First off, why are all your int values static, like static int gVal1;? Static is usually used for permanent class methods (where only a single copy of that class is needed, like the Math class).</p>

<p>Second, * public static void gradeValue() * was never called, so your letter grades are never converted to values. Therefore, gVal1, 2, 3, and 4 is storing their original values from when they had been initialized, which is 0.</p>

<p>Your * public static void courseValue() * was never called either. So cVal1, 2, 3, and 4 are also the value they were when you initialized them, which is 0.</p>

<p>Therefore, when you call * public static void getGPA() * from computeGPA, the total value would be 0.</p>

<p>double gpa = totalValue / totalHours;
double gpa = 0 / totalHours = 0.</p>

<p>So when you call displayData, it displays gpa, which is 0.</p>

<hr>

<p>I hope that made sense.</p>

<p>I don’t have a Java compiler on this computer, so I can’t test it out for you, but this is what I would try:</p>

<p>Change:
public static void computeGPA()
{ getGPA();
}</p>

<p>to:
public static void computeGPA()
{ gradeValue();
courseValue();
getGPA();
}</p>

<p>I hope this works for you.</p>

<p>I put BrightRed’s suggestion in the compiler and it still doesn’t work.
But I figured out what’s wrong: </p>

<p>When you define static values at the top of the program, there is no need to redefine them within the methods.
i.e. it is correct to say </p>

<p>static int gVal1; </p>

<p>provided that you don’t go inside a method and say </p>

<p>int gVal1 = value;</p>

<p>since you need not rewrite “int.” Does this make sense? Basically, here is a corrected version. Also, two of your methods were not implemented at all (courseValue() and gradeValue()) so I put them in appropriate places, but you can fit them wherever you want. Here it is: </p>

<p>import java.util.*;</p>

<p>public class Lab07 {</p>

<p>static String lGrade1,lGrade2,lGrade3,lGrade4;
static String dummy;
static int cHours1,cHours2,cHours3,cHours4;
static int gVal1;
static int gVal2;
static int gVal3;
static int gVal4;
static int cVal1;
static int cVal2;
static int cVal3;
static int cVal4;
static double gpa;
static double totalValue, totalHours; </p>

<p>public static void main (String args) {
System.out.println("
LAB07 BASIC VERSION

");
enterData();
courseValue();
computeGPA();
displayData();
}</p>

<p>public static void enterData()
{</p>

<p>Scanner in = new Scanner(System.in);
System.out.print("Enter course 1 Grade ===>> ");
lGrade1 = in.nextLine();
System.out.print("enter course 1 Hours ===>> ");
cHours1 = in.nextInt(); dummy = in.nextLine();
System.out.print("Enter course 2 Grade ===>> ");
lGrade2 = in.nextLine();
System.out.print("enter course 2 Hours ===>> ");
cHours2 = in.nextInt(); dummy = in.nextLine();
System.out.print("Enter course 3 Grade ===>> ");
lGrade3 = in.nextLine();
System.out.print("enter course 3 Hours ===>> ");
cHours3 = in.nextInt(); dummy = in.nextLine();
System.out.print("Enter course 4 Grade ===>> ");
lGrade4 = in.nextLine();
System.out.print("enter course 4 Hours ===>> ");
cHours4 = in.nextInt(); dummy = in.nextLine();
gradeValue();
}</p>

<p>public static void gradeValue()
{int value = 0;</p>

<p>char lg1 = lGrade1.charAt(0);
switch(lg1)
{
case ‘A’: value = 4; break;
case ‘B’: value = 3; break;
case ‘C’: value = 2; break;
case ‘D’: value = 1; break;
case ‘F’: value = 0; break;
}
gVal1 = value;</p>

<p>char lg2 = lGrade2.charAt(0);
switch(lg2)
{
case ‘A’: value = 4; break;
case ‘B’: value = 3; break;
case ‘C’: value = 2; break;
case ‘D’: value = 1; break;
case ‘F’: value = 0; break;
}
gVal2 = value;</p>

<p>char lg3 = lGrade3.charAt(0);
switch(lg3)
{
case ‘A’: value = 4; break;
case ‘B’: value = 3; break;
case ‘C’: value = 2; break;
case ‘D’: value = 1; break;
case ‘F’: value = 0; break;
}
gVal3 = value;</p>

<p>char lg4 = lGrade4.charAt(0);
switch(lg4)
{
case ‘A’: value = 4; break;
case ‘B’: value = 3; break;
case ‘C’: value = 2; break;
case ‘D’: value = 1; break;
case ‘F’: value = 0; break;
}
gVal4 = value;
}
public static void courseValue()
{
cVal1 = gVal1 * cHours1;
cVal2 = gVal2 * cHours2;
cVal3 = gVal3 * cHours3;
cVal4 = gVal4 * cHours4;</p>

<p>}
public static void getGPA()
{
totalValue = cVal1 + cVal2 + cVal3 + cVal4;
totalHours = cHours1 + cHours2 + cHours3 + cHours4;
gpa = totalValue / totalHours;
}
public static void computeGPA()
{ getGPA();
}
public static void displayData()
{
System.out.println();
System.out.println("Course1 Grade: " + lGrade1 + " Course1 Credit Hours: " + cHours1);
System.out.println("Course2 Grade: " + lGrade2 + " Course2 Credit Hours: " + cHours2);
System.out.println("Course3 Grade: " + lGrade3 + " Course3 Credit Hours: " + cHours3);
System.out.println("Course4 Grade: " + lGrade4 + " Course4 Credit Hours: " + cHours4);
System.out.println();
System.out.println("Current GPA: " + gpa);</p>

<p>}</p>

<p>}</p>

<p>Your variables are declared int’s inside the method, which means that the variable will only contain the proper value inside the method. Outside the specific method, the variable will be 0.</p>

<p>Wouldn’t it make more sense to create a class that took in all these values and hours and just pass it around and use methods to output the grade and such? …that would make the most sense. Like report card class or something and just have 7 fields or something with 7 grades and 7 hours…? This seems way too VisualBasic-ified (in terms of the massive amounts of lines you’re having to write)</p>

<p>ahhh… ok. that makes soooo much more sense now… thanks! so I was just messed up with how I declared variables and such, right?</p>