Skip to content

Commit b2a1d9c

Browse files
committed
Added projects from CH20
1 parent 5c4c310 commit b2a1d9c

19 files changed

+522
-1
lines changed

20.10 Sets/20.10 Sets.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

20.10 Sets/src/SetTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.*;
2+
3+
/**
4+
* Created by phong.tran on 5/30/16.
5+
*/
6+
public class SetTest {
7+
public static void main(String[] args) {
8+
// create and display a List<String>
9+
String[] colors = {"red", "white", "blue", "green", "gray",
10+
"orange", "tan", "white", "cyan", "peach", "gray", "orange"};
11+
List<String> list = Arrays.asList(colors);
12+
System.out.printf("List: %s\n", list);
13+
14+
// eliminate duplicates then print the unique values
15+
printNonDuplicates(list);
16+
} // end main
17+
18+
// create a Set from a Collection to eliminate duplicates
19+
private static void printNonDuplicates(Collection<String> values)
20+
{
21+
// create a HashSet
22+
Set<String> set = new HashSet<String>(values);
23+
24+
System.out.print("\nNonduplicates are: ");
25+
26+
for(String value : set)
27+
System.out.printf("%s ", value);
28+
29+
System.out.println();
30+
} // end method printNonDuplicates
31+
} // end class SetTest

20.10 Sets/src/SortedSetTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.Arrays;
2+
import java.util.SortedSet;
3+
import java.util.TreeSet;
4+
5+
/**
6+
* Created by phong.tran on 5/30/16.
7+
*/
8+
public class SortedSetTest
9+
{
10+
public static void main(String[] args)
11+
{
12+
// create TreeSet from array colors
13+
String[] colors = {"yellow", "green", "black", "tan", "grey",
14+
"white", "orange", "red", "green" };
15+
SortedSet<String> tree=
16+
new TreeSet<String>(Arrays.asList(colors));
17+
18+
System.out.print("sorted set: ");
19+
printSet(tree); // output contents of tree
20+
21+
// get headSet based on "orange"
22+
System.out.print("headSet (\"orange\"): ");
23+
printSet(tree.headSet("orange"));
24+
25+
// get tailSet based upon "orange"
26+
System.out.print("tailSet (\"orange\"): ");
27+
printSet(tree.tailSet("orange"));
28+
29+
// get first and last elements
30+
System.out.printf("first: %s\n", tree.first());
31+
System.out.printf("last : %s\n", tree.last());
32+
} // end main
33+
34+
// output SortedSet using enhanced for statement
35+
private static void printSet(SortedSet<String> set)
36+
{
37+
for (String s : set)
38+
System.out.printf("%s ", s);
39+
40+
System.out.println();
41+
} // end method printSet
42+
} // end Class SortedSetTest
43+

20.11 Maps/20.11 Maps.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

20.11 Maps/src/WordTypeCount.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Fig. 20.18: WordTypeCount.java
2+
// Program counts the number of occurrences of each word in a String.
3+
import java.util.Map;
4+
import java.util.HashMap;
5+
import java.util.Set;
6+
import java.util.TreeSet;
7+
import java.util.Scanner;
8+
9+
public class WordTypeCount
10+
{
11+
public static void main(String[] args)
12+
{
13+
// create HashMap to stroe String keys and Integer values
14+
Map< String, Integer> myMap = new HashMap<String, Integer>();
15+
16+
createMap(myMap); // create map based on user input
17+
displayMap(myMap); // display map content
18+
19+
} // end main
20+
21+
// create map from user input
22+
private static void createMap(Map<String, Integer> map)
23+
{
24+
Scanner scanner = new Scanner(System.in); // create scanner
25+
System.out.println("Enter a string:"); // prompt for user input
26+
String input = scanner.nextLine();
27+
28+
// tokenize the input
29+
String[] tokens = input.split(" ");
30+
31+
// processing input text
32+
for ( String token : tokens )
33+
{
34+
String word = token.toLowerCase(); // get lowercase word
35+
36+
// if the map contains the word
37+
if (map.containsKey(word)) // is word in map
38+
{
39+
int count = map.get(word); // get current count
40+
map.put(word, count + 1);
41+
} // end if
42+
else
43+
map.put(word, 1); // add new word with a count of 1 to map
44+
} // end for
45+
} // end method createMap
46+
47+
// display map content
48+
private static void displayMap(Map<String, Integer> map)
49+
{
50+
Set<String> keys = map.keySet(); // get keys
51+
52+
// sort keys
53+
TreeSet<String> sortedKeys = new TreeSet<String>( keys );
54+
55+
System.out.println("\nMap contains:\nKey\t\tValue");
56+
57+
// generate output for each key in map
58+
for (String key : sortedKeys )
59+
System.out.printf("%-10s%10s\n", key, map.get(key));
60+
61+
System.out.printf(
62+
"\nsize: %d\nisEmpty: %b\n", map.size(), map.isEmpty());
63+
64+
}
65+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="false">
4+
<orderEntry type="sourceFolder" forTests="false" />
5+
</component>
6+
</module>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="false">
4+
<orderEntry type="sourceFolder" forTests="false" />
5+
</component>
6+
</module>

20.7.2 Method shuffle/src/DeckOfCards.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Fig. 20.10: DeckOfCards.java
22
// Card shuffle and dealing with Collections method shuffle
33

4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
48
// class to represent a Card in a deck of cards
59
class Card
610
{
@@ -12,4 +16,69 @@ public static enum Suit {Club, Diamonds, Heart, Spades};
1216
private final Face face; // face of card
1317
private final Suit suit; // suit of card
1418

15-
}
19+
// two-argument constructor
20+
public Card(Face cardFace, Suit cardSuit)
21+
{
22+
face = cardFace; // initialize face of card
23+
suit = cardSuit; // initialize suit of card
24+
} // end two-argument Card constructor
25+
26+
// return face of the card
27+
public Face getFace()
28+
{
29+
return face;
30+
} // end method getFace
31+
32+
// return suit of card
33+
public Suit getSuit()
34+
{
35+
return suit;
36+
}
37+
38+
// return String representation of Card
39+
public String toString()
40+
{
41+
return String.format("%s of %s", face, suit);
42+
} // end method toString
43+
} // end class Card
44+
45+
// class DeckOfCards declaration
46+
public class DeckOfCards
47+
{
48+
private List<Card> list; // declare List that will store Cards
49+
50+
// set up deck of Cards and shuffle
51+
public DeckOfCards()
52+
{
53+
Card[] deck = new Card[52];
54+
int count = 0; // number of cards
55+
56+
// populate deck with Card objects
57+
for(Card.Suit suit : Card.Suit.values())
58+
{
59+
for (Card.Face face : Card.Face.values())
60+
{
61+
deck[count] = new Card(face, suit);
62+
++count;
63+
} // end for
64+
} // end for
65+
66+
list = Arrays.asList(deck); // get List
67+
Collections.shuffle(list); // shuffle deck
68+
} // end DeckOfCards constructor
69+
70+
// output deck
71+
public void printCards()
72+
{
73+
// display 52 cards in two columns
74+
for( int i = 0; i < list.size(); i++)
75+
System.out.printf("%-19s%s", list.get(i),
76+
((i + 1) % 4 == 0 ) ? "\n" : "");
77+
} // end method printCards
78+
79+
public static void main(String[] args)
80+
{
81+
DeckOfCards cards = new DeckOfCards();
82+
cards.printCards();
83+
} // end main
84+
} // end class DeckOfCards
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.Arrays;
2+
import java.util.Collection;
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
// Fig. 20.11: Algorithms1.java
7+
// Collections methods reverse, fill, copy, max and min
8+
public class Algorithms1
9+
{
10+
public static void main(String[] args)
11+
{
12+
// create and display a List<Character>
13+
Character[] letters = {'P', 'C', 'M'};
14+
List<Character> list = Arrays.asList(letters); // get List
15+
System.out.println("list contains: ");
16+
output(list);
17+
18+
// reverse and display the List<Character>
19+
Collections.reverse(list); // reverse order the elements
20+
System.out.println("\nAfter calling reverse, list contains: ");
21+
output(list);
22+
23+
// create copyList from an array of 3 Characters
24+
Character[] lettersCopy = new Character[3];
25+
List<Character> copyList = Arrays.asList(lettersCopy);
26+
27+
// copy the contents of list into copyList
28+
Collections.copy(copyList, list);
29+
System.out.println("\nAfter copying, copyList contains: ");
30+
output(copyList);
31+
32+
// fill list with Rs
33+
Collections.fill(list, 'R');
34+
System.out.println("\nAfter calling fill, list contains: ");
35+
output(list);
36+
} // end main
37+
38+
// output List information
39+
private static void output(List <Character> listRef)
40+
{
41+
System.out.print("The list is: ");
42+
43+
for(Character element : listRef )
44+
System.out.printf("%s ", element);
45+
46+
System.out.printf("\nMax: %s", Collections.max(listRef));
47+
System.out.printf(" Min: %s\n", Collections.min(listRef));
48+
} // end method output
49+
} // end class Algorithms1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Fig. 20.12: BinarySearchTest
2+
// Collections method binarySearch
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
public class BinarySearchTest
9+
{
10+
public static void main(String [] args)
11+
{
12+
// create an ArrayList<String> from the contents of colors array
13+
String[] colors = {"red", "white", "blue", "black", "yellow",
14+
"purple", "tan", "pink"};
15+
List<String> list =
16+
new ArrayList<String>(Arrays.asList(colors));
17+
18+
Collections.sort(list); // sort the ArrayList
19+
System.out.printf("Sorted ArrayList: %s\n", list);
20+
21+
// search list for various values
22+
printSearchResults(list, colors[3]); // first item
23+
printSearchResults(list, colors[0]); // middle item
24+
printSearchResults(list, colors[7]); // last item
25+
printSearchResults(list, "aqua"); // below lowest
26+
printSearchResults(list, "gray"); // does not exist
27+
printSearchResults(list, "teal"); // does not exist
28+
} // end main
29+
30+
// perform search and display result
31+
private static void printSearchResults(
32+
List<String> list, String key)
33+
{
34+
int result = 0;
35+
36+
System.out.printf("\nSearching for: %s\n", key);
37+
result = Collections.binarySearch(list, key);
38+
39+
if (result >= 0)
40+
System.out.printf("Find at index %d\n", result);
41+
else
42+
System.out.printf("Not Found (%d)\n", result);
43+
} // end method printSearchResults
44+
} // end class BinarySearchTest

0 commit comments

Comments
 (0)