Results 1 to 7 of 7
  1. #1
    ahernandez980 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Exclamation Need help with program

    My group and I are fairly new to Java. We are creating a transcript system in which a user is able to retrieve their transcript based on a studentID.

    Here are the classes:

    Java Code:
    /**
     * Final Group Project
     */
    package finalproject;
    
    /**
     * A class that retrieves a letter grade for a course.
     * 
     * @author Scott Renaud
     * @version 1.1
     */
    public class Grade 
    {
    	/** A grade of A */
    	private final static int A = 1;
    	
    	/** A grade of B */
    	private final static int B = 2;
    	
    	/** A grade of C */
    	private final static int C = 3;
    	
    	/** A grade of D */
    	private final static int D = 4;
    	
    	/** A grade of F */
    	private final static int F = 5;
    	
    	/** The numerical grade */
    	private double gpa;
    	
    	/** The letter grade */
    	private String grade;
    	
    	/**
    	 * Constructs a new Grade object.
    	 * 
    	 * @param numGrade The numerical grade associated with a course.
    	 */
    	public Grade(String grade) 
    	{
    		this.grade = grade;
    		checkCourseCompletion();
    	}
    	
    	/**
    	 * Returns the letter grade for a course.
    	 * 
    	 * @return A String literal specifying the letter grade.
    	 */
    	public double getGPA()
    	{
    		return this.gpa;
    	}
    	
    	/**
    	 * Checks if course has a grade associated with it.
    	 */
    	public void checkCourseCompletion()
    	{
    		if(!(this.grade == null));
    			findGPA();
    	}
    	
    	/**
    	 * Finds the GPA based on the letter grade.
    	 */
    	public void findGPA()
    	{
    		int key = 0;
    		
    		if(this.grade.equals("A"))
    			key = A;
    		else if(this.grade.equals("B"))
    			key = B;
    		else if(this.grade.equals("C"))
    			key = C;
    		else if(this.grade.equals("D"))
    			key = D;
    		else if(this.grade.equals("F"))
    			key = F;
    		
    		switch(key)
    		{
    		case A:
    			this.gpa = 4.0;
    		case B:
    			this.gpa = 3.0;
    		case C:
    			this.gpa = 2.0;
    		case D:
    			this.gpa = 1.0;
    		case F:
    			this.gpa = 0.0;
    		}
    	}
    	
    	/**
    	 * Returns the GPA associated with a course.
    	 */
    	public String toString()
    	{
    		return "Course GPA: " + gpa;
    	}
    	
    	/**
    	 * Compares two letter grades.
    	 * If not the same, returns false; else true.
    	 * 
    	 * @param obj An Object being compared to Grade.
    	 * @return A boolean response of either true or false.
    	 */
    	public boolean equals(Object obj)
    	{
    		if (!(obj instanceof Grade))
    			return false;
    		Grade otherGrade = (Grade) obj;
    		return gpa == otherGrade.gpa;
    	}
    }
    Java Code:
    /**
     * Final Group Project
     */
    package finalproject;
    
    /**
     * A class that determines the properties of a course.
     * 
     * @author Mike Kuehnle
     * @version 1.1
     */
    public class Course 
    {
    	/** The course number */
    	private String courseNum;
    	
    	/** The course name */
    	private String courseName;
    	
    	/** The course credit */
    	private double courseCredit;
    	
    	/** The course grade */
    	private String courseGrade;
    	
    	/** The course GPA */
    	private double courseGPA;
    	
    	/** The Grade object */
    	private Grade grade;
    	
    	/**
    	 * Constructs a new course.
    	 * 
    	 * @param crsNum The course number.
    	 * @param crsName The course name.
    	 * @param crsCredit The number of credits associated with a course.
    	 * @param crsNumGrade The numerical grade earned in a course.
    	 */
    	public Course(String crsNum, String crsName, 
    			String crsCredit, String crsGrade) 
    	{
    		courseNum = crsNum;
    		courseName = crsName;
    		courseCredit = Double.parseDouble(crsCredit);
    		courseGrade = crsGrade;
    		grade = new Grade(courseGrade);
    		courseGPA = grade.getGPA();
    	}
    	
    	/**
    	 * Returns the course number.
    	 * 
    	 * @return A String literal specifying the course number.
    	 */
    	public String getCourseNumber()
    	{
    		return courseNum;
    	}
    	
    	/**
    	 * Returns the course name.
    	 * 
    	 * @return A String literal specifying the course name.
    	 */
    	public String getCourseName()
    	{
    		return courseName;
    	}
    	
    	/**
    	 * Changes the course name.
    	 * 
    	 * @param name A String literal specifying the name of the new course.
    	 */
    	public void setCourseName(String name)
    	{
    		courseName = name;
    	}
    	
    	/**
    	 * Returns the number of credits for a course.
    	 * 
    	 * @return A double value specifying the number of credits for a course.
    	 */
    	public double getCourseCredit()
    	{
    		return courseCredit;
    	}
    	
    	/**
    	 * Returns the letter grade for a course.
    	 * 
    	 * @return A String literal specifying the course grade.
    	 */
    	public String getCourseGrade()
    	{
    		return courseGrade;
    	}
    	
    	/**
    	 * Returns the letter grade for a course. If empty,
    	 * course has not been taken.
    	 * 
    	 * @return A String literal specifying the letter grade.
    	 */
    	public double getCourseGPA()
    	{
    		return courseGPA;
    	}
    	
    	/**
    	 * Returns a String literal specifying course information.
    	 */
    	public String toString()
    	{
    		String message = "Course Number\tCourse Name\t\tCredit Hours\tGrade\tGPA\n\n";
    		
    		message = message + courseNum + "\t" + courseName + "\t\t" 
    			+ courseCredit + "\t" + courseGrade + "\t" + courseGPA + "\n";
    		
    		return message;
    	}
    	
    	/**
    	 * Compares two courses to see if they have the same course number and name.
    	 * Returns false if not, true if they are.
    	 */
    	public boolean equals(Object other)
    	{
    		if(!(other instanceof Course))
    			return false;
    		Course course = (Course)other;
    		return courseNum.equals(course.courseNum) && courseName.equals(course.courseName);
    	}
    }
    Java Code:
    /**
     * Final Group Project
     */
    package finalproject;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.StringTokenizer;
    
    /**
     * Calls the Course class to create an array of courses
     * to be attached to a student.
     * 
     * @author Benjamin French
     * @version 1.1
     */
    public class Transcript 
    {
    	/** An list of courses */
    	private ArrayList<Course> courseList;
    	
    	/** The courses read in */
    	private String[][]courses = new String[50][3];
    	
    	/** The student ID */
    	private String studentID;
    	
    	/** The Course object */
    	private Course course;
    	
    	/** The course name */
    	private String courseName;
    	
    	/** The course number */
    	private String courseNumber;
    	
    	/** The course credit */
    	private String courseCredit;
    	
    	/** The course grade */
    	private String courseGrade;
    	
    	/** The number of earned course credits */
    	private double earnedCredit;
    	
    	/** An array of course GPAs */
    	private double[] courseGPA;
    	
    	/** The cumulative GPA */
    	private double gpa;
    
    	/**
    	 * Constructs a new Transcript.
    	 * @throws IOException 
    	 */
    	public Transcript(String id) throws IOException 
    	{
    		courseList = new ArrayList<Course>();
    		studentID = id;
    		[COLOR="Red"]getCourses(studentID);[/COLOR]
    		populateTranscript();
    		calculateGPA();
    		addEarnedCredit();
    	}
    	
    	/**
    	 * Totals the number of earned course credits.
    	 */
    	public void addEarnedCredit()
    	{
    		for(int i = 0; i < courseList.size(); i++)
    		{
    			if(!(courseList.get(i).getCourseGrade().equals("")))
    				earnedCredit += courseList.get(i).getCourseCredit();
    		}
    	}
    	
    	/**
    	 * Returns the earned credit.
    	 * 
    	 * @return A double value specifying the total earned credit.
    	 */
    	public double getEarnedCredit()
    	{
    		return earnedCredit;
    	}
    	
    	/**
    	 * Calculates the GPA for a transcript.
    	 */
    	public void calculateGPA()
    	{
    		for(int i = 0; i < courseList.size(); i++)
    		{
    			courseGPA[i] = courseList.get(i).getCourseGPA();
    		}
    		
    		for(int i = 0; i < courseGPA.length; i++)
    		{
    			gpa += courseGPA[i];
    		}
    		gpa = gpa / courseGPA.length;	
    	}
    	
    	/**
    	 * Returns the GPA for a particular transcript.
    	 * 
    	 * @return A double value specifying the GPA.
    	 */
    	public double getGPA()
    	{
    		return gpa;
    	}
    	
    	/**
    	 * Populates the array list with course data.
    	 */
    	public void populateTranscript()
    	{
    		int row = 0;
    		while(row < 23)
    		{
    			courseNumber = courses[row][0];
    			courseName = courses[row][1];
    			courseCredit = courses[row][2];
    			courseGrade = courses[row][3];
    			course = new Course(courseNumber, courseName, courseCredit, courseGrade);
    			courseList.add(course);
    			row++;
    		}
    	}
    	
    	/**
    	 * Returns the transcript course information.
    	 */
    	public String toString()
    	{
    		String output = "COURSE NO:\tCOURSE NAME\t\tCOURSE CREDIT\tCOURSE GRADE\n\n";
    		for(int i = 0; i < courseList.size(); i++)
    		{
    			output += courseList.get(i).getCourseNumber() + "\t";
    			output += courseList.get(i).getCourseName() + "\t\t"; 
    			output += courseList.get(i).getCourseCredit() + "\t";
    			output += courseList.get(i).getCourseGrade() + "\n";
    		}
    		return output;
    	}
    	
    	/**
    	 * Compares two courses in an array.
    	 * Returns false if not the same;
    	 * true if they are.
    	 */
    	public boolean equals(Object other)
    	{
    		boolean isEqual = false;
    		
    		if(!(other instanceof Transcript))
    			return false;
    		Transcript trn = (Transcript)other;
    		
    		for(int i = 0; i < courseList.size(); i++)
    		{
    			isEqual = isEqual && this.courseList.get(i) == trn.courseList.get(i);
    		}
    		return isEqual;
    	}
    	
    	/**
    	 * Retrieves a list of courses based on the student ID.
    	 * 
    	 * @param ssn The student's ID.
    	 * @throws IOException
    	 */
    	public void getCourses(String ssn) throws IOException
    	{
    		String filename = ssn;
    		
    		File file = new File(filename + ".csv");
    		
    		BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
    		String line = null;
    		int row = 0;
    		int col = 0;
    	 
    		//read each line of text file
    		try
    		{
    			while((line = bufRdr.readLine()) != null && row < courses.length)
    			{	
    				StringTokenizer st = new StringTokenizer(line, ",");
    				while(st.hasMoreTokens())
    				{
    					//get next token and store it in the array
    					[COLOR="red"]courses[row][col] = st.nextToken();[/COLOR]
    					col++;
    				}
    			col = 0;
    			row++;
    			}
    		}
    		catch(FileNotFoundException e)
    		{
    			System.out.println("Can't access file!");
    			e.printStackTrace();
    		}
    		
    	}
    }
    Java Code:
    /**
     * Final Group Project
     */
    package finalproject;
    
    import java.io.IOException;
    
    /**
     * Calls the Transcript class to get Course and Grade data.
     * 
     * @author Rashad Ayyash
     * @version 1.1
     */
    public class Student
    {
    	/** The student ID */
    	private String studentID;
    	
    	/** The student name */
    	private String studentName;
    	
    	/** The student major */
    	private String studentMajor;
    	
    	/** The earned credit hours */
    	private double cHours;
    	
    	/** The cumulative GPA */
    	private double gpa;
    	
    	/** The Transcript belonging to the student */
    	private Transcript transcript;
    	
    	/**
    	 * Constructs a new Student with an id, name, and major.
    	 * 
    	 * @param id The student ID.
    	 * @param name The student name.
    	 * @param major The student major.
    	 * @throws IOException
    	 */
    	public Student(String id, String name, String major) throws IOException 
    	{
    		studentID = id;
    		studentName = name;
    		studentMajor = major;
    		[COLOR="red"]transcript = new Transcript(studentID);[/COLOR]
    		cHours = transcript.getEarnedCredit();
    		gpa = transcript.getGPA();
    	}
    	
    	/**
    	 * Returns the student ID.
    	 * 
    	 * @return A String literal specifying the student ID.
    	 */
    	public String getID()
    	{
    		return studentID;
    	}
    	
    	/**
    	 * Returns the credit hours earned.
    	 * 
    	 * @return A double value specifying the credit hours.
    	 */
    	public double getCreditHours()
    	{
    		return cHours;
    	}
    	
    	/**
    	 * Returns the GPA.
    	 * 
    	 * @return A double value specifying the GPA.
    	 */
    	public double getGPA()
    	{
    		return gpa;
    	}
    	
    	/**
    	 * Returns the student's major.
    	 * 
    	 * @return A String literal specifying the major.
    	 */
    	public String getMajor()
    	{
    		return studentMajor;
    	}
    	
    	/**
    	 * Returns the student's name.
    	 * 
    	 * @return A String literal specifying the name.
    	 */
    	public String getName()
    	{
    		return studentName;
    	}
    	
    	/**
    	 * Returns the student academic information.
    	 */
    	public String toString()
    	{
    		String output = "Name: " + studentID + "\n";
    		output += "Major: " + studentMajor + "\n";
    		output += "Earned Credit Hours: " + cHours + "\t";
    		output += "GPA: " + gpa + "\n";
    		output += transcript.toString();
    		return output;
    	}
    	
    	/**
    	 * Compares two Student objects.
    	 * Returns false if not identical;
    	 * true, if they are.
    	 */
    	public boolean equals(Object other)
    	{
    		if(!(other instanceof Student))
    			return false;
    		Student s = (Student)other;
    		return studentID == s.studentID;
    	}
    }
    
    /**
     * Final Group Project
     */
    package finalproject;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    /**
     * A Class designed to test the Student Transcript System.
     * 
     * @author Albert Hernandez
     * @version 1.1
     */
    public class StudentRecord 
    {
    	/**
    	 * Runs the program.
    	 * 
    	 * @param args A String literal specifying command-line parameters.
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException
    	{
    		Scanner s = new Scanner(System.in);
    		ArrayList<Student> students = new ArrayList<Student>();
    		System.out.println("Please enter your social security number:");
    		String ssn = s.nextLine();
    		[COLOR="red"]students.add(new Student("1001","Albert Gabriel Hernandez", "Computer Science"));[/COLOR]
    		
    		for(int i = 0; i < students.size(); i++)
    		{
    			if(ssn == students.get(i).getID())
    				students.get(i).toString();
    		}
    	}
    }

    I get the following error output:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at finalproject.Transcript.getCourses(Transcript.java :199)
    at finalproject.Transcript.<init>(Transcript.java:64)
    at finalproject.Student.<init>(Student.java:47)
    at finalproject.StudentRecord.main(StudentRecord.java :30)

    Please help me resolve this issue.
    Attached Files Attached Files
    Last edited by ahernandez980; 11-26-2010 at 06:21 AM. Reason: To be more thorough.

  2. #2
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Hi ,
    It would be much easier if you format and put ur code inside code tag ..else highlight line Transcript.java :199. Its too much of code and most of us are too lazy to go through it . So reformat the code or highlight the exact line ur code is throwing the error.



    warm regards
    Vinod M
    ____________________________
    Give me beans .........

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Vinod Mukundan View Post
    Hi ,
    It would be much easier if you format and put ur code inside code tag ..else highlight line Transcript.java :199. Its too much of code and most of us are too lazy to go through it . So reformat the code or highlight the exact line ur code is throwing the error.



    warm regards
    Vinod M
    ____________________________
    Give me beans .........
    That's try to be specific as much as possible. Posting such a long codes are not easy to read. Because of the members are not run your code and find bugs, instead they go through a dry-run. So posting relevant is the key. Good luck! :)

  5. #5
    ahernandez980 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    StudentRecord.java : 30 adds a new Student to the students arraylist.

  6. #6
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Hi ,

    private String[][]courses = new String[50][3]; in Transcript class


    Define it as

    private String[][]courses = new String[50][4];


    as you have 4 values to be inserted. Read about Arrays when you get time :)


    warm regards
    Vinod M
    ____________________________
    Give me beans .........

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by ahernandez980 View Post
    StudentRecord.java : 30 adds a new Student to the students arraylist.
    That's end at this line, seems you've track it correctly.

    Java Code:
    courses[row][col] = st.nextToken();
    In each adding check the size of the sources, and the index you are involve at that time in your code. Did you do that?


Similar Threads

  1. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  2. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 04:17 PM
  3. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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