HW3: preparing for submission

This commit is contained in:
Claudio Maggioni 2019-12-09 21:44:08 +01:00
parent cb78907485
commit 601abefbcf
10 changed files with 766 additions and 0 deletions

12
hw3/.idea/hw3.iml Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/out" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

9
hw3/.idea/misc.xml Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_12" default="false" project-jdk-name="12.0.1" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
hw3/.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/hw3.iml" filepath="$PROJECT_DIR$/.idea/hw3.iml" />
</modules>
</component>
</project>

6
hw3/.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

495
hw3/.idea/workspace.xml Normal file
View file

@ -0,0 +1,495 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="cf07aeb8-47a7-429b-9024-1f8bf10e85cc" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/ex3_test.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../hw2/Ex2/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../hw2/Ex2/.idea/workspace.xml" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="DefaultGradleProjectSettings">
<option name="isMigrated" value="true" />
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/submission.tex">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="367">
<caret line="53" column="25" selection-start-line="53" selection-start-column="25" selection-end-line="53" selection-end-column="25" />
<folding>
<element signature="e#39#66#2" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/fp/IntOperatorsUtil.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="165">
<caret line="12" column="5" selection-start-line="12" selection-start-column="5" selection-end-line="12" selection-end-column="5" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/fp/FunctionsUtil.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="135">
<caret line="10" selection-start-line="10" selection-end-line="10" />
<folding>
<element signature="e#194#195#0" expanded="true" />
<element signature="e#229#230#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/streams/FileLinesProcessor.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="280">
<caret line="99" column="22" selection-start-line="99" selection-start-column="22" selection-end-line="99" selection-end-column="22" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/ex3_test.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="60">
<caret line="4" selection-start-line="4" selection-end-line="4" />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="FindInProjectRecents">
<findStrings>
<find>lang</find>
</findStrings>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/src/Cose.java" />
<option value="$PROJECT_DIR$/src/fp/IntOperatorsUtil.java" />
<option value="$PROJECT_DIR$/src/fp/FunctionsUtil.java" />
<option value="$PROJECT_DIR$/ex3_test.txt" />
<option value="$PROJECT_DIR$/src/streams/FileLinesProcessor.java" />
<option value="$PROJECT_DIR$/submission.tex" />
</list>
</option>
</component>
<component name="ProjectConfigurationFiles">
<option name="files">
<list>
<option value="$PROJECT_DIR$/.idea/hw3.iml" />
<option value="$PROJECT_DIR$/.idea/misc.xml" />
<option value="$PROJECT_DIR$/.idea/vcs.xml" />
<option value="$PROJECT_DIR$/.idea/modules.xml" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" fullScreen="true">
<option name="width" value="1680" />
<option name="height" value="1050" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="hw3" type="b2602c69:ProjectViewProjectNode" />
<item name="hw3" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="hw3" type="b2602c69:ProjectViewProjectNode" />
<item name="hw3" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="hw3" type="b2602c69:ProjectViewProjectNode" />
<item name="hw3" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
<item name="fp" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="hw3" type="b2602c69:ProjectViewProjectNode" />
<item name="hw3" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
<item name="streams" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="Scope" />
<pane id="PackagesPane" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_npm_path_reset_for_default_project" value="true" />
<property name="project.structure.last.edited" value="Project" />
<property name="project.structure.proportion" value="0.15" />
<property name="project.structure.side.proportion" value="0.2" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager" selected="LaTeX.submission">
<configuration name="FileLinesProcessor" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="streams.FileLinesProcessor" />
<module name="hw3" />
<option name="VM_PARAMETERS" value="-ea" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="streams.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="FunctionsUtil" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="fp.FunctionsUtil" />
<module name="hw3" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="fp.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="IntOperatorsUtil" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="fp.IntOperatorsUtil" />
<module name="hw3" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="fp.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="submission bibliography" type="BIBTEX_RUN_CONFIGURATION" factoryName="LaTeX configuration factory">
<texify-bibtex>
<compiler>BIBTEX</compiler>
<compiler-path />
<compiler-arguments />
<main-file>$PROJECT_DIR$/submission.tex</main-file>
<aux-dir />
</texify-bibtex>
<method v="2" />
</configuration>
<configuration name="submission (1)" type="LATEX_RUN_CONFIGURATION" factoryName="LaTeX configuration factory" temporary="true">
<texify>
<compiler>PDFLATEX</compiler>
<compiler-path />
<sumatra-path />
<viewer-command />
<compiler-arguments />
<main-file>$PROJECT_DIR$/submission.tex</main-file>
<aux-dir>false</aux-dir>
<out-dir>false</out-dir>
<output-format>PDF</output-format>
<bib-run-config>BibTeX.submission bibliography</bib-run-config>
</texify>
<method v="2" />
</configuration>
<configuration name="submission" type="LATEX_RUN_CONFIGURATION" factoryName="LaTeX configuration factory">
<texify>
<compiler>PDFLATEX</compiler>
<compiler-path>/Library/TeX/texbin/pdflatex</compiler-path>
<sumatra-path />
<viewer-command>open -a Skim {pdf}</viewer-command>
<compiler-arguments />
<main-file>$PROJECT_DIR$/submission.tex</main-file>
<aux-dir>false</aux-dir>
<out-dir>false</out-dir>
<output-format>PDF</output-format>
<bib-run-config>BibTeX.submission bibliography</bib-run-config>
</texify>
<method v="2" />
</configuration>
<list>
<item itemvalue="Application.IntOperatorsUtil" />
<item itemvalue="Application.FunctionsUtil" />
<item itemvalue="Application.FileLinesProcessor" />
<item itemvalue="BibTeX.submission bibliography" />
<item itemvalue="LaTeX.submission" />
<item itemvalue="LaTeX.submission (1)" />
</list>
<recent_temporary>
<list>
<item itemvalue="Application.FileLinesProcessor" />
<item itemvalue="Application.FunctionsUtil" />
<item itemvalue="Application.IntOperatorsUtil" />
<item itemvalue="LaTeX.submission (1)" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="cf07aeb8-47a7-429b-9024-1f8bf10e85cc" name="Default Changelist" comment="" />
<created>1575886303360</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1575886303360</updated>
<workItem from="1575886304499" duration="94000" />
<workItem from="1575886414622" duration="3616000" />
<workItem from="1575903832348" duration="10727000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="14437000" />
</component>
<component name="ToolWindowManager">
<frame x="0" y="0" width="1015" height="1050" extended-state="0" />
<editor active="true" />
<layout>
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.43062693" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Designer" order="2" />
<window_info id="Image Layers" order="3" />
<window_info id="Capture Tool" order="4" />
<window_info id="UI Designer" order="5" />
<window_info id="Learn" order="6" />
<window_info id="Favorites" order="7" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info anchor="bottom" id="Run" order="2" visible="true" weight="0.32959184" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
<window_info anchor="bottom" id="Version Control" order="8" />
<window_info anchor="bottom" id="Database Changes" order="9" />
<window_info anchor="bottom" id="Terminal" order="10" />
<window_info anchor="bottom" id="Event Log" order="11" side_tool="true" />
<window_info anchor="bottom" id="Messages" order="12" weight="0.32959184" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="right" id="Palette" order="3" />
<window_info anchor="right" id="Theme Preview" order="4" />
<window_info anchor="right" id="Maven" order="5" />
<window_info anchor="right" id="Capture Analysis" order="6" />
<window_info anchor="right" id="Palette&#9;" order="7" />
<window_info anchor="right" id="Database" order="8" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="jar:///Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home/lib/src.zip!/java.base/java/util/stream/Collectors.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="318">
<caret line="317" column="28" selection-start-line="317" selection-start-column="28" selection-end-line="317" selection-end-column="28" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/ex3_test.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="60">
<caret line="4" selection-start-line="4" selection-end-line="4" />
</state>
</provider>
</entry>
<entry file="jar:///Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home/lib/src.zip!/java.base/java/util/Collection.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="2">
<caret line="694" column="7" lean-forward="true" selection-start-line="694" selection-start-column="7" selection-end-line="694" selection-end-column="7" />
<folding>
<element signature="e#35698#35699#0" expanded="true" />
<element signature="e#35763#35764#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="jar:///Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home/lib/src.zip!/java.base/java/util/stream/Stream.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="90">
<caret line="748" column="6" lean-forward="true" selection-start-line="748" selection-start-column="6" selection-end-line="748" selection-end-column="6" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Cose.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="315">
<caret line="21" lean-forward="true" selection-start-line="21" selection-end-line="21" />
<folding>
<element signature="e#292#293#0" expanded="true" />
<element signature="e#347#348#0" expanded="true" />
<element signature="e#395#396#0" expanded="true" />
<element signature="e#490#491#0" expanded="true" />
<element signature="e#521#522#0" expanded="true" />
<element signature="e#607#608#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/fp/FunctionsUtil.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="135">
<caret line="10" selection-start-line="10" selection-end-line="10" />
<folding>
<element signature="e#194#195#0" expanded="true" />
<element signature="e#229#230#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/streams/FileLinesProcessor.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="280">
<caret line="99" column="22" selection-start-line="99" selection-start-column="22" selection-end-line="99" selection-end-column="22" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/fp/IntOperatorsUtil.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="165">
<caret line="12" column="5" selection-start-line="12" selection-start-column="5" selection-end-line="12" selection-end-column="5" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/submission.tex">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="367">
<caret line="53" column="25" selection-start-line="53" selection-start-column="25" selection-end-line="53" selection-end-column="25" />
<folding>
<element signature="e#39#66#2" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component>
<component name="masterDetails">
<states>
<state key="ArtifactsStructureConfigurable.UI">
<settings>
<artifact-editor />
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="FacetStructureConfigurable.UI">
<settings>
<last-edited>No facets are configured</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="GlobalLibrariesConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="JdkListConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ModuleStructureConfigurable.UI">
<settings>
<last-edited>hw3</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
<option value="0.6" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ProjectLibrariesConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

4
hw3/ex3_test.txt Normal file
View file

@ -0,0 +1,4 @@
abc
xyz
hello world
play with streams

View file

@ -0,0 +1,15 @@
package fp;
import java.util.function.BiFunction;
import java.util.function.Function;
public class FunctionsUtil {
public static <T, U, V> Function<U, V> partial(BiFunction<T, U, V> f, T x) {
return u -> f.apply(x, u);
}
public static void main(String... args) {
BiFunction<String, String, Boolean> f = String::startsWith;
System.out.println(FunctionsUtil.partial(f, "abc").apply("a")); // true
}
}

View file

@ -0,0 +1,31 @@
package fp;
import java.util.function.IntBinaryOperator;
import java.util.function.IntUnaryOperator;
public class IntOperatorsUtil {
public static IntUnaryOperator compose(IntUnaryOperator... functions) {
IntUnaryOperator f = i -> i;
for (IntUnaryOperator g : functions) {
f = f.andThen(g);
}
return f;
}
public static IntUnaryOperator partial(IntBinaryOperator f, int x) {
return i -> f.applyAsInt(x, i);
}
public static void main(String... args) {
IntUnaryOperator[] functions = {
compose(),
compose(x -> x + 1),
compose(x -> x * 2, x -> x + 1),
compose(x -> x + 1, x -> x * 2),
compose(x -> x + 1, x -> x * 2, x -> x + 1)
};
for (IntUnaryOperator f : functions) System.out.println(f.applyAsInt(1));
IntOperatorsUtil.partial((x, y) -> x-y, 1).applyAsInt(2); // -1
}
}

View file

@ -0,0 +1,115 @@
package streams;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class FileLinesProcessor {
private final String filename;
FileLinesProcessor(String filename) {
this.filename = filename;
}
private Stream<String> getLines() throws IOException {
return Files.lines(Paths.get(filename));
}
public byte[][] fileLinesAsBytes() throws IOException {
return getLines().map(String::getBytes).toArray(byte[][]::new);
}
public long countByNumberOfSpaces(int n) throws IOException {
return getLines()
.map(s -> s.chars()
.filter(c -> c == ' ')
.count())
.filter(c -> c == n).count();
}
public String concatNth(int n) throws IOException {
return getLines()
.map(s -> n >= s.length() ? "" : s.substring(n, n + 1))
.collect(Collectors.joining());
}
public Map<Integer, List<String>> groupByLineLength() throws IOException {
return getLines()
.collect(Collectors.groupingBy(String::length, Collectors.toList()));
}
public Map<Character, Long> countCharsOccurrences() throws IOException {
return getLines()
.map(String::chars)
.reduce(IntStream.of(), IntStream::concat)
.boxed()
.collect(Collectors.groupingBy(i -> (char) (i & 0xFFFF), Collectors.counting()));
}
public Map<Character, Long> countCharsOccurrences(int n) throws IOException {
return getLines()
.map(String::chars)
.reduce(IntStream.of(), IntStream::concat)
.boxed()
.collect(Collectors.groupingBy(i -> (char) (i & 0xFFFF), Collectors.counting()))
.entrySet()
.stream()
.filter(e -> e.getValue() > n)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
public static void main(final String... args) throws IOException {
final FileLinesProcessor processor = new FileLinesProcessor("ex3_test.txt");
assert processor.fileLinesAsBytes()[0][1] == 'b';
assert processor.fileLinesAsBytes()[1][2] == 'z';
assert processor.countByNumberOfSpaces(0) == 2; // (i.e., "abc" and "xyz")
assert processor.countByNumberOfSpaces(2) == 1; // (i.e., "play with streams")
assert "axhp".equals(processor.concatNth(0));
assert "byel".equals(processor.concatNth(1));
assert "ly".equals(processor.concatNth(3));
final Map<Integer, List<String>> expected = new HashMap<>();
expected.put(3, Arrays.asList("abc", "xyz"));
expected.put(11, Collections.singletonList("hello world"));
expected.put(17, Collections.singletonList("play with streams"));
assert expected.equals(processor.groupByLineLength()); // Expected result: {3=[abc, xyz], 11=[hello world],
// 17=[play with streams]}
final Map<Character, Long> expected2 = new HashMap<>();
expected2.put(' ', 3L);
expected2.put('a', 3L);
expected2.put('b', 1L);
expected2.put('c', 1L);
expected2.put('d', 1L);
expected2.put('e', 2L);
expected2.put('h', 2L);
expected2.put('i', 1L);
expected2.put('l', 4L);
expected2.put('m', 1L);
expected2.put('o', 2L);
expected2.put('p', 1L);
expected2.put('r', 2L);
expected2.put('s', 2L);
expected2.put('t', 2L);
expected2.put('w', 2L);
expected2.put('x', 1L);
expected2.put('y', 2L);
expected2.put('z', 1L);
assert expected2.equals(processor.countCharsOccurrences()); // Expected result { =3, a=3, b=1, c=1, d=1, e=2,
// h=2, i=1, l=4, m=1, o=2, p=1, r=2, s=2, t=2, w=2,
// x=1, y=2, z=1}
final Map<Character, Long> expected3 = new HashMap<>();
expected3.put(' ', 3L);
expected3.put('a', 3L);
expected3.put('l', 4L);
assert expected3.equals(processor.countCharsOccurrences(2)); // Expected result: { =3, a=3, l=4}
System.out.println("OK");
}
}

71
hw3/submission.tex Normal file
View file

@ -0,0 +1,71 @@
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=2cm]{geometry}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{lmodern}
\title{Howework 3 -- Programming Fundamentals 3}
\author{Claudio Maggioni}
\lstset{
basicstyle=\small\ttfamily,
frame=shadowbox,
rulesepcolor=\color{black},
columns=fullflexible,
commentstyle=\color{gray},
keywordstyle=\bfseries,
escapeinside={\%*}{*)},
aboveskip=2em,
captionpos=b,
abovecaptionskip=1em,
belowcaptionskip=1em
}
\begin{document}
\maketitle
\tableofcontents
\section{Exercise 1}
\subsection{Exercise 1.1}
\paragraph{Example 1:}
The code will compile and run normally since Integer implements Number
and therefore it is a satisfiable parameter for
\texttt{Box<Number>.put(Number)}.
\paragraph{Example 2:}
The code will compile, but it will terminate with a NullPointerException.
The implicit unboxing of the return value of \texttt{iBox.get()} will call
the method \texttt{intValue()} on the object returned. But, since the Box object
contains an empty reference, this call will fail and thus cause a
\texttt{NullPointerException}.
\paragraph{Example 3:}
This code will not compile since \texttt{Box<Integer>.put()} requires an integer
as an argument, but a \texttt{double} is provided.
\subsection{Exercise 1.2}
\begin{lstlisting}[language=Java, caption=New signature of Box.put(...)]
public void put(Box<? extends T> box) { ... };
\end{lstlisting}
\subsection{Exercise 1.3}
\begin{lstlisting}[language=Java, caption=New signature of Box.put(...)]
public boolean containsSame(Box<? extends T> o,
EqualityComparator<? super T> c) { ... };
\end{lstlisting}
\section{Exercise 2}
\subsection{Exercise 2.1}
\begin{lstlisting}[language=Java, caption=BiPredicate with lambda]
BiPredicate<String, String> firstStartsWithSecond = (s, s2) -> s.startsWith(s2);
\end{lstlisting}
\begin{lstlisting}[language=Java, caption=BIPredicate with method reference]
BiPredicate<String, String> firstStartsWithSecond = String::startsWith;
\end{lstlisting}
\end{document}