CS 114 Exam 1
Name_ 1. Section_
Write a recursive method void hex(int n) that takes an integer parameter and prints the number in hexadecimal. You can assume that the input is greater than zero. public void hex(int n) cfw_ if(n > 0) cfw_ hex(n/16); if(n%1
CS 114, Fall 2014, Calvin
Practice problems for second midterm
(1) You have a queue Q containing n elements and an initially empty stack S.
You want to scan the queue to see if it contains a particular value x, with
the constraint that your algorithm must
Lecture Notes 8
Stacks
Ref: Data Structures and Algorithm Analysis, C. Shaffer, pp.
117124.
The Stack ADT
The Stack ADT stores arbitrary objects
Insertions and deletions follow the last-in first-out scheme
Think of a spring-loaded plate dispenser
Main
Lecture Notes 13
Sorting
Reference: Data Structures and Algorithm Analysis, C. Shaffer,
pp. 223257.
Sorting
Each record contains a field called the key.
Linear order: comparison.
Measures of cost:
Comparisons
Swaps
Sorting
Priority Queue Sort
To sort ar
Lecture Notes 12
Priority Queues
Reference: Data Structures and Algorithm Analysis, C. Shaffer,
pp. 170171, 180187.
Priority Queues
Problem: We want a data structure that stores records as they
come (insert), but on request, releases the record with the g
Lecture Notes 2
Recursion
A recursive definition is one which uses the word being defined in
the definition.
Such definitions are very useful in many areas of computer
science.
We will see that it is a powerful programming tool.
We will explore recurs
Lecture Notes 5
Algorithm Analysis
Ref: Data Structures and Algorithm Analysis, C. Shaffer, chapter
3.
Algorithm Efficiency
There are often many approaches (algorithms) to solve a problem.
How do we choose between them?
At the heart of computer program de
Lecture Notes 14
Sorting (cont.)
Reference: Data Structures and Algorithm Analysis, C. Shaffer,
pp. 253256.
Sorting (cont.)
14-2
Sorting Lower Bound
We would like to know a lower bound for all possible sorting
algorithms.
Sorting is O(n log n) (average, w
Lecture Notes 3
Polymorphism
Ref: Lewis & Loftus Java Software Solutions, Ch. 9.
Polymorphism is an object-oriented concept that allows us to create
versatile software designs
We will cover:
defining polymorphism and its benefits
using inheritance to cr
Lecture Notes 4
Math background
Ref: Data Structures and Algorithm Analysis, C. Shaffer, chapter
3.
Set concepts and notation
Common functions
Proofs
Summations
Recurrence Relations
Math background
Set notations
cfw_7, 2, 8 The set consisting of the
CIS 114, Fall 2014
Practice problems
Prof. J. Calvin
Notes:
You may refer to algorithms, data structures, and results that
we covered in class.
When asked to describe an efficient algorithm, you may give
an algorithm that is efficient in either the aver
Lecture Notes 10
Dictionaries
Ref: Data Structures and Algorithm Analysis, C. Shaffer, pp.
131137.
Often want to insert records, delete records, search for records.
Required concepts:
Search key: Describe what we are looking for
Key comparison
Equality
Lecture Notes 7
Node Lists
Node List ADT
The Node List ADT models a sequence of positions storing
arbitrary objects
It establishes a before/after relation between positions
Generic methods:
size(), isEmpty()
Node Lists
Accessor methods:
getFirst(), ge
Lecture Notes 9
Queues
Ref: Data Structures and Algorithm Analysis, C. Shaffer, pp.
125130.
Queues
The Queue ADT
The Queue ADT stores arbitrary objects
Insertions and deletions follow the first-in first-out scheme
Insertions are at the rear of the queu
Basic AFS commands
The following commands should be more than enough for our purposes. If you
want more information on one of these commands, or any other, the man command
will provide it. For example, type
man ls
to get all the options and information ab
Lecture Notes 11
Binary (Search) Trees
Reference: Data Structures and Algorithm Analysis, C. Shaffer,
pp. 145153, 163170.
Binary Trees
A binary tree is made up of a finite set of nodes that is either empty
or consists of a node called the root together wi
public class HashingTest cfw_
public static void main(String[] args) cfw_
HashTable<String, String> ht = new HashTable<String, String>(128);
while(true)cfw_
StringBuilder s = new StringBuilder();
for(int i = 0; i < 8; i+)cfw_
char randomChar = (char)
(int
CIS 114, Fall 2014
Practice problems
Prof. J. Calvin
Notes:
You may refer to algorithms, data structures, and results that
we covered in class.
When asked to describe an efficient algorithm, you may give
an algorithm that is efficient in either the aver
import java.util.*;
import java.io.*;
public class GameDriver cfw_
public static void main(String[] args) throws IOException cfw_
FileReader readFile = new FileReader("gamescore.txt"); / Correct
the path to test the program.
BufferedReader in = new Buffer
CS 114 Assignment 11
Topic: Iterable Interface and GUI file editor (4 points)
For each task, submit the source code with detail comments,
electronically (no hardcopy).
(1) Objective: Create java Class to implement Iterable interface (2 points)
(a) Write a
import random
def getPlayerNames():
' Prompt the two players for their names and return
these two strings.'
print()
player1 = input('Name of first player: ')
player2 = input('Name of second player: ')
return player1, player2
def boardSetup(boardSize, numT
import random
def getPlayerNames():
' Prompt the two players for their names and return
these two strings.'
print()
player1 = input('Name of first player: ')
player2 = input('Name of second player: ')
return player1, player2
def boardSetup():
boardSize =
' Trapdoors & Catapults is a digital version of Chutes & Ladders.
RULES:
PLAYERS. There are two players. Humans are prompted for two names, but
all subsequent action is handled by the program.
BOARD. The board consists of spaces numbered 0 through 99.
Ten