(* pgm2-Sort.cl - Kishen Maloor, CIS, University of Delaware This is an object oriented program which sorts any entered integers in the ascending order. The objective of writing this program is to demonstrate the use of the control flow constructs provided by the COOL compiler. The entered integers are stored in a singly linked list. The linked list is implemented using the List class that represents an empty list and the Cons class that represents a non-empty list. Links are maintained between subsequent cells. Each cell in the list carries an integer. Traversal of the list is facilited through the pointer to the rear end of the list. The use of the individual attributes and other secondary variables in this class have been defined. The bubble sort algorithm has been implemented to sort the integers. The sorted list is later displayed. *) -- the List class class List inherits IO { car : Int; -- cell data content cdr : List; -- pointer tmp : Int; -- temporary variable isNil() : Bool { true }; -- check if list is empty head() : Int { { abort(); 0; } }; -- aborts since list is empty tail() : List { { abort(); self; } }; -- aborts since list is empty -- initialize a new cell of the list cons(i : Int) : List { (new Cons).init(i, self) }; -- sets the data content of a list cell set(i : Int) : Int { {car <- i;} }; -- swaps the contents of 2 adjacent cells on the list swap(x : List) : Object { { tmp <- x.head(); x.set(car); car <- tmp; } }; -- used for list cell initialization cons1(i : Int,l : List) : List { (new Cons).init(i,l) }; }; -- the Cons class class Cons inherits List { -- alternate definitions of these generic functions -- on the list for non-empty lists isNil() : Bool { false }; head() : Int { car }; tail() : List { cdr }; init(i : Int, rest : List) : List { { car <- i; cdr <- rest; self; } }; }; -- the main class class Main inherits Cons { mylist : List; -- pointer that facilitates tree traversal header : List; -- used for temporary storage i : Int; -- counter used while sorting n : Int; -- number of integers entered c : Int; -- counter used while entering the integers x : Int; -- used to temporarily store input date -- prints the entire list print_list(l : List) : Object { if l.isNil() then out_string("\n") else { out_int(l.head()); out_string(" "); print_list(l.tail()); } fi }; -- implementation of the bubble sort algorithm sort_list() : Object { { i <- 1; header <- mylist; while i <= (n-1) loop { mylist <- header; while (not (mylist.tail()).isNil()) loop { if (mylist.tail()).head() < mylist.head() then { (mylist.tail()).swap(mylist); } else { 0; } fi; mylist <- mylist.tail(); } pool; i <- i+1; } pool; } }; -- the main function main() : Object { { out_string("Bubble Sort:\nEnter the # of numbers to sort: "); n <- in_int(); c <- 2; out_string("Enter the numbers: \n"); x <- in_int(); mylist <- new List.cons(x); if 1 < n then { while c <= n loop { x <- in_int(); mylist <- new List.cons1(x,mylist); c <- c+1; } pool; } else { 0; } fi; sort_list(); mylist <- header; print_list(mylist); } }; };