Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By ndsmith20
  • 1 Post By ndsmith20

Thread: Really need help with an Array problem

  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Really need help with an Array problem

    Need to get a GPA from the amount of courses I enter

    GPA class:
    Java Code:
     import java.util.*;
    
       public class GPA
       {
          private int points;
          private double credits;
          private String grade;
          private double totalPoints;
          private double totalCredits;
          private double gpa;
          private int numCourses;
          private int maxCourses;
       
       	
    
          public GPA()
          {
             points = 0;
             credits = 0;
             totalCredits = 0;
             totalPoints = 0;
             grade = "";
          }
       
          public GPA(int courses)
          {
             numCourses = courses;
             maxCourses = numCourses;
             points = 0;
             credits = 0;
             totalCredits = 0;
             totalPoints = 0;
             grade = "";
          }
       
    public void addCourse(int cr, String gr)
          {	
             int i = 0;
    			int [] sumCredits = new int [numCourses];
          	int [] sumPoints = new int [numCourses];
                  do{
             sumCredits[i] = cr;
             grade = gr;
             if((grade.equals("A"))||(grade.equals("a")))
             {
                points = 4;
             }
             else if((grade.equals("B"))||(grade.equals("b")))
             {
                points = 3;
             }
             else if((grade.equals("C"))||(grade.equals("c")))
             {
                points = 2;
             }
             else if((grade.equals("D"))||(grade.equals("d")))
             {
                points = 1;
             }
             else if((grade.equals("F"))||(grade.equals("f")))
             {
                points = 0;
             }
             else
             {
                points = -1;
             }
             sumPoints[i] = points;
    			i++;
          	
                     
          	}
    			while(i < numCourses);
    			for (int k = 0; k < sumCredits.length; k++) 
             { 
                totalCredits += sumCredits[k]; 
             } 
             for(int h = 0; h < sumPoints.length; h++)
             {
                totalPoints += sumPoints[h];
             }
    
          
          }
       
          public double calcGPA()
          {
    			 
             gpa = totalPoints/totalCredits;
             return gpa;
          }
    Java Code:
    import java.util.Scanner;
    
    public class GpaTestEx2
    {
    
    	public static void main (String[] args)
    	{
    		//declarations
    		Scanner in = new Scanner(System.in); //input object
    		int numCourses;	//number of courses - can be changed
    		int credits;		//number of credits for a course
    		String grade;		//grade for course
    		
    		
    		//read in number of courses
    		System.out.print("Enter number of courses: ");
    		numCourses = in.nextInt();
    		
    		//create Gpa object to hold specified number of courses
    		GPA myGPA = new GPA(numCourses);
    				
    		//read in all courses and add course information to Gpa object
    		for (int k=0; k<numCourses; k++)
    		{
    			System.out.print("Enter credits for course " + (k+1) + ": ");
    			credits = in.nextInt();
    			System.out.print("Enter grade for course " + (k+1) + ": ");
    			grade = in.next();
    			
    			myGPA.addCourse(credits, grade);
    		}
    		
    		//print results
    		System.out.println();
    		System.out.printf("GPA is %4.2f%n", myGPA.calcGPA( )); 
    
    	} //end main
    }
    Enter number of courses: 2
    Enter credits for course 1: 4
    Enter grade for course 1: a
    Enter credits for course 2: 3
    Enter grade for course 2: b

    GPA is 1.00


    I keep on getting a GPA of 1.00 back instead of 3.57

  2. #2
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Really need help with an Array problem

    I think the issue is when i enter the data it reset i to 0 everytime just replacing the info in slot 0 of my array...is there any way to fix this?

  3. #3
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Really need help with an Array problem

    how do you calculate GPA?what's the formula?
    because, when i use the test data you've given it tells me that totalPoints is equal to seven and totalCredits is also equal to seven.

    try rewriting the addCourse() method and make it shorter because that's where the confusion lies

  4. #4
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Really need help with an Array problem

    I see what your saying...i have a mistake in my adding of the points...GPA is the sum of points divided by sum of credits. Sum of points is credits for that course times points for that course(the part i forgot to put in my program)
    Kagiso likes this.

  5. #5
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Really need help with an Array problem

    Its working now...i dont know why i didnt see that...thank you
    Kagiso likes this.

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 09:39 PM
  2. array problem
    By surgeon in forum New To Java
    Replies: 3
    Last Post: 05-16-2012, 08:57 PM
  3. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  4. array problem
    By jabo in forum New To Java
    Replies: 2
    Last Post: 03-31-2010, 09:54 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 01:13 AM

Posting Permissions

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