Commit d36999ce authored by Your Name's avatar Your Name

added ue12

parent 291c1c9f
This source diff could not be displayed because it is too large. You can view the blob instead.
File added
From this distant vantage point, the Earth might not seem of any particular interest. But for us, it's different. Consider again that dot. That's here, that's home, that's us. On it everyone you love, everyone you know, everyone you ever heard of, every human being who ever was, lived out their lives. The aggregate of our joy and suffering, thousands of confident religions, ideologies, and economic doctrines, every hunter and forager, every hero and coward, every creator and destroyer of civilization, every king and peasant, every young couple in love, every mother and father, hopeful child, inventor and explorer, every teacher of morals, every corrupt politician, every superstar, every supreme leader, every saint and sinner in the history of our species lived there. On the mote of dust suspended in a sunbeam.
\ No newline at end of file
peasant: 1
but: 1
leader: 1
couple: 1
civilization: 1
forager: 1
suffering: 1
hopeful: 1
mother: 1
that: 1
religions: 1
doctrines: 1
from: 1
consider: 1
hunter: 1
different: 1
dust: 1
you: 3
us: 2
mote: 1
lives: 1
in: 3
king: 1
everyone: 3
it's: 1
might: 1
distant: 1
this: 1
supreme: 1
particular: 1
history: 1
coward: 1
being: 1
it: 1
thousands: 1
suspended: 1
ever: 2
sunbeam: 1
explorer: 1
know: 1
child: 1
who: 1
love: 2
here: 1
young: 1
ideologies: 1
father: 1
confident: 1
vantage: 1
dot: 1
for: 1
morals: 1
their: 1
hero: 1
that's: 3
destroyer: 1
our: 2
point: 1
aggregate: 1
out: 1
seem: 1
not: 1
teacher: 1
joy: 1
interest: 1
and: 9
of: 8
inventor: 1
lived: 2
human: 1
every: 12
heard: 1
on: 2
a: 1
creator: 1
saint: 1
corrupt: 1
politician: 1
again: 1
was: 1
any: 1
home: 1
the: 4
species: 1
there: 1
earth: 1
superstar: 1
sinner: 1
economic: 1
\ No newline at end of file
Wait a minute, Doc. Ah... Are you telling me you built a time machine... out of a DeLorean?
These aren't the droids you're looking for.
Extraordinary claims require extraordinary evidence.
\ No newline at end of file
/**
* functional interface
*/
public interface BiPredicate<T> {
boolean test(T e1, T e2);
/**
* @param e1 first element
* @param e2 second element
* @return true, if e1 is better element, false, if e2 is better
*/
static <T> T getBetterElement(T e1, T e2, BiPredicate<T> pred) {
return ((pred.test(e1, e2) == true) ? e1 : e2);
}
}
import java.io.*;
public class ReadWriteFiles {
public static void main(String[] args) throws IOException {
// a)
FileInputStream fis = new FileInputStream("oop.dat.sec");
DataInputStream dis = new DataInputStream(fis);
int bytesToLetter = 0;
while (bytesToLetter >= 0) {
System.out.print(dis.readChar());
bytesToLetter = dis.readInt();
dis.skipBytes(bytesToLetter);
}
dis.close();
// b)
// fileA: 110.005 Bytes
// fileB: 44.002 Bytes
// die Zahlen werden bei fileB (mit writeShort) direkt als short geschrieben
// bei fileA (mit print) werden sie noch nach Standard-Zeichenkodierung uebersetzt
File fileA = new File("fileA");
fileA.createNewFile();
PrintStream psA = new PrintStream(fileA);
for (short num = 10000; num <= 32000; num++) {
psA.print(num);
}
psA.close();
File fileB = new File("fileB");
fileB.createNewFile();
FileOutputStream fosB = new FileOutputStream(fileB);
DataOutputStream disB = new DataOutputStream(fosB);
for (short num = 10000; num <= 32000; num++) {
disB.writeShort(num);
}
disB.close();
}
}
package de.umr.oop;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Stream;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class IOStreams {
/**
* reads file and returns stream of words.
* @param filePath
* @return stream of words
* @throws IOException
*/
public static Stream<String> words(String filePath) throws IOException {
try {
FileReader fisB = new FileReader(filePath);
BufferedReader br = new BufferedReader(fisB);
String line;
String words = "";
while ((line = br.readLine()) != null) {
words += line + " ";
}
String[] wordArray = words
.replaceAll("[,.?!;(){}]", "")
.split(" ");
Stream<String> result = Arrays.asList(wordArray).stream();
return result;
} catch (IOException e) {
throw e;
}
}
/**
* encrypts stream of words with Caesar cipher.
* @param words stream of words to be encrypted
* @param offset key used to encrypt words
* @return encrypted stream of words
*/
public static Stream<String> encrypt(Stream<String> words, int offset) {
List<String> wordList = new ArrayList<>();
// converts words to uppercase, replaces umlaute and turns words into char arrays
Stream<char[]> charArrays = words
.map(x -> {
return x.toUpperCase()
.replaceAll("Ä", "AE")
.replaceAll("Ö", "OE")
.replaceAll("Ü", "UE")
.replaceAll("ß", "SS")
.toCharArray();
});
//encrypts words according to offset
charArrays = charArrays
.map(x -> {
char[] newX = new char[x.length];
for (int i = 0; i < x.length; i++) {
if (x[i] == '\'') {
newX[i] = x[i];
} else {
if (x[i] + offset > 'Z') {
newX[i] = (char) ('A' + offset - ('Z' - x[i]) - 1);
} else {
newX[i] = (char) (x[i] + offset);
}
}
}
return newX;
});
charArrays.forEach((char[] x) -> {
wordList.add(new String(x));
});
Stream<String> result = wordList.stream();
return result;
}
/**
* counts occurrences of all words in given word stream.
* @param words given word stream
* @return Map<String,Integer>
* String key is the word and
* Integer value is the number of occurrences of the key
*/
public static Map<String, Integer> countWords(Stream<String> words) {
Map<String, Integer> result;
Map<String, Long> resultLong;
resultLong = words
.map(x -> x.toLowerCase())
.collect(Collectors.groupingBy(Function.identity(), (Collectors.counting())));
result = resultLong
.entrySet().stream()
.collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue().intValue()));
return result;
}
}
package de.umr.oop;
import org.junit.Assert;
import org.junit.Test;
import java.io.*;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Scanner;
public class IOStreamsTest {
@Test
public void wordsTest() throws IOException {
Stream<String> words = IOStreams.words("quotes.txt");
Stream<String> wordsToPrint = IOStreams.words("quotes.txt");
Assert.assertEquals(30, words.count());
wordsToPrint.forEach(x -> System.out.print("[" + x + "]"));
}
@Test
public void encryptTest() throws IOException {
Stream<String> actualText = IOStreams.words("paleBlueDot.txt");
Stream<String> expectedText = IOStreams.words("paleBlueDot.txt");
Stream<String> printText = IOStreams.words("paleBlueDot.txt");
Stream<String> encryptedText = IOStreams.encrypt(actualText, 13);
encryptedText = IOStreams.encrypt(encryptedText, 13);
Stream<String> actualStream = encryptedText;
printText = IOStreams.encrypt(printText, 13);
printText = IOStreams.encrypt(printText, 13);
String actualString = actualStream.collect(Collectors.joining());
String expectedString = expectedText.collect(Collectors.joining());
expectedString = expectedString.toUpperCase();
Assert.assertEquals(expectedString, actualString);
printText.forEach(x -> System.out.print("[" + x + "]"));
}
@Test
public void countWordsTest() throws IOException {
FileInputStream fis = new FileInputStream("paleBlueDot.txt");
Scanner s = new Scanner(fis).useDelimiter("\\A");
String text = s.hasNext() ? s.next() : "";
text = text.replaceAll("[.,!?]","");
Stream<String> words = Arrays.stream(text.split(" "));
Map<String, Integer> numWords = IOStreams.countWords(words);
System.out.print(numWords);
}
}
\ No newline at end of file
<?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" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment