Learning outcomes
When you have completed this assignment, you will understand:
- How to create an instance of a class (object instantiation).
- How to invoke an objects instance methods.
- How to create and use an array of objects.
- How to search and sort through data in an array.
In this assignment you will be writing methods in a Java program UvicOrganizer.java. Your program will use UvicCourse objects. Download the UvicCourse.java file and save it in the same directory as the UvicOrganizer.java file.
UvicCourse objects have three instance variables, String dept, int num, and String title, as shown below. Suppose our course, CSC 110, was represented as an instance of a UvicCourse called c1, it would look like the following:
In this assignment, you will use a Scanner to read through a text file containing course data, and fill an array of UvicCourses with this data. You will then create methods to print, search and sort through the array.
Your program must:
- Follow the specification document.
- Ask the user to enter the name of the input file. Note that the marker can use a different data file than the examples provided in the Appendex. The data file will have exactly the same format (i.e., same columns, with each row having the same number of items) but it may contain more or fewer rows. Continue to ask for another input filename if the one entered is not valid.
Input file format (example: smallList.txt)
- The first line contains the number of UvicCourse data elements are contained in the file (the total number of courses to put into the array)
- Each remaining line contains the department name, followed by the course number. The remaining words on each line make up the courses title.
- After linking a Scanner to an input file, write the method makeArray, that accepts the Scanner as a parameter, and then creates and fills a UvicCourse array with all of the UvicCourse data contained in the file the Scanner is linked to. The method then returns the array.
- Using the array returned from the makeArray method, create and test all of the other methods according to the specification document. Examples of how to test these methods are shown in the Appendix.
Marking
Your mark will be based on the following criteria:
- Your code must compile and run. Some examples of how to test your methods, along with expected output, are outlined in Appendix A.
- Your code must conform to all the requirements mentioned in the specification document.
- Test each of the required methods to ensure each one functions correctly.
- Your code must follow the guidelines outlined in Style_Guidelines.pdf, found through the Lectures & Stuff link in the Lab Resources folder on connex. You may notice that the specification document provides some very nice comments you are welcome to borrow.
Appendix A Testing your code
Getting started setting up the main method:
Your main method will have two Scanners. The first Scanner is used to read in user input from the console (user entries underlined in red):
The second Scanner scans the contents of a file. If the file cannot be found, the program prompts the user to enter another file name, until the second Scanner is successfully linked to a file (as shown above).
makeArray method:
Now that you have a Scanner linked to a text file, the next step is to copy all of the contents of the file into an array of UvicCourses.
Input files will all have the following format:
- The first line contains the number of UvicCourse data elements that are contained in the file (the total number of courses to put into the array)
- Each remaining line contains the department name, followed by the course number. The remaining words on each line make up the courses title.
Assuming we have linked a Scanner to the file smallList.txt:
Each line of data can be used to create a single instance of a UvicCourse. For example, the first line would create the following (in this case named c1):
Then each UvicCourse is inserted into an array, creating the following array of UvicCourse objects:
printArray method:
Given an array of UvicCourse objects, the method prints out information about each element in the array.
Given the following array (read in from smallList.txt):
Calling this method and passing in the array above produces the following output:
listCoursesInDept method:
Given an array of UvicCourses and a department name, this method prints out all UvicCourses in the array that match the department.
Example:
courseArray (read in from firstYearList.txt):
Given the above courseArray, and 3 lines of code, the following output is produced (input entered by the user into the console is underlined in red):
listCoursesByDeptAndYear method:
Given an array of UvicCourses and a department name, this method prints out all UvicCourses in the array that match the department.
Example:
courseArray (read in from file medList.txt):
Given the above courseArray, and 5 lines of code, the following output is produced (input entered by the user into the console is underlined in red):
sortByNumber method:
Given an array of UvicCourses, this method sorts the array by the num instance variable. Example:
courseArray (read in from file medList.txt):
Call your completed printArray method before and after sorting to ensure the array has been properly sorted.
Reviews
There are no reviews yet.