first commit

This commit is contained in:
marguesto 2022-06-01 20:57:28 +02:00
commit 6408f107b0
64 changed files with 8937 additions and 0 deletions

1
README.md Normal file
View File

@ -0,0 +1 @@
### practicing java for school

BIN
Vehicle.class Normal file

Binary file not shown.

BIN
lain.class Normal file

Binary file not shown.

21
lain.java Normal file
View File

@ -0,0 +1,21 @@
class test{
void noChange(int i, int j){
i++;
j--;
}
}
class lain{
public static void main(String[] args){
test ob = new test();
int a=15, b=20;
String orgstr = "java sucks";
String substr = orgstr.substring(0,orgstr.length());
System.out.println(substr);
String str = new String("Hello");
String str02 = new String("lmao");
System.out.println(str.equals(str02));
}
}

BIN
lain/lain.class Normal file

Binary file not shown.

19
lain/lain.java Normal file
View File

@ -0,0 +1,19 @@
class FailSoftArray{
private int a[];
private int errval;
public int length;
public FailSoftArray(int size, int errv){
a = new int[size];
errval = errv;
length = size;
}
}
class lain{
public static void main(String[] args){
System.out.println("lmao");
System.out.println("lmao");
}
}

View File

@ -0,0 +1,82 @@
package utils;
//https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/TreeSet.html
import java.util.TreeSet;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
//https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Objects.html
import java.util.Objects;
public class Dictionary
{
private TreeSet<String> wordList = new TreeSet<>();
public Dictionary(String fileName)
{
try (BufferedReader br = new BufferedReader(
new FileReader(new File(fileName)))) {
String line;
while ((line = br.readLine()) != null) {
wordList.add(line);
}
} catch (FileNotFoundException e) {
} catch (IOException e) {}
}
public TreeSet<String> getWords()
{
//strings are immutable so just copy outer TreeSet
return new TreeSet<String>(wordList);
}
@Override
public boolean equals(Object o)
{
if (o == null) return false;
if (o == this) return true;
if (!(o instanceof Dictionary)) return false;
return wordList.equals(((Dictionary)o).wordList);
}
@Override
public int hashCode()
{
//return Objects.hash(wordList);
return wordList.hashCode();
}
}
/*
//Wrong 1: Does not import BufferedReader, FileReader, File, IOException
//Wrong 2: Cannot make new Set<> interface, must make a new TreeSet
//Wrong 3: package is utils without class specified
//Wrong 4: Set requires add not put
//Wrong 5: .equals on null pointer would throw exception use: == null
//Wrong 6: Forgot to initialize reader to null (better is try-with-resources)
//Wrong 7: constructor on FileReader and File called without new operator
//Wrong 8: Does not explicitly handle file-not-found scenario
//Improvement 1: imports all of util, not just Set, TreeSet
//Improvement 2: could store Set<String> instead of TreeSet - more general
package utils.Dictionary;
import java.util.*;
public class Dictionary{
protected TreeSet<String> wordList = new Set<String>();
public Dictionary(String filePath) {
BufferedReader reader;
try {
reader = new BufferedReader(FileReader(File(filePath)));
for (String line = reader.readLine(); line.equals(null);
line = reader.readLine()) {
wordList.put(line);
}
} catch (IOException e) {
} finally {
if (reader != null) {
reader.close();
}
}
}
}*/

View File

@ -0,0 +1,13 @@
package utils;
public class DisconnectedException extends Exception
{
public DisconnectedException()
{
super("Not connected!");
}
public DisconnectedException(String message)
{
super(message);
}
}

View File

@ -0,0 +1,8 @@
package graph;
public interface Graph<V>
{
boolean hasEdge(V v1, V v2);
void addNode(V v);
void addEdge(V v1, V v2);
}

Binary file not shown.

View File

@ -0,0 +1,81 @@
package graph.undirected;
import graph.Graph;
import utils.DisconnectedException;
//https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/LinkedList.html
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.NoSuchElementException;
public class UndirectedGraph<V> implements Graph<V>
{
private HashMap<V, HashSet<V>> neighborhoodList = new HashMap<>();
private HashMap<V, V> bfsParents = new HashMap<>();
public UndirectedGraph() {}
public boolean hasEdge(V v1, V v2)
{
if (!neighborhoodList.containsKey(v1) ||
!neighborhoodList.containsKey(v2))
throw new NoSuchElementException("Nonexistent node.");
if (neighborhoodList.get(v1).contains(v2) !=
neighborhoodList.get(v2).contains(v1)) {
throw new IllegalArgumentException();
}
return neighborhoodList.get(v1).contains(v2);
}
public void addNode(V v)
{
neighborhoodList.put(v, new HashSet<V>());
}
public void addEdge(V v1, V v2)
{
if (!neighborhoodList.containsKey(v1)) addNode(v1);
if (!neighborhoodList.containsKey(v2)) addNode(v2);
neighborhoodList.get(v1).add(v2);
neighborhoodList.get(v2).add(v1);
}
public LinkedList<V> bfs(V start)
{
LinkedList<V> queue = new LinkedList<>();
HashSet<V> visited = new HashSet<>();
LinkedList<V> order = new LinkedList<>();
bfsParents.clear();
queue.add(start);
visited.add(start);
bfsParents.put(start, null);
while (!queue.isEmpty()) {
V top = queue.remove();
order.add(top);
for (V child : neighborhoodList.get(top)) {
if (!visited.contains(child)) {
queue.add(child); visited.add(child);
bfsParents.put(child, top);
}
}
}
return order;
}
public LinkedList<V> pathTo(V end)
{
if (!hasPathTo(end)) return null;
LinkedList<V> list = new LinkedList<>();
do {
list.addFirst(end);
} while ((end = bfsParents.get(end)) != null);
return list;
}
public int distTo(V end)
{
return hasPathTo(end) ? pathTo(end).size()-1 : Integer.MAX_VALUE;
}
public boolean hasPathTo(V end) //throws DisconnectedException
{
return bfsParents.containsKey(end);
/*if (!bfsParents.containsKey(end)) {
throw new DisconnectedException("Not connected!");
}
return true;*/
}
}

View File

@ -0,0 +1,141 @@
package test;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import graph.undirected.UndirectedGraph;
import java.util.*;
public class UndirectedGraphTest {
@Test
public void testNullGraph(){
UndirectedGraph<String> g1 = null;
assertEquals(g1, null);
}
@Test
public void testEmptyGraph(){
UndirectedGraph<String> g2 = new UndirectedGraph<String>();
assertTrue(g2 != null);
}
@Test
public void testAddNode(){
UndirectedGraph<String> g3 = new UndirectedGraph<String>();
g3.addNode("flirt");
assertTrue(g3 != null);
}
@Test
public void testAddEdge(){
UndirectedGraph<String> g4 = new UndirectedGraph<String>();
g4.addNode("fling");
g4.addNode("cling");
assertFalse(g4.hasEdge("fling", "cling"));
g4.addEdge("fling", "cling");
assertTrue(g4.hasEdge("fling", "cling"));
}
@Test(expected = NoSuchElementException.class)
public void throwsExceptionOnNonexistentNode(){
UndirectedGraph<String> g5 = new UndirectedGraph<String>();
g5.hasEdge("fling", "cling");
}
@Test
public void testHasEdge(){
UndirectedGraph<String> g6 = new UndirectedGraph<String>();
g6.addNode("fling");
g6.addNode("cling");
assertFalse(g6.hasEdge("fling", "cling"));
assertFalse(g6.hasEdge("cling", "fling"));
g6.addEdge("fling", "cling");
assertTrue(g6.hasEdge("fling", "cling"));
assertTrue(g6.hasEdge("cling", "fling"));
}
@Test
public void testBfs(){
UndirectedGraph<String> g7 = new UndirectedGraph<String>();
g7.addNode("stone"); //v1
g7.addNode("stony"); //v2
g7.addNode("store"); //v3
g7.addNode("atone"); //v4
g7.addNode("alone"); //v5
g7.addNode("clone"); //v6
g7.addNode("crone"); //v7
g7.addNode("drone"); //v8
g7.addNode("crane"); //v9
g7.addEdge("stone","store"); //e12
g7.addEdge("stone","stony"); //e13
g7.addEdge("stone","atone"); //e14
g7.addEdge("atone","alone"); //e45
g7.addEdge("alone","clone"); //e56
g7.addEdge("clone","crone"); //e67
g7.addEdge("crone","drone"); //e78
g7.addEdge("crone","crane"); //e79
assertEquals(g7.bfs("atone"),new LinkedList<String>(Arrays.asList("atone","alone","stone","clone","stony","store","crone","crane","drone")));
}
@Test
public void testPathTo(){
UndirectedGraph<String> g8 = new UndirectedGraph<String>();
g8.addNode("stone"); //v1
g8.addNode("stony"); //v2
g8.addNode("store"); //v3
g8.addNode("atone"); //v4
g8.addNode("alone"); //v5
g8.addEdge("stone","store"); //e12
g8.addEdge("stone","stony"); //e13
g8.addEdge("stone","atone"); //e14
g8.bfs("stone");
assertEquals(g8.pathTo("stone"),new LinkedList<String>(Arrays.asList("stone")));
assertEquals(g8.pathTo("atone"),new LinkedList<String>(Arrays.asList("stone","atone")));
assertNotEquals(g8.pathTo("atone"),new LinkedList<String>(Arrays.asList("stony","store")));
assertEquals(g8.pathTo("alone"),null);
}
@Test
public void testHasPathTo(){
UndirectedGraph<String> g9 = new UndirectedGraph<String>();
g9.addNode("stone"); //v1
g9.addNode("stony"); //v2
g9.addNode("store"); //v3
g9.addNode("atone"); //v4
g9.addNode("alone"); //v5
g9.addNode("clone"); //v6
g9.addEdge("stone","store"); //e12
g9.addEdge("stone","stony"); //e13
g9.addEdge("stone","atone"); //e14
g9.bfs("stony");
assertTrue(g9.hasPathTo("stone")==true);
assertTrue(g9.hasPathTo("clone")==false);
}
@Test
public void testDistTo(){
UndirectedGraph<String> g10 = new UndirectedGraph<String>();
g10.addNode("stone"); //v1
g10.addNode("stony"); //v2
g10.addNode("store"); //v3
g10.addNode("atone"); //v4
g10.addNode("alone"); //v5
g10.addEdge("stone","store"); //e12
g10.addEdge("stone","stony"); //e13
g10.addEdge("stone","atone"); //e14
g10.bfs("stone");
assertTrue(g10.distTo("stone")==0);
assertFalse(g10.distTo("stone")==1);
assertTrue(g10.distTo("stony")==1);
assertTrue(g10.distTo("alone")==Integer.MAX_VALUE);
}
}

View File

@ -0,0 +1,73 @@
package ladder;
import utils.Dictionary;
import utils.DisconnectedException;
import graph.undirected.UndirectedGraph;
import java.util.TreeSet;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
public class WordLadder
{
public static void buildLadder(String fileName,
String wordFile, String outputFile) throws IOException, DisconnectedException
{
Dictionary dict = new Dictionary(wordFile);
UndirectedGraph<String> graph = new UndirectedGraph<>();
TreeSet<String> words = dict.getWords();
for (String word : words) {
for (int i = 0; i < word.length(); i++) {
for (char c = 'a'; c <= 'z'; c++) {
if (c == word.charAt(i)) continue;
String str = word.substring(0, i) +
c + word.substring(i+1, word.length());
if (words.contains(str)) {
graph.addEdge(word, str);
}
}
}
}
try (BufferedReader br = new BufferedReader(
new FileReader(new File(fileName)));
PrintWriter pw = new PrintWriter(new File(outputFile))) {
String line = br.readLine();
if (line == null || br.readLine() != null) {
pw.println("class java.lang.RuntimeException");
pw.println("The input file does not consist of one line.");
return;
}
String[] lines = line.split(",");
if (lines.length != 2) {
pw.println("class java.lang.RuntimeException");
pw.println("The number of words in the input file in a line differs from two.");
return;
} else if (lines[0].length() != lines[1].length()) {
pw.println("class java.lang.RuntimeException");
pw.println("Words have different lengths.");
return;
}
String startWord = lines[0], endWord = lines[1];
if (!words.contains(startWord)) {
pw.println("class java.lang.RuntimeException");
pw.println(startWord + " is not in the dictionary.");
return;
} else if (!words.contains(endWord)) {
pw.println("class java.lang.RuntimeException");
pw.println(endWord + " is not in the dictionary.");
return;
}
graph.bfs(startWord);
if (graph.hasPathTo(endWord)) {
pw.println("The length of the path is " +
graph.distTo(endWord) + ".");
pw.println(graph.pathTo(endWord));
} else {
pw.println("DISCONNECTED");
throw new DisconnectedException();
}
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,19 @@
javac -d . Dictionary.java Graph.java DisconnectedException.java UndirectedGraph.java WordLadder.java main\Main.java
java main.Main words_1.txt dictionary.txt output_actual_1.txt
java main.Main words_2.txt dictionary.txt output_actual_2.txt
java main.Main words_3.txt dictionary.txt output_actual_3.txt
java main.Main words_4.txt dictionary.txt output_actual_4.txt
java main.Main words_5.txt dictionary.txt output_actual_5.txt
java main.Main words_6.txt dictionary.txt output_actual_6.txt
java main.Main words_7.txt dictionary.txt output_actual_7.txt
javac -cp junit-4.12.jar;hamcrest-core-1.3.jar -d . UndirectedGraphTest.java UndirectedGraph.java Graph.java DisconnectedException.java
java -cp .;junit-4.12.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore test.UndirectedGraphTest
fc output_actual_1.txt output_expected_1.txt
fc output_actual_2.txt output_expected_2.txt
fc output_actual_3.txt output_expected_3.txt
fc output_actual_4.txt output_expected_4.txt
fc output_actual_5.txt output_expected_5.txt
fc output_actual_6.txt output_expected_6.txt
fc output_actual_7.txt output_expected_7.txt

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,30 @@
package main;
import ladder.WordLadder;
import utils.Dictionary;
import utils.DisconnectedException;
import java.util.List;
import java.io.*;
public class Main {
public static void main(String[] args){
if (args.length != 3) {
System.err.println("Three arguments are required.");
return;
}
String wordsFile = args[0]; // a file containing two words (the bottom and top of the layer)
String dictionaryFile = args[1]; // the file containing the dictionary
String outputFile = args[2]; // the output file
try {
WordLadder wL = new WordLadder();
WordLadder.buildLadder(args[0],args[1],args[2]);
} catch (DisconnectedException e) {
} catch (IOException e) {}
}
}

View File

@ -0,0 +1,2 @@
The length of the path is 10.
[flirt, flint, fling, cling, clink, click, clock, crock, croak, creak, break]

View File

@ -0,0 +1 @@
DISCONNECTED

View File

@ -0,0 +1,2 @@
The length of the path is 18.
[white, whine, chine, chink, clink, clank, flank, flask, flash, slash, slosh, sloth, sooth, south, sough, rough, rouge, rouse, house]

View File

@ -0,0 +1,2 @@
class java.lang.RuntimeException
doggy is not in the dictionary.

View File

@ -0,0 +1,2 @@
class java.lang.RuntimeException
The input file does not consist of one line.

View File

@ -0,0 +1,2 @@
class java.lang.RuntimeException
Words have different lengths.

View File

@ -0,0 +1,2 @@
class java.lang.RuntimeException
The number of words in the input file in a line differs from two.

View File

@ -0,0 +1,2 @@
The length of the path is 10.
[flirt, flint, fling, cling, clink, click, clock, crock, croak, creak, break]

View File

@ -0,0 +1 @@
DISCONNECTED

View File

@ -0,0 +1,2 @@
The length of the path is 18.
[white, whine, chine, chink, clink, clank, flank, flask, flash, slash, slosh, sloth, sooth, south, sough, rough, rouge, rouse, house]

View File

@ -0,0 +1,2 @@
class java.lang.RuntimeException
doggy is not in the dictionary.

View File

@ -0,0 +1,2 @@
class java.lang.RuntimeException
The input file does not consist of one line.

View File

@ -0,0 +1,2 @@
class java.lang.RuntimeException
Words have different lengths.

View File

@ -0,0 +1,2 @@
class java.lang.RuntimeException
The number of words in the input file in a line differs from two.

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1 @@
flirt,break

View File

@ -0,0 +1 @@
allow,brown

View File

@ -0,0 +1 @@
white,house

View File

@ -0,0 +1 @@
doggy,lilac

View File

@ -0,0 +1,2 @@
flirt,break
white,house

View File

@ -0,0 +1 @@
flirt,brea

View File

@ -0,0 +1 @@
break,white,house

View File

@ -0,0 +1,11 @@
package cards;
abstract public class Card
{
protected Suit suit;
protected Card(Suit suit)
{
this.suit = suit;
}
public String toString() { return suit.toString(); }
}

View File

@ -0,0 +1,27 @@
package cards;
//https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/LinkedList.html
import java.util.LinkedList;
public class Deck
{
private LinkedList<Card> cards = new LinkedList<>();
public boolean isEmpty() { return cards.size() == 0; }
public Card draw() {
return cards.remove();
}
public static Deck makeFrenchDeck()
{
Deck deck = new Deck();
for (Suit s : Suit.values()) {
for (int i = 1; i <= 10; i++) {
deck.cards.add(new PipCard(i, s));
}
for (Face f : Face.values()) {
if (f == Face.CAVALIER) continue;
deck.cards.add(new FaceCard(f, s));
}
}
return deck;
}
}

View File

@ -0,0 +1,5 @@
package cards;
public enum Face {
JACK, CAVALIER, QUEEN, KING;
}

View File

@ -0,0 +1,14 @@
package cards;
public class FaceCard extends Card
{
private Face face;
public FaceCard(Face face, Suit suit)
{
super(suit);
this.face = face;
}
public String toString() {
return this.face.toString() + " of " + super.toString();
}
}

Binary file not shown.

View File

@ -0,0 +1,14 @@
import cards.Deck;
import cards.Card;
public class Main
{
public static void main(String[] args)
{
Deck deck = Deck.makeFrenchDeck();
while (!deck.isEmpty()) {
Card c = deck.draw();
System.out.println(c);
}
}
}

View File

@ -0,0 +1,17 @@
package cards;
public class PipCard extends Card
{
private int value;
public PipCard(int value, Suit suit)
{
super(suit);
this.value = value;
}
public String toString()
{
if (value == 1)
return "ACE of " + super.toString();
else return super.toString() + " (" + value + ")";
}
}

Binary file not shown.

View File

@ -0,0 +1,6 @@
package cards;
public enum Suit
{
CLUBS, DIAMONDS, HEARTS, SPADES;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,2 @@
javac -d . Card.java Deck.java Face.java FaceCard.java Main.java PipCard.java Suit.java
java Main