Logout

D.4.11

Construct algorithms using the standard library collections included in JETS.

 

Teaching Note:

The classes are ArrayList and LinkedList. Students should have a broad understanding of the operation of these lists and their interface (methods) but not of the internal structure.

 

Sample Question:

sdfsdfsf

JSR Notes:

 

May 1st 2015 Note - Refer to "May Day Review" Netbeans project, the "JETS" IB document, page 8, and also the pseudocode examples in the Pseudocode in Examinations document.



(Remember that "libraries" are groups of code which has been made by others, and are now available for you to use, or in the case of the library analogy, to "borrow". So there is no need to code your own selection sort, or binary search tree etc. It's nice to make your own for learning purposes, and so that you intimately know how they work, but once you get out in the "real world", rather than in an educational setting, you might as well be the most efficient you can be and use pre-made classes and algorithms like the ones mentioned here.)

*** 2014 They are straight forward, but need to be looked at.

A JSR note on the teaching note: only LinkedList is shown in the JETS document, and it is definitely more useful as it includes all list functionality as well as the "array" functionality of ArrayList.

Here's an example of using the LinkedList Collection class:

array list project__ array list project 2

 

 26 public class GUICars extends javax.swing.JFrame {
 27 
 33     
 34     LinkedList <Car> carLinkedList = new LinkedList();
 35     
 36     public GUICars() {
 37         initComponents();
 38         myInit();
 39     }
 40                    
323 
324     private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                             
325         System.exit(0);
326     }                                            
327 
328     private void entryButtonMouseReleased(java.awt.event.MouseEvent evt) {                                          
329        
330         int numDoors = 5;
331         if(threeDoorRB.isSelected()){
332             numDoors = 3;
333         }else if(fourDoorRB.isSelected()){
334             numDoors = 4;
335         }
336         
337         carLinkedList.add(new Car(carColorTF.getText(), carNameTF.getText(), isManual.isSelected(), numDoors));
338         carColorTF.setText("");
339         carNameTF.setText("");
340         
341     }                                         
342 
343     
344     private void displayPanelMouseReleased(java.awt.event.MouseEvent evt){
345          for(int i = 0; i < carLinkedList.size(); i++){
346             displayTable.setValueAt(carLinkedList.get(i).getColor(), i, 0);
347             displayTable.setValueAt(carLinkedList.get(i).getName(), i, 1);
348             
349         }
350     }
351     
352     

A GUI class using LinkedList

 

 6 public class CarSortAndSearch {
 7 
 8     public void sortByCarName(LinkedList <Car> carLinkedList) {
 9         int n = carLinkedList.size();
10         boolean sorted = false;
11         while (!sorted) {
12             n--;
13             sorted = true;
14             for (int i = 0; i < n; i++) {
15                 if (carLinkedList.get(i).getName().compareTo(carLinkedList.get(i).getName()) > 0) {
16                     //Swap:
17                     Car temp = carLinkedList.get(i); //making a temporary carList object
18                     carLinkedList.set(i, carLinkedList.get(i+1)); //set the first to be the second
19                     carLinkedList.set(i+1, temp);  //set the second to be the temp       
20                     sorted = false;
21                 }
22             }
23         }
24     }
25 
26     public int binarySearchCarName(String carToSearchFor, LinkedList <Car> carLinkedList) {
27         int low = 0;
28         int high = carLinkedList.size() - 1;
29         while (low <= high) {
30             int mid = (high + low) / 2;
31             if (carLinkedList.get(mid).getName().equalsIgnoreCase(carToSearchFor)) {
32                 return mid;
33             } else if (carLinkedList.get(mid).getName().compareTo(carToSearchFor) < mid) {
34                 low = mid + 1;
35             } else {
36                 if (carLinkedList.get(mid).getName().compareTo(carToSearchFor) > mid) {
37                     high = mid - 1;
38                 }
39             }
40         }
41         return -1;
42     }
43     
A Sort&Search Class using LinkedList