Results 1 to 7 of 7
Thread: Need help with program
- 11-26-2010, 05:54 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
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.Last edited by ahernandez980; 11-26-2010 at 06:21 AM. Reason: To be more thorough.
- 11-26-2010, 06:04 AM #2
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 .........
- 11-26-2010, 06:21 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you've on line 30 ?
StudentRecord.java :30
- 11-26-2010, 06:23 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-26-2010, 06:24 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
StudentRecord.java : 30 adds a new Student to the students arraylist.
- 11-26-2010, 06:42 AM #6
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 .........
- 11-26-2010, 07:00 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
Execute A program from a Program!
By Moncleared in forum Advanced JavaReplies: 2Last Post: 02-22-2009, 04:17 PM -
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks