Let's get warmed up with a few simple Java exercises:
In-Class Exercise 1:
Write a HelloWorld program that prints a string out (perhaps
your name instead of HelloWorld) to the screen. Compile and
run at the command-line.
In-Class Exercise 2: Download Wall.java and and ImageTool.java.
In-Class Exercise 3: Download and modify this Java program print out patterns that look like this (when N=5):
1 2 1 3 2 1 4 3 2 1 5 4 3 2 1
If you need to review Java:
In brief:
What exactly is a list?
⇒
A data structure to hold, well, a list of items.
Example:
// Need to import the LinkedList class from java.util: import java.util.*; public class ListExample { public static void main (String[] argv) { // Create an empty list: LinkedList<Integer> intList = new LinkedList<Integer> (); // Produce data and add each to list. for (int i=0; i < 10; i++) { // Compute i-th square number. int square = i*i; intList.add (square); } // Print. for (int i=0; i < 10; i++) { int element = intList.get (i); System.out.println ("Element #" + i + " = " + element); } } }Note:
Lists can contain any kind of object, for example:
import java.util.*; import java.awt.*; import java.awt.image.*; public class ListExample2 { public static void main (String[] argv) { // Create an empty list that can hold objects of type Image: LinkedList<Image> imageList = new LinkedList<Image> (); // Retrieve images from files using ImageTool, and add one // by one into the list. ImageTool imTool = new ImageTool (); Image image1 = imTool.readImageFile ("alum1.jpg"); imageList.add (image1); Image image2 = imTool.readImageFile ("alum2.jpg"); imageList.add (image2); Image image3 = imTool.readImageFile ("alum3.jpg"); imageList.add (image3); viewImages (imageList); } // The list as method parameter: static void viewImages (java.util.List<Image> imageList) { ImageTool imTool = new ImageTool (); for (Image image : imageList) { // Note the for-loop imTool.showImage (image); } } }
In-Class Exercise 4: Download the above program, along with ImageTool.java and the three image files (alum1.jpg, alum2.jpg, alum3.jpg), compile and execute. Can you identify the person in the third picture?
Can an array be used for a list?
⇒ Yes, for example
public class ListExample3 { public static void main (String[] argv) { // Plain old array: must specify size int[] intList = new int [10]; for (int i=0; i < 10; i++) { intList[i] = i*i; } // Print. for (int i=0; i < 10; i++) { System.out.println ("Element #" + i + " = " + intList[i]); } } }Note:
What is a queue?
Let's look at an example
import java.util.*; public class QueueExample { public static void main (String[] argv) { // We'll use a list, but treat it like a queue. // Note: it's a list meant to hold strings. LinkedList<String> queue = new LinkedList<String> (); // Add some names: queue.add ("Alice"); queue.add ("Bob"); queue.add ("Chen"); queue.add ("Dagmar"); queue.add ("Eva"); queue.add ("Faisal"); System.out.println ("Queue contents: " + queue); // Extract one by one: while (! queue.isEmpty() ) { String name = queue.remove (); System.out.println ("Next comes " + name); System.out.println ("Queue contents: " + queue); } } }Note:
Queue contents: [Alice, Bob, Chen, Dagmar, Eva, Faisal] Next comes Alice Queue contents: [Bob, Chen, Dagmar, Eva, Faisal] Next comes Bob Queue contents: [Chen, Dagmar, Eva, Faisal] Next comes Chen Queue contents: [Dagmar, Eva, Faisal] Next comes Dagmar Queue contents: [Eva, Faisal] Next comes Eva Queue contents: [Faisal] Next comes Faisal Queue contents: []
System.out.println ("Queue contents: " + queue);Of course, this may not be a good idea if the queue is large (e.g., 100,000 items).
What is a stack?
Let's look at an example:
import java.util.*; public class StackExample { public static void main (String[] argv) { // The Java library has a Stack data structure. Here, we'll // make a stack to hold strings. Stack<String> toDoList = new Stack<String> (); // Add some strings. toDoList.push ("Pay bills"); toDoList.push ("Clean room"); toDoList.push ("Do homework"); toDoList.push ("See movie"); toDoList.push ("Hang out"); // Print. System.out.println ("Priorities: "); while (! toDoList.isEmpty() ) { String nextPriority = toDoList.pop (); System.out.println (" " + nextPriority); } } }Note:
Priorities: Hang out See movie Do homework Clean room Pay bills
What is a tree?
public class TreeExample { public static void main (String[] argv) { // Java's library has tree variant called TreeSet. // We'll make an instance to hold String's. TreeSet<String> unusualWords = new TreeSet<String> (); // Put some stuff in. unusualWords.add ("queueing"); // Six vowels in a row. unusualWords.add ("psychorhythms"); // Longest with one vowel. unusualWords.add ("verisimilitudes"); // Longest with alternating vowel/consonant. unusualWords.add ("frillless"); // Three letters in a row. unusualWords.add ("bookkeeper"); // Three double-letter's in a row. unusualWords.add ("cwm"); // Pronounced "koom" (type of crevasse). unusualWords.add ("feedback"); // Contains all letters 'a' to 'f'. // Print. System.out.println (unusualWords); } }Note:
[bookkeeper, cwm, feedback, frillless, psychorhythms, queueing, verisimilitudes]Thus, the output looks like a simple list. It's the internal structure that's tree-like.
In-Class Exercise 5: In this exercise, you will compare the speed of searching in a tree vs. a straightforward list. Download TreeExample2.java and UniformRandom.java. You will see code that creates a tree and a linked-list and sets them up for a search comparison. The code for searching in the tree is already given. You need to add very similar code for searching in the list. Then, compile and execute.
Now that we've seen some examples, you have a better sense of what's forthcoming:
Ready?