I need help with a question for my into comp sci course. This is what the desire
ID: 3672093 • Letter: I
Question
I need help with a question for my into comp sci course. This is what the desired output is for the question:
The first line "Deplicate ..." and the last number of each line do not show up for some reason.
All code files: https://docs.google.com/document/d/1vNiQa_orgQjeugok9kjy_SPD_HPzwmEz9CMqajWC20w/edit?usp=sharing
3.3 SaMpLe oUTPUT (produced by the supplied TestA2Q1.java program) Duplicate CRN: 20162883 20163888,Aristotle, Department of Philosophy,3,2 20162006, Zookeeping,Animal Science,2,1 20162003,Intermediate Cereal Cooking,Culinary Arts, 2,2 20163084, Potato Trees,Agriculture, 3,2 20161005, Paper Airplanes,Mechanical Engineering,1,4 20161082, Basket Weaving, Fine Arts,1,1 TestA201: java.lang.ArrayIndexOutofBoundsException: getcourse received out of bounds index of 6 End of processing Programmed by Stew DentExplanation / Answer
Problem is in your TestA2Q1 class
Mistake: for(int i=0; i<=courses.getNumCourses(); i++)
Correction : for(int i=0; i< courses.getNumCourses(); i++)
Explanation: array index always starts with 0. So if you have 6 as the num of courses, then you can access each one of them using 0,1,2,3,4,5. If i<=courses.getNumCourses(), then for 6 also for loop will execute and you will get ArrayInexOutOfBoundsException.
TestA2Q1.java
public class TestA2Q1
{
public static void main(String[] args)throws Exception
{
//Create a Database object and load the test file into it.
Database courseList = new Database("courses.txt","coursesOut.txt");
CourseListQ2 courses = courseList.load();
//Add a new course and delete an existing course
courses.addCourse(new Course("Aristotle", "Department of Philosophy",3,2));
courses.removeCourse("20161001");
//Write out the contents of the database, and save it to a file.
try {
for(int i=0; i<courses.getNumCourses(); i++)
System.out.println(courses.getCourse(i).toCsv());
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("TestA2Q1: " + e);
}
try {
courseList.save(courses);
} catch (Exception e){
System.out.println("TestA2Q1: " + e);
}
System.out.println("End of processing. Programmed by Stew Dent.");
}//main
}//TestA2Q1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.