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

    Default Need help with an ArrayList please

    Need to calculate the GPA and I am receiving an error. I am supposed to create a new class Courses to create an object that holds the credits and the grades but I do not know how to implement this so I am trying to bypass it. Here it was I have so far:

    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;
    		private int i;
       
       	
    
          public GPA()
          {
             points = 0;
             credits = 0;
             totalCredits = 0;
             totalPoints = 0;
             grade = "";
          }
       
          public GPA(int courses)
          {
             numCourses = 0;
             maxCourses = courses;
             points = 0;
             credits = 0;
             totalCredits = 0;
             totalPoints = 0;
             grade = "";
          }
    public void addCourse(int cr, String gr)
          {	
             
    			ArrayList<Integer> sumCredits = new ArrayList<Integer>(maxCourses);
          	ArrayList<Integer> sumPoints = new ArrayList<Integer>(maxCourses);
    			
    			sumCredits.add(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.add(points);
          	
             totalCredits = totalCredits + sumCredits.get(numCourses); 
             
             totalPoints = totalPoints + (sumPoints.get(numCourses)*sumCredits.get(numCourses));
    			numCourses++;
          
          }
       
          public double calcGPA()
          {
    			 
             gpa = totalPoints/totalCredits;
             return gpa;
          }
    
    
    
    
    import java.util.*;
    
    public class Course
    {
    	private int credits;
    	private String grade;
    	
    	public Course(int cr, String gr)
    	{
    		credits = cr;
    		grade = gr;
    	}
    	
    	public String getGrade()
    	{
    		return grade;
    	}
    	
    	public int getCredits()
    	{
    		return credits;
    	}
    }
    
    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
    }
    This is my error:

    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
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at GPA.addCourse(GPA.java:103)
    at GpaTestEx2.main(GpaTestEx2.java:38)

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Need help with an ArrayList please

    Index in an ArrayList object start from 0. The IndexOutOfBoundException means you have access an index outside the size of your current array list. If you have list that contains a single object then it will stored in the 0 index.

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. Replies: 1
    Last Post: 05-15-2011, 06:07 PM
  3. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 06:07 AM
  4. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 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
  •