Solved–Assignment #7:- Objects, searching, sorting- -Solution

$30.00 $19.00

Specification document How to hand in your work Submit the file UvicOrganizer.java in the Assignment #7 link on connex. 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 object’s instance methods. How to create and use an array of…

You’ll get a: . zip file solution

 

 

Description

5/5 – (2 votes)

Specification document

How to hand in your work

Submit the file UvicOrganizer.java in the Assignment #7 link on connex.

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 object’s 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.

Page 1 of 8

Your program must:

  1. Follow the specification document.

  1. 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 course’s title.

  1. 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.

  1. 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.

  • 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.

Page 2 of 8

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).

Page 3 of 8

Testing the 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 course’s title.

Assuming we have linked a Scanner to the file smallList.txt:

The first line will always contain a single value representing the number of Uvic Course data items contained in the file (make your array this size)

After the first line, the first token on every subsequent line represents the course’s department

The second token per line is an

integer value representing the

course’s unique course number

The remaining words on a line represent the course’s title.

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:

Page 4 of 8

Testing the 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:

Page 5 of 8

Testing the 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):

Page 6 of 8

Testing the 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):

Page 7 of 8

Testing the 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):

sortByNumber

Call your completed printArray method before and after sorting to ensure the array has been properly sorted.

Page 8 of 8