CSE 174 - Lab 13
Part 1 (8 points): Learning a new class.
1. Study the Customer class:
● For this lab you are to use the Customer class and in order to finish this lab
successfully you need to study this class very carefully, and you will be tested for
that.
● Download theAPI Documentation for Customer Class.
● Start studying the class description first.
● Scroll down and study the constructors.
● After learning about constructors, scroll down again and study the methods. You
can click on each method name to see more details.
1 | ★ You should not go any further if there is somethingin this class that you don’t |
2. Using the Customer class:
● In jGRASP create a java file called Lab13.
● DownloadtheCustomer.javafile(DONOTCHANGEANYTHINGINSIDETHIS
FILE) and put it in the same folder as your Lab13.javafile.
● Insidethemainmethod,createafewCustomerobjects,calldifferentmethods
and printthe results. Forinstance: createtwoCustomerobjects,calldifferent
methodsoneachobjectandprinttheresults.Comparetwoobjects,seeifthe
resultisacceptableforyou.Printtheobjectstoseehowtheoutputlookslike.
Now, try to print the object in a different format.
1 | ★ Again, if you have any questions please ask your instructor. You should not go to the |
- Take the quiz 13.1:
● Nowit’stimetoevaluateyourunderstandingofthisclass.Pleasegoaheadandtake the quiz 13.
Part 2 (12 points): Array of Objects & Search algorithms
InthisPartofthelabyouaretocreateanarrayofCustomerobjects,andinitializeeach
objectwiththedatafromafile.Thenyouaretowrite 2 methods,oneforlinearsearchand
oneforbinarysearchandlookfordifferentkeystoseewhichsearchmethodcanfindthe
keys faster.
1. Reading data from a file:
● Download the filecustomer_list.txtand put it inthe same folder as your
Lab13.java file.
● Try not toopenthefile,sincethistextfilecontainsmorethanamillionlinesand
could causeyourjGRASPtocrash.Asyoucanseeinthefollowingimage,in
each line there is a nickname and idnumber separated byspace which is
necessary for creating one Customer object:
1 | ● Nowlet’swritesomecode.Insidethemainmethod(ifyoulikeyoucanwritea |
1 | (you should not hardcode that number, your code shouldcalculate the number) |
2. Creating an array of Customer objects:
● Nowthatyouknowhowmanylinesthetextfilehas,thatmeansyouknowthe
lengthofyourarray.So,goaheadanddefineanarrayofCustomersusingthat
number as the length of your array.
● Youcannotdefineanarraywithoutknowingthelength.Inaddition,afterdefining
one,youcannotchangethelengthofthearraydirectly.Therefore,havingafixed
size list/array is one of the weaknesses of using arrays.
● Afterdefiningthearray,javawillinitializeeachelementofthearraywitha null
value, which means each element inside the array (eachelement is like a
Customervariable)is pointingto nothingand it isreadytopointtoanactual
Customer object.
● Theonly differencebetweenarraysofprimitivetypesandobjectsisthatafter
defininganarrayofobjectsyoushouldassignanobjecttoeachelement.So,it’s
timetoreadfromthefilelinebylineagain,butthistimeforcreatingaCustomer
object from each line and assigning it to each element of the array.
● Reopen your text file again. The file needs to be reopened because inthe
previouspartwealreadywentthroughthefileoncetocounthowmanylinesthe
file has.
Hint: for reopening the file you can create a new Scanner object and
re-assign it into your previous Scanner variable.
● Usingaloop,startreadingfromthefileaslongasthereissomethinginsidethe
file. UsehasNext() methodoverhasNextLine().Keepthatinmindduringeach
iteration you need to read one String and one long value.
● During eachiterationafter readingtwoelementsfroma line(oneString,one
long),usethemtocreateaCustomerobject(youpracticedthatinthePart1of
Lab13),andusethatCustomerobjecttoinitializethecurrentelementofthearray
with.So,thefollowingisthesummaryofwhatyouneedtodoafterdefiningan
array of Customers:
1 | loop (as long as there is something inside the file) { |
1 | ● Nowlet’s testyour arraytosee ifyouwereabletoreadfromthefile,create |
3. Writing a linear search method:
● So far,youwereable toreadfrom afileand createalist/arrayofCustomer
objects.
● NowthequestionishowwillyoufindaspecificCustomerobjectbetweenmillions
of elements inside the array?
● Asasolution,let’swriteamethodcalled linearSearch thatacceptsanarrayof
Customerobjects,andoneCustomerobjectasthekeythatneedstobefound
insidethe array.Andif yourecall, thelinearsearch methodshouldreturnan
index.
● Theonlydifferencebetweenthismethodandtheoneinthevideosisthathere
youaredealingwithCustomerobjects.So,everytimeyouneedtocheckeach
elementofthearraywiththegivenkeytoseewhethertheyareequalornot(you
practiced comparing two Customer objects in the Part1 of this lab).
● When you are done with the method, let’s test it to see if it works. Inside the main
method create a Customer object called key using the nickname: “gwstikg” and
id: 100005949
● Call the linearSearch method and give it your array and the key you just created,
and print the result. Add some messages to have your output as following:
1 | ● Now, inside the linearSearch method add a counterto count every time it |
4. Writing a binary search method:
● Repeat the whole process from the last section this time to write the
binarySearch method. Write a binarySearch method thatgets an array of
Customers and a key, and returns an index. Count how many comparisons
happen inside this method and print it inside this method right before returning
any values.
● The question that you need to ask here is whether the array is sorted since the
binary search algorithm only works with sorted lists. The good news here is that
the array is sorted based on id numbers, therefore inside the binarySearch
methodwhere you need to compare the key with thevalue of the middle element
to see if it’s greater or smaller, you only need to compare the idNumbers.
● Comment out some of the previous prints and make sure your output matches
the following output:
1 | ● Repeat the process for finding the following Customers (the numbers are long |
1 | ● After printing the results your output should look like the following: |
5. Analyzing the results:
1 | ● Looking at the first test, you can see the linear search only did one comparison |
6. Submit your Code on canvas:
● If your code generated the last results as shown above, you are ready to submit
your work on canvas.
● There are two tests: one that tests your linear search, and one for testing your
binary search.
Rubric
1 | Criteria Full Credit Partial Credit No Credit |
1 | Successful |
1 | A fully successful |
1 | Complete each |
1 | 4 Points |
1 | if your submission is |
1 | 0 Points |
1 | Writing comments for |
1 | Writing comments for the |
1 | Complete |
1 | 0.5 Point |
1 | Not writing |
1 | 0 Points |
1 | Correct Style If your submission |
1 | If there are any style |