Skip to content

Commit

Permalink
Random class for Java
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBuusJensen committed Dec 15, 2016
1 parent 82b15d0 commit 33ebe6d
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
49 changes: 49 additions & 0 deletions inputParsers/Java/src/Edge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
public class Edge {

private String id;
private Vertex vertex1 = new Vertex();
private Vertex vertex2 = new Vertex();
private int weight;

public Edge() {
}

public Edge(String id, Vertex vertex1, Vertex vertex2, int weight) {
this.id = id;
this.vertex1 = vertex1;
this.vertex2 = vertex2;
this.weight = weight;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public Vertex getVertex1() {
return vertex1;
}

public void setVertex1(Vertex vertex1) {
this.vertex1 = vertex1;
}

public Vertex getVertex2() {
return vertex2;
}

public void setVertex2(Vertex vertex2) {
this.vertex2 = vertex2;
}

public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}
}
29 changes: 29 additions & 0 deletions inputParsers/Java/src/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.ArrayList;
import java.util.List;

public class Graph {

private List<Edge> edges = new ArrayList<>();
private List<Vertex> vertices = new ArrayList<>();

public Graph(List<Edge> edges, List<Vertex> vertices) {
this.edges = edges;
this.vertices = vertices;
}

public List<Edge> getEdges() {
return edges;
}

public void setEdges(List<Edge> edges) {
this.edges = edges;
}

public List<Vertex> getVertices() {
return vertices;
}

public void setVertices(List<Vertex> vertices) {
this.vertices = vertices;
}
}
59 changes: 59 additions & 0 deletions inputParsers/Java/src/Rand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;


public class Rand {

public List<Integer> generateRandomInts(boolean negativeAllowed, int startInclusive, int endInclusive, int limit) {

List<Integer> intList;

Random random = new Random(System.currentTimeMillis());
intList = random.ints()
.map(x -> x % (Math.max(Math.abs(startInclusive), endInclusive)) + 1)
.map(x -> negativeAllowed ? x : Math.abs(x))
.filter(x -> x >= startInclusive && x <= endInclusive)
.boxed()
.limit(limit)
.collect(Collectors.toList());

return intList;
}

public List<Integer> generateInts(boolean negativeAllowed, int startInclusive, int endInclusive, int interval) {
List<Integer> intList;
IntStream intStream;

intStream = IntStream.range(startInclusive, endInclusive + 1);
intList = intStream
.map(x -> negativeAllowed ? x : Math.abs(x))
.filter(x -> x % interval == 0)
.boxed()
.collect(Collectors.toList());

return intList;
}


public void printGeneratedInts(List<Integer> integers) {

for (Integer integer : integers) {
System.out.println(integer);
}
}

public static void main(String[] args) {

Rand rand = new Rand();

List<Integer> randomInts = rand.generateRandomInts(true, 0, 20, 10);
rand.printGeneratedInts(randomInts);

List<Integer> integers = rand.generateInts(false, 0, 20, 5);
rand.printGeneratedInts(integers);

}

}
42 changes: 42 additions & 0 deletions inputParsers/Java/src/Vertex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.ArrayList;
import java.util.List;

public class Vertex {

private String id;
private Vertex prev = new Vertex();
private List<Vertex> adjVertices = new ArrayList<>();

Vertex() {
}

public Vertex(String id, Vertex prev, List<Vertex> adjVertices) {
this.id = id;
this.prev = prev;
this.adjVertices = adjVertices;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public Vertex getPrev() {
return prev;
}

public void setPrev(Vertex prev) {
this.prev = prev;
}

public List<Vertex> getAdjVertices() {
return adjVertices;
}

public void setAdjVertices(List<Vertex> adjVertices) {
this.adjVertices = adjVertices;
}
}

0 comments on commit 33ebe6d

Please sign in to comment.