Skip to content
Open

d #8

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "DepthFirstTester",
"request": "launch",
"mainClass": "DepthFirstTester",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "TreeDemo",
"request": "launch",
"mainClass": "TreeDemo",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "TreeTester",
"request": "launch",
"mainClass": "TreeTester",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "QueueTester",
"request": "launch",
"mainClass": "QueueTester",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "SizeTester",
"request": "launch",
"mainClass": "SizeTester",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "ListTester",
"request": "launch",
"mainClass": "ListTester",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "HTMLChecker",
"request": "launch",
"mainClass": "HTMLChecker",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "FloodFillTester",
"request": "launch",
"mainClass": "FloodFillTester",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "DrivewayDemo",
"request": "launch",
"mainClass": "DrivewayDemo",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "FirstLetterMap",
"request": "launch",
"mainClass": "FirstLetterMap",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "Gradebook",
"request": "launch",
"mainClass": "Gradebook",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "ListDemo",
"request": "launch",
"mainClass": "ListDemo",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "MapDemo",
"request": "launch",
"mainClass": "MapDemo",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "PriorityQueueDemo",
"request": "launch",
"mainClass": "PriorityQueueDemo",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "QueueDemo",
"request": "launch",
"mainClass": "QueueDemo",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "StackDemo",
"request": "launch",
"mainClass": "StackDemo",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "SudokuSolver",
"request": "launch",
"mainClass": "SudokuSolver",
"projectName": "data-structures_addf3fee"
},
{
"type": "java",
"name": "WordAnalysis",
"request": "launch",
"mainClass": "WordAnalysis",
"projectName": "data-structures_addf3fee"
}
]
}
2 changes: 1 addition & 1 deletion Chapter 15 Activities/Downsize/src/Business.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public class Business
*/
public static void downsize(LinkedList<String> employeeNames, int n)
{
...

}
}
39 changes: 25 additions & 14 deletions Chapter 15 Activities/Driveway/src/Driveway.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ public class Driveway
/**
* Stack representing the cars in the driveway.
*/

private Stack<Integer> driveway;
/**
* Stack representing the cars in the street.
*/

private Stack<Integer> street;

/**
* Constructor.
*/
public Driveway()
{
// Complete the constructor
...
driveway = new Stack<>();
street= new Stack<>();


}
Expand All @@ -35,7 +37,7 @@ public Driveway()
public void add(int licensePlate)
{
// Complete this method
...
driveway.push(licensePlate);


}
Expand All @@ -48,23 +50,32 @@ public void add(int licensePlate)
public void remove(int licensePlate)
{
// Complete this method
...

while (!driveway.isEmpty() && driveway.peek() != licensePlate) {
int car = driveway.pop();
street.push(car);}

}
if (!driveway.isEmpty() && driveway.peek() == licensePlate) {
driveway.pop();}


//after a car is taken out, the ones on the street are always returned to the driveway
while (!street.isEmpty()) {
int car = street.pop();
driveway.push(car);}}
/**
* Prints the driveway and street details to the screen.
*/
public void print()
{
System.out.println("In Driveway, starting at first in (one license plate per line):");
// Print the cars in the driveway here
...

System.out.println("In Street, starting at first in (one license plate per line):");
// Print the cars in the street here
...
{
System.out.println("In Driveway, starting at first in (one license plate per line):");
for (int car : driveway) {
System.out.println(car);
}

System.out.println("In Street, starting at first in (one license plate per line):");
for (int car : street) {
System.out.println(car);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ public class FirstLetterMap
{
public static void main(String[] args)
{
String filename = "src/test1.txt";
String filename = "Chapter 15 Activities/FirstLetterMap/FirstLetterMap1/src/test1.txt";

try (Scanner in = new Scanner(new File(filename)))
{

// Create your map here
...
Map<Character, Set<String>> firstLetterMap = new TreeMap<>();

while (in.hasNext())
{
Expand All @@ -27,13 +26,23 @@ public static void main(String[] args)

// Update the map here
// Use the Java 8 merge method
. . .
if (word.length() > 0) {

// Use the Java 8 merge method to update the map
firstLetterMap.merge(c, new TreeSet<>(Set.of(word)), (existingSet, newSet) -> {
existingSet.addAll(newSet);
return existingSet;
});
}

}

// Print the map here in this form
// a: [a, able, aardvark]
. . .

for (Map.Entry<Character, Set<String>> entry : firstLetterMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
} catch (FileNotFoundException e)
{
System.out.println("Cannot open: " + filename);
Expand All @@ -53,5 +62,5 @@ public static String clean(String s)
}
return r.toLowerCase();
}

//in.close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,50 @@
* Read all words from a file and add them to a map
* whose keys are the first letters of the words and
* whose values are sets of words that start with
* that same letter. Then print out the word sets in
* alphabetical order. Update the map by modifying
* Worked Example 15.1.
* that same letter.
* Then print out the word sets in alphabetical order.
* Use the Java 8 merge() feature.
*/
public class FirstLetterMap
{
public static void main(String[] args)
{
String filename = "src/test1.txt";
String filename = "Chapter 15 Activities/FirstLetterMap/FirstLetterMap2/src/test1.txt";

try (Scanner in = new Scanner(new File(filename)))
{

// Create your map here
...
Map<Character, Set<String>> firstLetterMap = new TreeMap<>();

while (in.hasNext())
{
String word = clean(in.next());
Character c = word.charAt(0);

// Update the map here
// Modify Worked Example 15.1
. . .
if (word.length() > 0) {


}
if (firstLetterMap.containsKey(c)) {
// If the key exists, add the word to the corresponding set
Set<String> existingSet = firstLetterMap.get(c);
existingSet.add(word); // Add word to the set
} else {
// If the key does not exist, create a new set with the word
Set<String> newSet = new TreeSet<>(); // TreeSet to automatically sort
newSet.add(word);
firstLetterMap.put(c, newSet); // Put the new set in the map
}
}
}


// Print the map here in this form
// a: [a, able, aardvark]
. . .


for (Map.Entry<Character, Set<String>> entry : firstLetterMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
} catch (FileNotFoundException e)
{
System.out.println("Cannot open: " + filename);
Expand All @@ -54,4 +67,5 @@ public static String clean(String s)
}
return r.toLowerCase();
}
//in.close();
}
Loading