CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2010
    Location
    Canada
    Posts
    3

    Exclamation Need help with this simple Java code

    Hello.

    Taking a Java course, but I have been stuck on this code for over a week, and its already due. My problem is, is that I have declared global variables, but the local variables are never read. The program is designed to take course hours and grade and spit out the GPA.

    Input should be:


    LAB07 BASIC VERSION


    Enter course 1 Grade ===>> A
    enter course 1 Hours ===>> 4
    Enter course 2 Grade ===>> A
    enter course 2 Hours ===>> 3
    Enter course 3 Grade ===>> A
    enter course 3 Hours ===>> 2
    Enter course 4 Grade ===>> A
    enter course 4 Hours ===>> 1

    Course1 Grade: A Course1 Credit Hours: 4
    Course2 Grade: A Course2 Credit Hours: 3
    Course3 Grade: A Course3 Credit Hours: 2
    Course4 Grade: A Course4 Credit Hours: 1

    Current GPA: 4.0

    Everything current works, except for the "Current GPA: 0".. It needs to have "4.0" and have all variables read.



    Current code is below:




    //CODE BEGINS

    import java.util.*;

    public class Lab07
    {

    static String lGrade1, lGrade2, lGrade3, lGrade4;
    static int cHours1,cHours2,cHours3,cHours4;
    static int cVal1, cVal2, cVal3, cVal4;
    static int gradeValue, courseValue;
    static int gVal1, gVal2, gVal3, gVal4;
    static int value;
    static int gpa;
    static String dummy;


    public static void main (String args[])
    {
    System.out.println("\nLAB07 BASIC VERSION\n\n");
    enterData();
    computeGPA();
    displayData();
    gradeValue();
    courseValue();
    getGPA();
    }




    public static void enterData()
    {
    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();
    }
    public static void computeGPA()
    {

    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;
    }
    }

    public static void gradeValue()
    {
    int gVal4 = value;
    }

    public static void courseValue()
    {
    int cVal1 = gVal1 * cHours1;
    int cVal2 = gVal2 * cHours2;
    int cVal3 = gVal3 * cHours3;
    int cVal4 = gVal4 * cHours4;

    double totalValue = cVal1 + cVal2 + cVal3 + cVal4;
    double totalHours = cHours1 + cHours2 + cHours3 + cHours4;
    double gpa = totalValue / totalHours;
    }

    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();
    }

    public static void getGPA()
    {
    System.out.println("Current GPA: " + gpa);
    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need help with this simple Java code

    If you declare local variables with the same name as class variables you will invariably end up assigning a value to the wrong variable. Change the names of your local variables to unique names and you will see which variables you are assigning which value to.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Nov 2010
    Location
    Canada
    Posts
    3

    Question Re: Need help with this simple Java code

    Quote Originally Posted by keang View Post
    If you declare local variables with the same name as class variables you will invariably end up assigning a value to the wrong variable. Change the names of your local variables to unique names and you will see which variables you are assigning which value to.
    Thanks for the quick reply. Unfortunately, I can not change the names of my local variables. They have to remain those names by instructions from my course.

    Any further advise?

    Thanks again!

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Need help with this simple Java code

    Unfortunately, I can not change the names of my local variables. They have to remain those names by instructions from my course.
    Change the names of the class variables then.

    It's generally considered bad practice to use the same names for variables with overlapping scope. So much so that many IDE's warn you of the danger when you do this.

    If your tutor has really specified that you must have class and local variables of the same name then the point of the exercise may be to show you how confusing it is. If not, find yourself another tutor.

    If you still have to do this with the existing names then you need to look at every use of your local variables and see which of the two variables (ie local or class) you are using. If there is ambiguity over which one to use Java will always use the variable with the most local scope. For instance in method computeGPA() all your uses of 'value' will be the local variable and not the class variable. If you want to use the class variable you have to explicitly state that by using the class name ie Lab07.value
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Nov 2010
    Location
    Canada
    Posts
    3

    Smile Re: Need help with this simple Java code

    Thank you very much. I will consult with my tutor and continue to look over my code, and go from there.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured