diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea1bd37 --- /dev/null +++ b/.gitignore @@ -0,0 +1,356 @@ +# Created by .ignore support plugin (hsz.mobi) +### Example user template template +### Example user template + +# IntelliJ project files +.idea +*.iml +out +gen### Kotlin template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +### Maven template +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +### macOS template +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk +### VisualStudioCode template +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +### Android template +# Built application files +*.apk +*.ap_ + +# Files for the ART/Dalvik VM +*.dex + +# Java class files + +# Generated files +bin/ +gen/ +out/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ +.idea/workspace.xml +.idea/tasks.xml +.idea/gradle.xml +.idea/assetWizardSettings.xml +.idea/dictionaries +.idea/libraries +.idea/caches + +# Keystore files +# Uncomment the following line if you do not want to check your keystore files in. +#*.jks + +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild + +# Google Services (e.g. APIs or Firebase) +google-services.json + +# Freeline +freeline.py +freeline/ +freeline_project_description.json + +# fastlane +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots +fastlane/test_output +fastlane/readme.md +### Eclipse template + +.metadata +tmp/ +*.tmp +*.bak +*.swp +*~.nib +.settings/ +.loadpath +.recommenders + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# CDT- autotools +.autotools + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +# Annotation Processing +.apt_generated/ + +# Scala IDE specific (Scala & Java development for Eclipse) +.cache-main +.scala_dependencies +.worksheet +### Gradle template +.gradle +/build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar + +# Cache of project +.gradletasknamecache + +# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 +# gradle/wrapper/gradle-wrapper.properties +### Java template +# Compiled class file + +# Log file + +# BlueJ files + +# Mobile Tools for Java (J2ME) + +# Package Files # + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +### Linux template +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* +### Windows template +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk +### NetBeans template +nbproject/private/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests +### Vim template +# Swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim + +# Temporary +.netrwhist +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +# Created by https://www.gitignore.io/api/bluej + +### bluej ### +# BlueJ: A Java IDE for programming beginners. http://bluej.org + +# Editor preferences for a class etc, see http://lists.bluej.org/pipermail/bluej-discuss/2003-May/002351.html +# *.ctxt (ignored before in this file) + +# Duplicate of project file, see http://www.hs-owl.de/fb5/labor/it/de/if1/vl/IF1JavaOhneBluej.pdf, page 4 +bluej.pkh + + +# End of https://www.gitignore.io/api/bluej diff --git a/src/exercise/LinkedList.java b/src/exercise/LinkedList.java deleted file mode 100644 index 17cbc2b..0000000 --- a/src/exercise/LinkedList.java +++ /dev/null @@ -1,94 +0,0 @@ -package exercise; - -class Node { - int elem; - Node next; - - Node(int i, Node n) { - elem = i; - next = n; - } -} - -public class LinkedList { - - // storage for elements - Node first; - - public LinkedList() { - first = null; - } - - /** - * Adds an integer to the end of this list. - * - * @param i the integer to add - */ - void add(int i) { - Node newNode = new Node(i, null); - - if (first == null) { // list is empty - first = newNode; - } else { // list is not empty - // 1. step find the last node - Node current = first; - - while (current.next != null) { - // current is not the last node! - current = current.next; - } - // current.next == null - // that means that current is the last node! YAY! :-) - - // 2. step insert new node - current.next = newNode; - } - } - - /** - * Return element at given index. - * - * @param index - * @return element at index - * @throws {@link IndexOutOfBoundsException} if index is negative - */ - int get(int index) { - if (index < 0) { - throw new IndexOutOfBoundsException(); - } - - int i = 0; - Node current = first; - - while (index != i && current.next != null) { - i++; - current = current.next; - } - - if (index <= i) { - // here, we are at the right place! - return current.elem; - } else { - throw new IndexOutOfBoundsException(); - } - } - - void printAll() { - if (first == null) { // list is empty - System.out.print(""); - } else { - // we know that first is not null! - System.out.print("["); - - Node current = first; - while (current.next != null) { - System.out.print(current.elem + ","); - current = current.next; - } - // print last element - System.out.print(current.elem); - - System.out.print("]"); - } - } -} diff --git a/src/exercise/Main.java b/src/exercise/Main.java index 903f8e7..f3eedf7 100644 --- a/src/exercise/Main.java +++ b/src/exercise/Main.java @@ -1,18 +1,20 @@ package exercise; +import exercise.linkedlist.LinkedList; + public class Main { public static void main(String[] args) { LinkedList l = new LinkedList(); - l.add(5); + l.addValue(5); - int res = l.get(0); + int res = l.getValue(0); System.out.println("res: " + res); - l.add(7); + l.addValue(7); - int res2 = l.get(1); + int res2 = l.getValue(1); System.out.println("res2: " + res2); l.printAll(); diff --git a/src/exercise/linkedlist/LinkedList.java b/src/exercise/linkedlist/LinkedList.java new file mode 100644 index 0000000..e973d42 --- /dev/null +++ b/src/exercise/linkedlist/LinkedList.java @@ -0,0 +1,95 @@ +package exercise.linkedlist; + +public class LinkedList { + + // storage for elements + private Node first; + + public LinkedList() { + first = null; + } + + /** + * Adds an integer to the end of this list. + * + * @param i the integer to addValue + */ + public void addValue(int i) { + Node newNode = new Node(i, null); + add(newNode); + } + + /** + * Add a node to the list + * + * @param node Node to be added + */ + public void add(Node node) { + if (first == null) { // list is empty + first = node; + } else { // list is not empty + // 1. step find the last node + Node current = first; + while (current.getNext() != null) { + // current is not the last node! + current = current.getNext(); + } + // current.next == null + // that means that current is the last node! YAY! :-) + // 2. step insert new node + current.setNext(node); + } + } + + /** + * Return element at given index. + * + * @param index + * @return element at index + * @throws {@link IndexOutOfBoundsException} if index is negative + */ + public int getValue(int index) { + return get(index).getElem(); + } + + /** + * Get Node at index + * + * @param index Index of Node + * @return {@link Node} at the given index + */ + public Node get(int index) { + if (index < 0) { + throw new IndexOutOfBoundsException(); + } + int i = 0; + Node current = first; + while (index != i && current.getNext() != null) { + i++; + current = current.getNext(); + } + if (index <= i) { + // here, we are at the right place! + return current; + } else { + throw new IndexOutOfBoundsException(); + } + } + + public void printAll() { + if (first == null) { // list is empty + System.out.print(""); + } else { + // we know that first is not null! + System.out.print("["); + Node current = first; + while (current.getNext() != null) { + System.out.print(current.getElem() + ","); + current = current.getNext(); + } + // print last element + System.out.print(current.getElem()); + System.out.print("]"); + } + } +} diff --git a/src/exercise/linkedlist/Node.java b/src/exercise/linkedlist/Node.java new file mode 100644 index 0000000..5d8db8d --- /dev/null +++ b/src/exercise/linkedlist/Node.java @@ -0,0 +1,28 @@ +package exercise.linkedlist; + +public class Node { + + private int elem; + private Node next; + + public Node(int i, Node n) { + elem = i; + next = n; + } + + public int getElem() { + return elem; + } + + public void setElem(int elem) { + this.elem = elem; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } +} diff --git a/test/exercise/linkedlist/LinkedListTest.java b/test/exercise/linkedlist/LinkedListTest.java new file mode 100644 index 0000000..8341200 --- /dev/null +++ b/test/exercise/linkedlist/LinkedListTest.java @@ -0,0 +1,85 @@ +package exercise.linkedlist; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class LinkedListTest { + + private LinkedList linkedList; + + @BeforeEach + void setUp() { + linkedList = new LinkedList(); + } + + @AfterEach + void tearDown() { + linkedList = null; + } + + @Test + void addValue() { + int expected = 105; + linkedList.addValue(105); + assertEquals(expected, linkedList.getValue(0)); + } + + @Test + void addToEmptyLinkedList() { + Node expectedNode = new Node(303, null); + linkedList.add(expectedNode); + assertEquals(expectedNode, linkedList.get(0)); + } + + @Test + void addToNonEmptyLinkedList() { + Node someNode = new Node(101, null); + linkedList.add(someNode); + Node expectedNode = new Node(303, null); + linkedList.add(expectedNode); + assertEquals(expectedNode, linkedList.get(1)); + } + + @Test + void addToNonEmptyLinkedListWithMoreThanOneNode() { + Node firstNode = new Node(909, null); + linkedList.add(firstNode); + Node someNode = new Node(101, null); + linkedList.add(someNode); + Node expectedNode = new Node(303, null); + linkedList.add(expectedNode); + assertEquals(expectedNode, linkedList.get(2)); + } + + @Test + void getValue() { + int expected = 202; + linkedList.addValue(expected); + assertEquals(expected, linkedList.getValue(0)); + } + + @Test + void get() { + Node expectedNode = new Node(303, null); + linkedList.add(expectedNode); + assertEquals(expectedNode, linkedList.get(0)); + } + + @Test + void positiveIndexOutOfBoundException() { + Node newNode = new Node(303, null); + linkedList.add(newNode); + assertThrows(IndexOutOfBoundsException.class, () -> linkedList.getValue(1)); + } + + @Test + void negativeIndexOutOfBoundException() { + Node newNode = new Node(303, null); + linkedList.add(newNode); + assertThrows(IndexOutOfBoundsException.class, () -> linkedList.getValue(-1)); + } +} diff --git a/test/exercise/linkedlist/NodeTest.java b/test/exercise/linkedlist/NodeTest.java new file mode 100644 index 0000000..bb822cd --- /dev/null +++ b/test/exercise/linkedlist/NodeTest.java @@ -0,0 +1,51 @@ +package exercise.linkedlist; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class NodeTest { + + public static final int VALUE = 123; + private Node node; + private Node nextNode; + + @BeforeEach + void setUp() { + nextNode = new Node(400, null); + node = new Node(VALUE, nextNode); + } + + @AfterEach + void tearDown() { + node = null; + nextNode = null; + } + + @Test + void getElem() { + assertEquals(VALUE, node.getElem()); + } + + @Test + void setElem() { + int expected = 109; + node.setElem(expected); + assertEquals(expected, node.getElem()); + } + + @Test + void getNext() { + assertEquals(nextNode,node.getNext()); + } + + @Test + void setNext() { + nextNode = new Node(300,null); + node.setNext(nextNode); + assertEquals(nextNode,node.getNext()); + } +}