forked from sensei-thundercleese/closet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderedArrayList.java
More file actions
88 lines (64 loc) · 2.38 KB
/
Copy pathOrderedArrayList.java
File metadata and controls
88 lines (64 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*============================================
class OrderedArrayList
Wrapper class for ArrayList.
Imposes the restriction that stored items
must remain sorted in ascending order
============================================*/
//ArrayList's implementation is in the java.util package
import java.util.ArrayList;
public class OrderedArrayList {
// instance of class ArrayList, holding objects of type Comparable
// (ie, instances of a class that implements interface Comparable)
private ArrayList<Comparable> _data;
// default constructor initializes instance variable _data
public OrderedArrayList() {
// *** YOUR IMPLEMENTATION HERE ***
}
public String toString() {
// *** YOUR IMPLEMENTATION HERE ***
return ""; //placeholder
}
public Comparable remove( int index ) {
// *** YOUR IMPLEMENTATION HERE ***
return ""; //placeholder
}
public int size() {
// *** YOUR IMPLEMENTATION HERE ***
return -1; //placeholder
}
public Comparable get( int index ) {
// *** YOUR IMPLEMENTATION HERE ***
return ""; //placeholder
}
// addLinear takes as input any comparable object
// (i.e., any object of a class implementing interface Comparable)
// inserts newVal at the appropriate index
// maintains ascending order of elements
// uses a linear search to find appropriate index
public void addLinear(Comparable newVal) {
// *** YOUR IMPLEMENTATION HERE ***
}
// addBinary takes as input any comparable object
// (i.e., any object of a class implementing interface Comparable)
// inserts newVal at the appropriate index
// maintains ascending order of elements
// uses a binary search to find appropriate index
public void addBinary(Comparable newVal) {
// *** YOUR IMPLEMENTATION HERE ***
}
// main method solely for testing purposes
public static void main( String[] args ) {
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OrderedArrayList Franz = new OrderedArrayList();
// testing linear search
for( int i = 0; i < 15; i++ )
Franz.addLinear( (int)( 50 * Math.random() ) );
System.out.println( Franz );
// testing binary search
Franz = new OrderedArrayList();
for( int i = 0; i < 15; i++ )
Franz.addBinary( (int)( 50 * Math.random() ) );
System.out.println( Franz );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
}
}//end class OrderedArrayList