Skip to content
54 changes: 41 additions & 13 deletions Chapter 15 Activities/SentenceReverser/src/SentenceReverser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,53 @@
import java.util.Stack;

/**
* Class for reversing the order of a sentence.
Class for reversing the order of a sentence.
*/
public class SentenceReverser
{
/**
* Reverses the given sentence.
*
* @param sentence Sentence to be reversed.
* @return reversed sentence.
Reverses the given sentence.
@param sentence Sentence to be reversed.
@return reversed sentence.
*/
public static String reverse(String sentence)
{
// Complete this method. Use a Stack.
...






String reversed = "";
Scanner scanner = new Scanner(sentence);
Stack<String> stack = new Stack<>();
while (scanner.hasNext())
{
String pushWord = scanner.next();
stack.push(pushWord);
if (pushWord.contains("."))
{
boolean firstWord = true;
while (stack.size() > 0)
{
String popWord = stack.pop();
if (firstWord)
{
String firstLetter = popWord.substring(0, 1);
popWord = firstLetter.toUpperCase() + popWord.substring(1);
firstWord = false;
}
else
{
popWord = popWord.toLowerCase();
}
popWord = popWord.replace(".", "");
reversed += popWord;
if (stack.size() <= 0)
{
reversed += ".";
}
if (scanner.hasNext() || (stack.size() > 0))
{
reversed += " ";
}
}
}
}
return reversed;
}
}
113 changes: 113 additions & 0 deletions Chapter 15 Class Notes/src/ListDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,118 @@ public class ListDemo
{
public static void main(String[] args)
{
/*
The addLast method can be used to populate a list.
*/
LinkedList<String> staff = new LinkedList<>();
staff.addLast("Diana");
staff.addLast("Harry");
staff.addLast("Romeo");
staff.addLast("Tom");

// The list is currently: DHRT

/*
The listIterator method creates a new list iterator that is
positioned at the front of the list.
The "|" in the comment indicates the iterator position.
*/
ListIterator<String> iterator = staff.listIterator(); // |DHRT

/*
The next method advances the iterator over the next element
in the list.
*/
iterator.next(); // D|HRT

/*
The next method also returns the element that the iterator
is passing.
*/
String name = iterator.next(); // DH|RT
System.out.println(name);
System.out.println("Expected: Harry");

/*
The add method inserts an element at the iterator position.
The iterator is then positioned after the element that was added.
*/
iterator.add("Juliet"); // DHJ|RT
iterator.add("Nina"); // DHJN|RT

/*
The remove method removes the element returned by the last
call to next or previous.
The remove method can only be called once after calling next
or previous.
The remove method cannot be called after calling add.
*/
iterator.next(); // DHJNR|T
iterator.remove(); // DHJN|T

System.out.println(staff);
System.out.println("Expected: [Diana, Harry, Juliet, Nina, Tom]");

/*
The set method updates the element returned by the last call
to next or previous.
*/
iterator.previous(); // DHJ|NT
iterator.set("Albert"); // DHJ|AT

/*
The hasNext method is often used in the context of a while loop.
*/
iterator = staff.listIterator(); // |DHJAT
while(iterator.hasNext())
{
String n = iterator.next();
if(n.equals("Juliet"))
{
// DHJ|AT
iterator.remove(); // DH|AT
}
}
// DHAT|

/*
Enhanced for loops work with linked lists!
*/
for(String n : staff)
{
System.out.print(n + " ");
}
System.out.println();
System.out.println("Expected: Diana, Harry, Albert, Tom");

/*
ConcurrentModificationException

Cannot modify a linked list while also using an iterator
unless you use that iterator to so.
*/
iterator = staff.listIterator();
while(iterator.hasNext())
{
String n = iterator.next();
if(n.equals("Harry"))
{
//staff.remove("Diana");
}
}

/*
ConcurrentModificationException

The enhanced for loop automatically creates an iterator!
That's how it works!
*/
for(String n : staff)
{
if(n.equals("Harry"))
{
staff.add("Charlie");
}
}
}
}
31 changes: 31 additions & 0 deletions Chapter 15 Class Notes/src/MapDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,36 @@ public class MapDemo
{
public static void main(String[] args)
{
/*
The Map interface is a generic. The first type is the type
of the key; the second, the type of the value.
*/
Map<String, Color> favoriteColors = new HashMap<>();

favoriteColors.put("Brian", Color.RED);
favoriteColors.put("Minghan", Color.ORANGE);
favoriteColors.put("Dylan", Color.BLUE);
favoriteColors.put("Daniel", Color.MAGENTA);
favoriteColors.put("Robert", Color.CYAN);

// two different keys can have the same value
favoriteColors.put("Schmit", Color.MAGENTA);

// but the same key cannot have two different values
// this changes the value for the key "Daniel"
favoriteColors.put("Daniel", Color.RED);

// create a set of the keys in the map
Set<String> keySet = favoriteColors.keySet();

for(String key : keySet)
{
System.out.println(key + " (" + key.hashCode() + "): " +
favoriteColors.get(key));
}




}
}
14 changes: 14 additions & 0 deletions Chapter 15 Class Notes/src/PriorityQueueDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,19 @@ public class PriorityQueueDemo
{
public static void main(String[] args)
{
// create a priority queue of to-do items
Queue<WorkOrder> q = new PriorityQueue<>();

q.add(new WorkOrder(3, "vacuum carpets"));
q.add(new WorkOrder(2, "water plants"));
q.add(new WorkOrder(2, "make dinner"));
q.add(new WorkOrder(2, "walk dog"));
q.add(new WorkOrder(10, "play video games"));
q.add(new WorkOrder(1, "take Chapter 15 practice quiz"));

while(q.size() > 0)
{
System.out.println(q.remove());
}
}
}
22 changes: 22 additions & 0 deletions Chapter 15 Class Notes/src/QueueDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,27 @@ public class QueueDemo
{
public static void main(String[] args)
{
// create a print queue of strings (using a linked list)
Queue<String> jobs = new LinkedList<>();

// add several print jobs
jobs.add("Joe: Expense Report #1");
jobs.add("Cathy: Meeting Memo");

System.out.println("Printing: " + jobs.remove());

jobs.add("Cathy: Purchase Order #1");
jobs.add("Joe: Expense Report #2");
jobs.add("Joe: Weekly Report");

System.out.println("Printing: " + jobs.remove());

jobs.add("Cathy: Purchase Order #2");

// print the rest of the jobs in the queue
while(jobs.size() > 0)
{
System.out.println("Printing: " + jobs.remove());
}
}
}
21 changes: 21 additions & 0 deletions Chapter 15 Class Notes/src/StackDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,26 @@ public class StackDemo
{
public static void main(String[] args)
{
// create a stack of commands
Stack<String> commands = new Stack<>();

// push a bunch of commands onto the undo stack
commands.push("Insert 'Hello'");
commands.push("Insert ','");
commands.push("Insert ' '");
commands.push("Insert 'World'");
commands.push("Insert '?'");
commands.push("Delete '?'");
commands.push("Insert '!'");

// print the stack; the top of the stack is on the far right
System.out.println(commands);

// undo the last 4 commands
for(int i = 0; i < 4; i++)
{
String command = commands.pop();
System.out.println("Undo: " + command);
}
}
}
49 changes: 48 additions & 1 deletion Chapter 15 Class Notes/src/WordAnalysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,34 @@ public class WordAnalysis
public static void main(String[] args)
throws FileNotFoundException
{
// read the dictionary and the novel
Set<String> dictionaryWords = readWords("src/words");
Set<String> novelWords = readWords("src/alice30.txt");

// 1. print all words that are in the novel but not the dictionary

// the enhanced for loop works with sets
for(String word : novelWords)
{
if(!dictionaryWords.contains(word))
{
System.out.println(word);
}
}

System.out.println("unique words: " + novelWords.size());

// 2. print the number of unique words with > 3 letters
Iterator<String> i = novelWords.iterator();
while(i.hasNext())
{
if(i.next().length() <= 3)
{
i.remove();
}
}

System.out.println("unique words (> 3 letters): " + novelWords.size());
}

/**
Expand All @@ -25,6 +53,25 @@ public static void main(String[] args)
public static Set<String> readWords(String filename)
throws FileNotFoundException
{
return null;
/*
The implementation of the set doesn't matter; so,
store the reference in a variable of type Set
*/
Set<String> words = new HashSet<>();
Scanner in = new Scanner(new File(filename));

// use any character other than a-z or A-Z as delimiters
in.useDelimiter("[^a-zA-Z]+");

while(in.hasNext())
{
/*
adding duplicates to a set is ignored
(so is removing elements that don't exist)
*/
words.add(in.next().toLowerCase());
}

return words;
}
}
4 changes: 1 addition & 3 deletions Chapter 15 Class Notes/src/WorkOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public String toString()
public int compareTo(Object otherObject)
{
WorkOrder other = (WorkOrder) otherObject;
if (priority < other.priority) { return -1; }
else if (priority > other.priority) { return 1; }
else { return 0; }
return this.priority - other.priority;
}
}
Loading