# lab3 - CS116 LAB 3 This Lab is to be started in the lab. It...

This preview shows pages 1–4. Sign up to view the full content.

CS116 LAB 3 This Lab is to be started in the lab. It is due on June 19 on Blackboard time stamped by 10:00 p.m. Objectives: 1. Sorting of a Vector data structure. 2. Binary search of a Vector data structure. 3. Multidimensional Arrays. 4. ArrayLists Task 1 (1.0 point)-Sorting Vectors and Binary Search of Vectors We are going to use the classes from InClassExercise2. Therefore make sure that you have the classes Vehicle, VehicleType, ArrayandVectorOfVehicles and AnotherVehicleClient. You also need the text file Vehicles.txt. Modify the Vehicle.txt to add 3 more lines of information in order to make the number of vehicles to be generated equal to 10. car8, 7.0, 5, 2.0, 0.0, MINI_VAN car9, 10.0, 1, 0.0, 0.0, VAN 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
car10, 2.0, 3, 20.0, 0.0, SUV A. Modify ArrayandVectorOfVehicles 1. In class ArrayandVectorOfVehicles we want to add a method called sortVector. This method takes as argument a vector object that has Vehicle objects stored in it. It returns a vector type with the Vehicle objects that are sorted according to their distance instance variable from the smallest to the largest. Public Vector sortVector(Vector vec) { } Modify the selection sorting algorithm so that it handles Vehicle objects out of a Vector rather than an array and sorts them according to distance. o Note : To swap elements of a vector by comparison to swapping elements in an array consider the example code: Object obj2=mv.elementAt(index) / /temp=newArray[index];/ /equivalent array code VehicleA temp=(VehicleA)obj2; / /newArray[index]=newArray[newArray.length-d-1]; / /equivelant array code mv.set(index, mv.elementAt(vm.size()-d-1)); / /newArray[newArray.length-d-1]=temp;/ /equivalent array code mv.set(mv.size()-d-1, temp); 2
2. In the class ArrayandVectorOfVehicles also add a method called vector BinarySearch . The method accepts a Vector whose Vehicle objects are sorted according to their distance value and a double value that represents a distance value that acts as the searching key. public String binarySearchVector(Vector veh, double key) Note 1: If the key is not found the method returns the message String : “The object was not found”. o Note 1: Remember that this method does not return the index of the array (or vector in this case) as in the example discussed in the lecture but instead it returns the name of the vehicle located at that index (return type is a String as shown in the signature of the method). Therefore, what you have to do is capture the index (middle) with a variable and then recover the Vehicle object from the vector and use it to identify the Vehicle object’ s name and return it. o Note 4: Since we are dealing with a vector and not an array few changes to the binary search code are needed. The length of the vector is found by using the vector object (i.e.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 07/06/2010 for the course CS 116 taught by Professor Koutsogiannakis during the Spring '08 term at Illinois Tech.

### Page1 / 13

lab3 - CS116 LAB 3 This Lab is to be started in the lab. It...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online