Combinations of a matrix (rows and columns) - java

Good afternoon, I'm having problems finding all the combinations of a matrix.
I am testing with a 4x4 matrix but I can not get the approximate 2000 possible combinations of its elements, but I only generate 256 combinations. The idea is to take into account the repetitions to reach this number.
The problem is that my algorithm does not create the combinations of the rows. Explained graphically with a 2x2 matrix:
Correct solution
The correct solution would be the image above, but it only generates 4 combinations.
Solution thrown by the algorithm
My code is:
package ia;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class Nodo implements Iterator<String> {
Nodo anterior = null;
Nodo raiz = null;
int posicion = 0;
List<String> columnas = new LinkedList<>();
int indicesTotales = 0;
public Nodo(List<String> columnas, Nodo anterior, Nodo raiz) {
this.columnas = columnas;
this.anterior = anterior;
this.raiz = raiz;
this.posicion = 0;
this.indicesTotales = columnas.size() - 1;
}
#Override
public boolean hasNext() {
try {
if (this.raiz != null && this.raiz.posicion <= this.raiz.indicesTotales) {
return true;
} else if (this.posicion > this.indicesTotales) {
return false;
} else {
return false;
}
} catch (Exception exception) {
System.out.println(exception);
}
throw new UnsupportedOperationException("Aún no es compatible.");
}
#Override
public String next() {
if (this.posicion >= this.indicesTotales) {
if (this.anterior != null) {
this.posicion = 0;
this.anterior.next();
} else {
++this.posicion;
}
} else {
++this.posicion;
}
return null;
}
public String actual() {
return columnas.get(this.posicion);
}
#Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
2.
package ia;
import java.util.Arrays;
import java.util.LinkedList;
public class Combinaciones {
private Nodo iterador = null;
public Combinaciones(String[][] lista) {
Nodo raiz = null;
Nodo aux = null;
for (String[] columnas : lista) {
aux = new Nodo(new LinkedList<>(Arrays.asList(columnas)), aux, raiz);
if (raiz == null) {
raiz = aux;
}
}
iterador = aux;
}
public int generar() {
int contador = 0;
while (iterador.hasNext()) {
Nodo aux = iterador;
System.out.print(aux.actual());
while (aux.anterior != null) {
System.out.print(" " + aux.anterior.actual());
aux = aux.anterior;
}
iterador.next();
System.out.println();
contador++;
}
return contador;
}
}
3.
package ia;
public class Prueba {
public static void main(String[] args) {
Combinaciones lista = new Combinaciones(
new String[][] { { "1", "2" }, { "3", "4" } } );
System.out.println("\n" + lista.generar());
}
}
I would appreciate if someone could help me solve the error.
Tranks.

Related

Java BFS Webcrawler produces duplicated website links

I am tasked with creating a Java BFS Algorithm without using the built-in LinkedList and Dynamic ArrayList.
I managed to find 2 examples that seem to achieve the result that I am looking for. They can be found below. When I compare my results to the examples I have found my results seem to have duplicated links.
I suspect it has something to do with my contains() method however having tried many different options that I found from Java: Implement String method contains() without built-in method contains() the issue still persist.
Could someone pls help me with this? Thank you so much in advance!!
Examples
https://github.com/theexplorist/WebCrawler
https://www.youtube.com/watch?v=lyVjfz2Tuck&ab_channel=SylvainSaurel (The code in the video can be found below)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WebCrawler {
public static Queue<String> queue = new LinkedList<>();
public static Set<String> marked = new HashSet<>();
public static String regex = "http[s]*://(\\w+\\.)*(\\w+)";
public static void bfsAlgorithm(String root) throws IOException{
queue.add(root);
BufferedReader br = null;
while (!queue.isEmpty()){
String crawledUrl = queue.poll();
System.out.println("\n=== Site crawled : " + crawledUrl + " ===");
if(marked.size() > 100)
return;
boolean ok = false;
URL url = null;
while(!ok){
try{
url = new URL(crawledUrl);
br = new BufferedReader(new InputStreamReader(url.openStream()));
ok = true;
} catch (MalformedURLException e) {
System.out.println("*** Maformed URL : " + crawledUrl);
crawledUrl = queue.poll();
ok = false;
}
}
StringBuilder sb = new StringBuilder();
String tmp = null;
while((tmp = br.readLine()) != null){
sb.append(tmp);
}
tmp = sb.toString();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(tmp);
while (matcher.find()){
String w = matcher.group();
if(!marked.contains(w)){
marked.add(w);
System.out.println("Sited added for crawling : " + w);
queue.add(w);
}
}
}
if(br != null){
br.close();
}
}
public static void showResults(){
System.out.println("\n\nResults : ");
System.out.println("Web sites crawled : " + marked.size() + "\n");
for (String s: marked){
System.out.println("* " + s);
}
}
public static void main(String[] args){
try{
bfsAlgorithm("https://www.tesla.com/");
showResults();
} catch (IOException e) {
}
}
}
Below are the results for the root url: https://en.wikipedia.org/
I edited the code to only show the first 20 links.
Result from https://github.com/theexplorist/WebCrawler
Result from the Youtube Video
Result from my code
If you look at my result you will see that there are duplicated links.
Pls find my code below
Main.java
public static void main(String[] args) {
WebCrawler crawler = new WebCrawler();
String rootUrl = "https://en.wikipedia.org/";
crawler.discoverWeb(rootUrl);
}
}
DA.java (Dynamic Array)
class DA{
int size;
int capacity = 10;
Object[] nameofda;
public DA(){
this.nameofda = new Object[capacity];
}
public DA(int capacity){
this.capacity = capacity;
this.nameofda = new Object[capacity];
}
public void add(Object anything){
if(size >= capacity){
grow();
}
nameofda[size] = anything;
size++;
}
public void insert(int index, Object anything){
if(size >= capacity){
grow();
}
for (int i = size; i > index; i--){
nameofda[i] = nameofda[i - 1];
}
nameofda[index] = anything;
size++;
}
public void delete(Object anything){
for(int i = 0; i < size; i++){
if(nameofda[i] == anything){
for(int j = 0; j < (size - i - 1); j++){
nameofda[i + j] = nameofda[i + j + 1];
}
nameofda[size - 1] = null;
size--;
if(size <=(int)(capacity/3)){
shrink();
}
break;
}
}
}
public boolean contains(Object anything){
for(int i = 0; i < size; i++){
if (nameofda[i] == anything){
return true;
}
}
return false;
}
private void grow(){
int newcap = (int)(capacity *2);
Object[] newnameofda = new Object[newcap];
for(int i = 0; i < size; i++){
newnameofda[i] = nameofda[i];
}
capacity = newcap;
nameofda = newnameofda;
}
private void shrink(){
int newcap = (int)(capacity / 2);
Object[] newnameofda = new Object[newcap];
for(int i = 0; i < size; i++){
newnameofda[i] = nameofda[i];
}
capacity = newcap;
nameofda = newnameofda;
}
public boolean isEmpty(){
return size == 0;
}
public String toString(){
String nameofstring = "";
for(int i = 0; i < size; i++){
nameofstring += nameofda[i] + ", ";
}
if(nameofstring != ""){
nameofstring = "[" + nameofstring.substring(0, nameofstring.length() - 2) + "]";
}
else {
nameofstring = "[]";
}
return nameofstring;
}
Queue.java (LinkedList)
public class Queue<T> {
private Node<T> front;
private Node<T> rear;
private int length;
private static class Node<T> {
private final T data;
private Node<T> next;
public Node(T data) {
this.data = data;
}
}
public void enQueue(T item) {
if (front == null) {
rear = new Node<T>(item);
front = rear;
} else {
rear.next = new Node<T>(item);
rear = rear.next;
}
length++;
}
public T deQueue() {
if (front != null) {
T item = front.data;
front = front.next;
length--;
return item;
}
return null;
}
public int size() {
return length;
}
public boolean isEmpty(){
return length == 0;
}
public void displayQueue() {
Node<T> currentNode = front;
while (currentNode != null) {
System.out.print(currentNode.data+" ");
currentNode = currentNode.next;
}
}
}
WebCrawler.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WebCrawler {
private Queue<String> queue;
DA discoveredWebsitesList = new DA(5);
public WebCrawler() {
this.queue = new Queue<>();
}
public void discoverWeb(String root) {
this.queue.enQueue(root);
this.discoveredWebsitesList.add(root);
while (!queue.isEmpty()) {
String v = this.queue.deQueue();
StringBuilder rawHtml = readUrl(v);
String regexe = "https://(\\w+\\.)*(\\w+)";
Pattern pattern = Pattern.compile(regexe);
Matcher matcher = pattern.matcher(rawHtml);
while(matcher.find()){
String actualUrl = matcher.group();
if(!this.discoveredWebsitesList.contains(actualUrl)){
this.discoveredWebsitesList.add(actualUrl);
System.out.println("website has been found with URL :" + actualUrl);
this.queue.enQueue(actualUrl);
//System.out.println("Size is: " + queue.size());
if(queue.size() == 20){
System.exit(0);
}
}
}
}
}
public StringBuilder readUrl(String v) {
StringBuilder rawHtml = new StringBuilder() ;
URL ur;
try {
ur = new URL(v);
BufferedReader br = new BufferedReader(new InputStreamReader(ur.openStream()));
String inputLine = "";
while((inputLine = br.readLine()) != null){
rawHtml.append(inputLine);
}br.close();
} catch (Exception e) {
e.printStackTrace();
}
return rawHtml;
}
}

Can't access the object within the GraphNode

I have a graph that contains objects of type GraphNodes. These nodes contain an object City that has properties if It's infected or not. I want to loop through all the nodes and check if a city is infected or not. I have a generic method getInfo which returns an object of type E in my case City. But when i try to chain another method or to get property i can't see them as if they are not available. All the classes in the code are from college so i can't add/remove methods. I've tried with foreach but I still can't get the methods.
Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.LinkedList;
class City {
String osnovna_granka;
boolean zarazen;
City(String osnovna_granka, boolean zarazen) {
this.osnovna_granka = osnovna_granka;
this.zarazen = zarazen;
}
#Override
public String toString() {
if (zarazen == true) {
return osnovna_granka + " zarazen";
} else {
return osnovna_granka + " nezarazen";
}
}
}
class Graph {
int num_nodes;
GraphNode<City> adjList[];
#SuppressWarnings("unchecked")
public Graph(int num_nodes) {
this.num_nodes = num_nodes;
adjList = (GraphNode<City>[]) new GraphNode[num_nodes];
}
int adjacent(int x, int y) {
// proveruva dali ima vrska od jazelot so
// indeks x do jazelot so indeks y
return (adjList[x].containsNeighbor(adjList[y])) ? 1 : 0;
}
void addEdge(int x, int y) {
// dodava vrska od jazelot so indeks x do jazelot so indeks y
if (!adjList[x].containsNeighbor(adjList[y])) {
adjList[x].addNeighbor(adjList[y]);
}
}
void deleteEdge(int x, int y) {
adjList[x].removeNeighbor(adjList[y]);
}
#Override
public String toString() {
String ret = new String();
for (int i = 0; i < this.num_nodes; i++) {
ret += i + ": " + adjList[i] + "\n";
}
return ret;
}
}
class GraphNode<E> {
private int index;//index (reden broj) na temeto vo grafot
private E info;
private LinkedList<GraphNode<E>> neighbors;
public GraphNode(int index, E info) {
this.index = index;
this.info = info;
neighbors = new LinkedList<GraphNode<E>>();
}
boolean containsNeighbor(GraphNode<E> o) {
return neighbors.contains(o);
}
void addNeighbor(GraphNode<E> o) {
neighbors.add(o);
}
void removeNeighbor(GraphNode<E> o) {
if (neighbors.contains(o)) {
neighbors.remove(o);
}
}
#Override
public String toString() {
String ret = "INFO:" + info + " SOSEDI:";
for (int i = 0; i < neighbors.size(); i++) {
ret += neighbors.get(i).info + " ";
}
return ret;
}
#Override
public boolean equals(Object obj) {
#SuppressWarnings("unchecked")
GraphNode<E> pom = (GraphNode<E>) obj;
return (pom.info.equals(this.info));
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public E getInfo() {
return info;
}
public void setInfo(E info) {
this.info = info;
}
public LinkedList<GraphNode<E>> getNeighbors() {
return neighbors;
}
public void setNeighbors(LinkedList<GraphNode<E>> neighbors) {
this.neighbors = neighbors;
}
}
public class Main {
public static void main(String[] args) throws Exception {
int i, j, k;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Graph g = new Graph(N);
for (i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
st.nextToken();
String osnovna_granka = st.nextToken();
String str_zarazen = st.nextToken();
if (str_zarazen.equals("zarazen")) {
g.adjList[i] = new GraphNode(i, new City(osnovna_granka, true));
} else {
g.adjList[i] = new GraphNode(i, new City(osnovna_granka, false));
}
}
int M = Integer.parseInt(br.readLine());
for (i = 0; i < M; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
g.addEdge(a, b);
g.addEdge(b, a);
}
br.close();
Stack<GraphNode> stack = new Stack<>();
int counter = 0;
// vasiot kod ovde;
for(GraphNode gn: g.adjList) {
gn.getInfo().// Here the properties of City should show up
}
}
}
GraphNode is a generic type and you have not specified the type, the IDE cannot infer the type so no methods can be suggested. in the for loop you need to specify the type of the GraphNode.
for(GraphNode<City> gn: g.adjList)

JavaFX Tableview and ScrollPane scroll issue

I am using JavaFX from 2 years back. Now i am creating spreadsheet like control using JavaFX.For creating control i am using TableView and ScrollPane with ListView control for Spreadsheet Row header as JavaFX do not provide support for row headers.Everything is working fine except one thing when i scroll table , initially table and scrollpane rows scroll in sync but after some scroll its starting mismatch.I have attached the screen shots of the same.
1)Initial screen without scroll
2)Screen after some scroll
Following are the code snippet for this control which i have pasted in pastebin.
1)Cells.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Cells extends Application {
public void start(Stage stage) {
stage.setScene(new Scene(new SpreadSheet(100, 26)));
stage.setTitle("Cells");
stage.setWidth(400);
stage.setHeight(400);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
2)SpreadSheet.java
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.HBox;
public class SpreadSheet extends HBox {
public SpreadSheet(int height, int width) {
super();
Model model = new Model(height, width);
TableView<ObservableList<Model.Cell>> table = new TableView<>();
table.setEditable(true);
table.setItems(model.getCellsAsObservableList());
for (char w = 'A'; w < 'A'+width; w++) {
TableColumn<ObservableList<Model.Cell>, String> column = new TableColumn<>(w+"");
column.setSortable(false);
column.setMinWidth(50);
column.setCellFactory(TextFieldTableCell.forTableColumn());
final char w0 = w;
column.setCellValueFactory(param -> param.getValue().get(w0-'A').text);
column.setOnEditStart(event -> {
int row = event.getTablePosition().getRow();
int col = event.getTablePosition().getColumn();
Model.Cell c = model.getCells()[row][col];
c.setShowUserData(true);
});
column.setOnEditCommit(event -> {
int row = event.getTablePosition().getRow();
int col = event.getTablePosition().getColumn();
Model.Cell c = model.getCells()[row][col];
System.out.println("Hello");
c.userData.set(event.getNewValue());
c.setShowUserData(false);
});
table.getColumns().add(column);
}
ListView<String> rowHeaders = new ListView<>();
rowHeaders.getItems().add("");
for (int i = 0; i < height; i++) {
rowHeaders.getItems().add(i+"");
}
ScrollPane scrolledRowHeaders = new ScrollPane(rowHeaders);
scrolledRowHeaders.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrolledRowHeaders.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
table.getChildrenUnmodifiable().addListener((ListChangeListener<Node>) c -> {
ScrollBar vbarTable = (ScrollBar) table.lookup(".scroll-bar:vertical");
ScrollBar vbarRowHeaders = (ScrollBar) scrolledRowHeaders.lookup(".scroll-bar:vertical");
if (vbarRowHeaders != null && vbarTable != null)
vbarTable.valueProperty().bindBidirectional(vbarRowHeaders.valueProperty());
});
getChildren().addAll(scrolledRowHeaders, table);
}
}
3)Model.java
import java.util.List;
import javafx.beans.binding.Binding;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
class Model {
private Cell[][] cells;
Model(int height, int width) {
cells = new Cell[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
cells[i][j] = new Cell();
}
}
}
public Cell[][] getCells() {
return cells;
}
public ObservableList<ObservableList<Cell>> getCellsAsObservableList() {
ObservableList<ObservableList<Cell>> cs = FXCollections.observableArrayList();
for (int i = 0; i < cells.length; i++) {
cs.add(FXCollections.observableArrayList());
for (int j = 0; j < cells[i].length; j++) {
cs.get(i).add(cells[i][j]);
}
}
return cs;
}
class Cell {
public final StringProperty userData = new SimpleStringProperty("");
public final StringProperty text = new SimpleStringProperty("");
ObservableValue<Double>[] toArray(List<ObservableValue<Double>> l) {
return l.toArray(new ObservableValue[l.size()]);
}
// Has same problem
// public ObservableValue<Double> value = EasyBind.map(userData, Parser::parse)
// .flatMap(f -> Bindings.createObjectBinding(() -> f.eval(Model.this), toArray(f.getReferences(Model.this))));
// Has same problem
public ObservableValue<Double> value =
Bindings.createObjectBinding(() -> {
System.out.println(System.currentTimeMillis());
Formula f = Parser.parse(userData.get());
ObservableValue<Double>[] fs = toArray(f.getReferences(Model.this));
Binding<Double> d = Bindings.createObjectBinding(() -> {
double v = f.eval(Model.this);
// text.set(String.valueOf(v));
return v;
}, fs);
d.addListener((v, o, n) -> {
// ???
});
return d.getValue();
}, userData);
public void setShowUserData(Boolean b) {
if (b) text.setValue(userData.get());
else text.setValue(String.valueOf(value.getValue()));
}
}
}
4)Parser.java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Parser {
private static Parser instance = new Parser();
private static Tokenizer tokenizer;
static {
tokenizer = new Tokenizer();
tokenizer.add("[a-zA-Z_]\\d+", Token.CELL);
tokenizer.add("[a-zA-Z_]\\w*", Token.IDENT);
tokenizer.add("-?\\d+(\\.\\d*)?", Token.DECIMAL);
tokenizer.add("=", Token.EQUALS);
tokenizer.add(",", Token.COMMA);
tokenizer.add(":", Token.COLON);
tokenizer.add("\\(", Token.OPEN_BRACKET);
tokenizer.add("\\)", Token.CLOSE_BRACKET);
}
public static Formula parse(String formulaString) {
return instance.parseFormula(formulaString);
}
String formulaString;
LinkedList<Token> tokens;
Token lookahead;
private Parser() {}
private Formula parseFormula(String formulaString) {
this.formulaString = formulaString;
try {
tokenizer.tokenize(formulaString.replaceAll("\\s+",""));
} catch (ParseError e) {
System.out.println(e.getMessage());
}
this.tokens = tokenizer.getTokens();
if (tokens.isEmpty()) return Formula.Empty;
lookahead = this.tokens.getFirst();
return formula();
}
private Formula formula() {
switch(lookahead.token) {
case Token.DECIMAL:
String n = lookahead.sequence;
nextToken();
return new Number(Double.parseDouble(n));
case Token.EQUALS:
nextToken();
return expression();
case Token.EPSILON:
return Formula.Empty;
default:
return new Textual(formulaString);
}
}
private Formula expression() {
switch(lookahead.token) {
case Token.CELL:
int c = lookahead.sequence.charAt(0) - 'A';
int r = Integer.parseInt(lookahead.sequence.substring(1));
nextToken();
if (lookahead.token == Token.COLON) { // Range
nextToken();
if (lookahead.token == Token.CELL) {
int c2 = lookahead.sequence.charAt(0) - 'A';
int r2 = Integer.parseInt(lookahead.sequence.substring(1));
nextToken();
return new Range(new Coord(r, c), new Coord(r2, c2));
} else {
throw new ParseError("Incorrect Range: " + lookahead.sequence);
}
} else {
return new Coord(r, c);
}
case Token.DECIMAL:
Double d = Double.parseDouble(lookahead.sequence);
nextToken();
return new Number(d);
case Token.IDENT:
return application();
default:
throw new ParseError("Incorrect Expression: " + lookahead.sequence);
}
}
private Formula application() {
String opName = lookahead.sequence;
nextToken();
if (lookahead.token != Token.OPEN_BRACKET)
throw new ParseError("No opening bracket: " + opName);
nextToken();
List<Formula> args = new ArrayList<Formula>();
while (true) {
if (lookahead.token == Token.EPSILON)
throw new ParseError("No closing bracket");
args.add(expression());
if (lookahead.token == Token.COMMA) nextToken();
if (lookahead.token == Token.CLOSE_BRACKET)
return new Application(opName, args);
}
}
private void nextToken() {
tokens.pop();
if (tokens.isEmpty()) lookahead = new Token(Token.EPSILON, "");
else lookahead = tokens.getFirst();
}
}
class ParseError extends RuntimeException {
ParseError(String message) {
super(message);
}
}
class Token {
public static final int EPSILON = 0;
public static final int EQUALS = 1;
public static final int IDENT = 2;
public static final int DECIMAL = 3;
public static final int OPEN_BRACKET = 4;
public static final int CLOSE_BRACKET = 5;
public static final int COMMA = 6;
public static final int COLON = 7;
public static final int CELL = 8;
public final int token;
public final String sequence;
public Token(int token, String sequence) {
this.token = token;
this.sequence = sequence;
}
}
class Tokenizer {
private LinkedList<TokenInfo> tokenInfos;
private LinkedList<Token> tokens;
public Tokenizer() {
tokenInfos = new LinkedList<TokenInfo>();
tokens = new LinkedList<Token>();
}
public void add(String regex, int token) {
tokenInfos.add(new TokenInfo(Pattern.compile("^("+regex+")"), token));
}
public void tokenize(String s) {
tokens.clear();
while (!s.equals("")) {
boolean match = false;
for (TokenInfo info : tokenInfos) {
Matcher m = info.regex.matcher(s);
if (m.find()) {
match = true;
String tok = m.group().trim();
tokens.add(new Token(info.token, tok));
s = m.replaceFirst("");
break;
}
}
if (!match) throw new ParseError("Unexpected char in input: " + s);
}
}
public LinkedList<Token> getTokens() {
return tokens;
}
private static class TokenInfo {
public final Pattern regex;
public final int token;
public TokenInfo(Pattern regex, int token) {
super();
this.regex = regex;
this.token = token;
}
}
}
5)Formula.java
import javafx.beans.value.ObservableValue;
import javafx.scene.control.Cell;
import java.util.*;
abstract class Formula {
public static final Formula Empty = new Textual("");
public double eval(Model env) { return 0.0; }
public List<ObservableValue<Double>> getReferences(Model env) { return Collections.emptyList(); }
}
class Textual extends Formula {
String value;
public Textual(String value) {
this.value = value;
}
public String toString() {
return value;
}
}
class Number extends Formula {
double value;
public Number(double value) {
this.value = value;
}
public String toString() {
return String.valueOf(value);
}
public double eval(Model env) {
return value;
}
}
class Coord extends Formula {
int row, column;
public Coord(int row, int column) {
this.row = row;
this.column = column;
}
public String toString() {
return ((char)('A'+column))+""+row;
}
public double eval(Model env) {
return env.getCells()[row][column].value.getValue();
}
public List<ObservableValue<Double>> getReferences(Model env) {
List<ObservableValue<Double>> result = new ArrayList<>(1);
result.add(env.getCells()[row][column].value);
return result;
}
}
class Range extends Formula {
Coord coord1, coord2;
public Range(Coord coord1, Coord coord2) {
this.coord1 = coord1; this.coord2 = coord2;
}
public String toString() {
return String.valueOf(coord1)+":"+String.valueOf(coord2);
}
public double eval(Model env) {
throw new RuntimeException("Range cannot be evaluated!");
}
public List<ObservableValue<Double>> getReferences(Model env) {
List<ObservableValue<Double>> result = new ArrayList<>();
for (int r = coord1.row; r <= coord2.row; r++) {
for (int c = coord1.column; c <= coord2.column; c++) {
result.add(env.getCells()[r][c].value);
}
}
return result;
}
}
class Application extends Formula {
String function;
List<Formula> arguments;
public Application(String function, List<Formula> arguments) {
this.function = function;
this.arguments = arguments;
}
public String toString() {
StringBuilder t = new StringBuilder();
t.append(function);
t.append("(");
for (int i = 0; i < arguments.size()-1; i ++) {
t.append(arguments.get(i).toString());
t.append(", ");
}
if (!arguments.isEmpty()) t.append(arguments.get(arguments.size()-1).toString());
t.append(")");
return t.toString();
}
public double eval(Model env) {
try {
List<Double> argvals = evalList(arguments, env);
return opTable.get(function).eval(argvals);
} catch(Exception e) {
return Double.NaN;
}
}
public List<ObservableValue<Double>> getReferences(Model env) {
List<ObservableValue<Double>> result = new ArrayList<>();
for (Formula argument : arguments) {
result.addAll(argument.getReferences(env));
}
return result;
}
private static List<Double> evalList(List<Formula> args, Model env) {
List<Double> result = new ArrayList<>();
for (Formula f : args) {
if (f instanceof Range) {
for (ObservableValue<Double> c : f.getReferences(env)) {
result.add(c.getValue());
}
} else {
result.add(f.eval(env));
}
}
return result;
}
private static Map<String, Op> opTable = new HashMap<>();
static {
opTable.put("add", vals -> vals.get(0) + vals.get(1));
opTable.put("sub", vals -> vals.get(0) - vals.get(1));
opTable.put("div", vals -> vals.get(0) / vals.get(1));
opTable.put("mul", vals -> vals.get(0) * vals.get(1));
opTable.put("mod", vals -> vals.get(0) % vals.get(1));
opTable.put("sum", vals -> {
double accum = 0;
for (Double i : vals) {
accum += i;
}
return accum;
});
opTable.put("prod", vals -> {
double accum = 1;
for (Double i : vals) {
accum *= i;
}
return accum;
});
}
private static interface Op {
public double eval(List<Double> vals);
}
}
Try this
For your ListView get your VBar and bind it with your TableView VBar and your alignment will be intact.
To get your ListView VBar run this code same for TableView VBar
VirtualFlow tvVF = (VirtualFlow) TableView.lookup("#virtual-flow");
for (Node n : tvVF.getChildrenUnmodifiable()) {
if (n.getClass().isAssignableFrom(VirtualScrollBar.class)) {
VirtualScrollBar table_vsb = (VirtualScrollBar) n;
if (tvVF.getWidth() - table_vsb.getWidth() > tvVF.getWidth() / 2) {
//table_vsb is your Vertical bar for the TableView
....//close braces
VirtualFlow lvVF = (VirtualFlow) ListView.lookup("#virtual-flow");
// we do the same for listview
for (Node c : lvVF.getChildrenUnmodifiable()) {
if (c.getClass().isAssignableFrom(VirtualScrollBar.class)) {
VirtualScrollBar list_vsb = (VirtualScrollBar) c;
if (tvVF.getWidth() - vsb.getWidth() > tvVF.getWidth() / 2) {
//list_vsb is your vbar for the listview
now since you have your two VBar's bind the ListView's to the TableView like this
list_vsb.valueProperty().bind(table_vsb.valueProperty());
Please note that you need to call these codes after a full layout, or when Stage.show(); is called - to be safe
Hope its lucid and helps
EDIT
[][]3
it does work for me :)

AVL tree in java

The program that I am writing simulates a road charging system which reads several lines of inputs, each one representing a different command until I reach the EOF (\n).
This commands simulate a road charging system, where the input reads as follows:
PASS 44AB55 I -> where the first word is the command the program receives, the second word is the number plate of the car (44AB55) and the second is the status of the car (Regular or Irregular).
There are three types of commands:
“PASS 00AA00 R” – Increments the number of times that the car has passed in the system and marks its status has Regular or Irreguar. If the car isnt still in the database, it inserts the car as Regular and starts the counter with one passage.
“UNFLAG 00AA00” – Flags the car as Regular if it exists in the database. If the car doesnt exist in the database ignores the command.
“STATUS 00AA00” – Retrieves the status of the car (Regular or Irregular) and the number of passages of the car in the system. If it the car doesnt exist in the database, prints a "NO RECORD" message.
To solve this problem, I am using AVL trees and using the following code:
import java.util.ArrayList;
import java.util.Scanner;
public class TP2_probB {
static No raiz = null;
static double numRotacoes = 0;
static double numAtravessos = 0;
public static class No {
private String matricula;
private int porticos;
private boolean estadoRegular;
private No pai;
private No filhoEsq;
private No filhoDir;
private int balanco;
public No(String matricula, int porticos, boolean estadoRegular) {
this.matricula = matricula;
this.porticos = porticos;
this.estadoRegular = estadoRegular;
this.pai = null;
this.filhoDir = null;
this.filhoEsq = null;
this.balanco = 0;
}
public void setEstadoRegular(boolean estadoRegular) {
this.estadoRegular = estadoRegular;
}
public void setPai(No pai) {
this.pai = pai;
}
public void setFilhoEsq(No filhoEsq) {
this.filhoEsq = filhoEsq;
}
public void setFilhoDir(No filhoDir) {
this.filhoDir = filhoDir;
}
public void atribuiNoEsq(No noEsq) {
this.filhoEsq = noEsq;
}
public void atribuiNoDir(No noDir) {
this.filhoDir = noDir;
}
public void atribuiPai(No noPai) {
this.pai = noPai;
}
public void aumentaPortico() {
porticos++;
}
public String getMatricula() {
return matricula;
}
public boolean isEstadoRegular() {
return estadoRegular;
}
public No getPai() {
return pai;
}
public No getFilhoEsq() {
return filhoEsq;
}
public No getFilhoDir() {
return filhoDir;
}
#Override
public String toString() {
String estado;
if (estadoRegular == true) {
estado = "R";
} else {
estado = "I";
}
return matricula + " " + porticos + " " + estado;
}
}
public static No duplaRotacaoFilhoEsq(No k3)
{
k3.filhoEsq = rotacaoFilhoDir(k3);
return rotacaoFilhoEsq(k3);
}
public static No duplaRotacaoFilhoDir(No k3)
{
k3.filhoDir = rotacaoFilhoEsq(k3);
return rotacaoFilhoDir(k3);
}
public static No rotacaoFilhoDir(No k1) {
No k2 = k1.filhoDir;
k2.pai=k1.pai;
k1.filhoDir = k2.filhoEsq;
if(k1.filhoDir!=null)
{
k1.filhoDir.pai=k1;
}
k2.filhoEsq = k1;
k1.pai=k2;
if(k2.pai!=null)
{
if(k2.pai.filhoDir==k1)
{
k2.pai.filhoDir = k2;
}
else if(k2.pai.filhoEsq==k1)
{
k2.pai.filhoEsq = k2;
}
}
balanco(k2);
balanco(k1);
return k2;
}
public static No rotacaoFilhoEsq(No k1) {
No k2 = k1.filhoEsq;
k2.pai=k1.pai;
k1.filhoEsq = k2.filhoDir;
if(k1.filhoEsq!=null)
{
k1.filhoEsq.pai=k1;
}
k2.filhoDir = k1;
k1.pai=k2;
if(k2.pai!=null)
{
if(k2.pai.filhoDir==k1)
{
k2.pai.filhoDir = k2;
}
else if(k2.pai.filhoEsq==k1)
{
k2.pai.filhoEsq = k2;
}
}
balanco(k2);
balanco(k1);
return k2;
}
public static int pesagem(No aux)
{
if(aux==null)
{
return -1;
}
if(aux.filhoEsq == null && aux.filhoDir == null)
{
return 0;
}
else if ((aux.filhoEsq == null))
{
return (pesagem(aux.filhoDir) + 1);
}
else if ((aux.filhoDir == null))
{
return (pesagem(aux.filhoEsq) + 1);
}
else
return (Math.max(pesagem(aux.filhoEsq), pesagem(aux.filhoDir)) + 1);
}
public static void balanco(No tmp)
{
tmp.balanco = pesagem(tmp.filhoDir)-pesagem(tmp.filhoEsq);
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
String linha;
String[] aux;
ArrayList<String> output = new ArrayList<String>();
int x = 0;
while (true) {
linha = input.nextLine();
if (linha.isEmpty())
{
break;
}
else
{
aux = linha.split(" ");
if (aux[0].compareTo("PASS") == 0) {
No novo;
if (aux[2].compareTo("R") == 0) {
novo = new No(aux[1], 1, true);
} else {
novo = new No(aux[1], 1, false);
}
if (raiz == null) {
raiz = novo;
balanco(raiz);
} else {
procuraNo(novo);
}
} else if (aux[0].compareTo("UNFLAG") == 0) {
if (raiz != null) {
No no = new No(aux[1], 0, false);
mudaEstado(no);
}
} else if (aux[0].compareTo("STATUS") == 0) {
if (raiz == null) {
output.add(aux[1] + " NO RECORD");
} else {
No no = new No(aux[1], 0, false);
output.add(procuraRegisto(no));
}
}
}
}
for (int i = 0; i < output.size(); i++) {
System.out.println(output.get(i));
}
System.out.println("Número de Rotações: "+numRotacoes+"\nNúmero de atravessias: "+numAtravessos);
}
public static void procuraNo(No novo) {
No aux = raiz;
while (true) {
if (aux.getMatricula().compareTo(novo.getMatricula()) == 0) {
aux.aumentaPortico();
aux.setEstadoRegular(novo.isEstadoRegular());
equilibra(aux);
break;
} else if (aux.getMatricula().compareTo(novo.getMatricula()) < 0) {
if (aux.getFilhoDir() == null) {
novo.setPai(aux);
aux.setFilhoDir(novo);
aux=aux.filhoDir;
equilibra(aux);
break;
} else {
aux = aux.getFilhoDir();
numAtravessos++;
}
} else if (aux.getMatricula().compareTo(novo.getMatricula()) > 0) {
if (aux.getFilhoEsq() == null) {
novo.setPai(aux);
aux.setFilhoEsq(novo);
aux=aux.filhoEsq;
equilibra(aux);
break;
} else {
aux = aux.getFilhoEsq();
numAtravessos++;
}
}
}
}
public static void equilibra(No tmp) {
balanco(tmp);
int balanco = tmp.balanco;
System.out.println(balanco);
if(balanco==-2)
{
if(pesagem(tmp.filhoEsq.filhoEsq)>=pesagem(tmp.filhoEsq.filhoDir))
{
tmp = rotacaoFilhoEsq(tmp);
numRotacoes++;
System.out.println("Rodou");
}
else
{
tmp = duplaRotacaoFilhoDir(tmp);
numRotacoes++;
System.out.println("Rodou");
}
}
else if(balanco==2)
{
if(pesagem(tmp.filhoDir.filhoDir)>=pesagem(tmp.filhoDir.filhoEsq))
{
tmp = rotacaoFilhoDir(tmp);
numRotacoes++;
System.out.println("Rodou");
}
else
{
tmp = duplaRotacaoFilhoEsq(tmp);
numRotacoes++;
System.out.println("Rodou");
}
}
if(tmp.pai!=null)
{
equilibra(tmp.pai);
}
else
{
raiz = tmp;
}
}
public static void mudaEstado(No novo) {
No aux = raiz;
while (true) {
if (aux.getMatricula().compareTo(novo.getMatricula()) == 0) {
aux.setEstadoRegular(true);
break;
} else if (aux.getMatricula().compareTo(novo.getMatricula()) < 0) {
if (aux.getFilhoDir() == null) {
break;
} else {
aux = aux.getFilhoDir();
numAtravessos++;
}
} else if (aux.getMatricula().compareTo(novo.getMatricula()) > 0) {
if (aux.getFilhoEsq() == null) {
break;
} else {
aux = aux.getFilhoEsq();
numAtravessos++;
}
}
}
}
public static String procuraRegisto(No novo) {
No aux = raiz;
while (true) {
if (aux.getMatricula().compareTo(novo.getMatricula()) == 0) {
return aux.toString();
} else if (aux.getMatricula().compareTo(novo.getMatricula()) < 0) {
if (aux.getFilhoDir() == null) {
return (novo.getMatricula() + " NO RECORD");
} else {
aux = aux.getFilhoDir();
numAtravessos++;
}
} else if (aux.getMatricula().compareTo(novo.getMatricula()) > 0) {
if (aux.getFilhoEsq() == null) {
return (novo.getMatricula() + " NO RECORD");
} else {
aux = aux.getFilhoEsq();
numAtravessos++;
}
}
}
}
}
The problem is that I am getting a stack overflow error:
Exception in thread "main" java.lang.StackOverflowError
at TP2_probB.pesagem(TP2_probB.java:174)
at TP2_probB.pesagem(TP2_probB.java:177)
at TP2_probB.pesagem(TP2_probB.java:177)
at TP2_probB.pesagem(TP2_probB.java:177)
(...)
at TP2_probB.pesagem(TP2_probB.java:177)
Here is a link with two files with inputs used to test the program:
https://drive.google.com/folderview?id=0B3OUu_zQ9xlGfjZHRlp6QkRkREc3dU82QmpSSWNMRlBuTUJmWTN5Ny1LaDhDN3M2WkVjYVk&usp=sharing

Counting distinct words with Threads

The objective is to count distinct words from a file.
UPDATE: Previous Code was successfully finished. Now I have to do the same but using threads (Oh man, I hate them...) and in addition I want to make it with semaphores for better flow.
Code contains some extra stuff left out from previous attempts, I'm trying to figure out what can be used..
I can read one word at a time but mostly I get a "null" in the container. So until I get anything from the container all the time I can't test the Sorter class and so on...
The new addition to the program is WordContainer class to store one word to pass it from reader to sorter:
package main2;
import java.util.ArrayList;
public class WordContainer
{
private ArrayList<String> words;
public synchronized String take()
{
String nextWord = null;
while (words.isEmpty())
{
try
{
wait();
}
catch (InterruptedException e)
{
}
}
nextWord = words.remove(0);
notify();
return nextWord;
}
public synchronized void put(String word)
{
while (words.size() > 999)
{
try
{
wait();
}
catch (InterruptedException e)
{
}
}
words.add(word);
notify();
}
}
DataSet Class combined with Sorter method resulting in Sorter Class:
package main2;
import java.util.concurrent.Semaphore;
public class Sorter extends Thread
{
private WordContainer wordContainer;
private int top;
private String[] elements;
private boolean stopped;
private Semaphore s;
private Semaphore s2;
public Sorter(WordContainer wordContainer, Semaphore s, Semaphore s2)
{
this.wordContainer = wordContainer;
elements = new String[1];
top = 0;
stopped = false;
this.s = s;
this.s2 = s2;
}
public void run()
{
String nextWord = wordContainer.take();
while (nextWord != null)
{
try
{
s.acquire();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
nextWord = wordContainer.take();
s2.release();
add(nextWord);
}
}
public void startSorting()
{
start();
}
public void stopSorting()
{
stopped = true;
}
public boolean member(String target)
{
if (top > 0)
{
return binarySearch(target, 0, top);
}
else
{
return false;
}
}
private boolean binarySearch(String target, int from, int to)
{
if (from == to - 1)
{
return elements[from].equals(target);
}
int middle = (to - from) / 2 + from;
if (elements[from].equals(target))
{
return true;
}
if (elements[middle].compareTo(target) > 0)
{
// search left
return binarySearch(target, from, middle);
}
else
{
// search right
return binarySearch(target, middle, to);
}
}
public void add(String nextElement)
{
if (top < elements.length)
{
elements[top++] = nextElement;
System.out.println("[" + top + "] " + nextElement);
sort();
}
else
{
String[] newArray = new String[elements.length * 2];
for (int i = 0; i < elements.length; i++)
{
newArray[i] = elements[i];
}
elements = newArray;
add(nextElement);
}
}
private void sort()
{
int index = 0;
while (index < top - 1)
{
if (elements[index].compareTo(elements[index + 1]) < 0)
{
index++;
}
else
{
String temp = elements[index];
elements[index] = elements[index + 1];
elements[index + 1] = temp;
if (index > 0)
{
index--;
}
}
}
}
public int size()
{
return top;
}
public String getSortedWords()
{
String w = "";
for (int i = 0; i < elements.length; i++)
{
w += elements[i] + ", ";
}
return w;
}
public int getNumberOfDistinctWords()
{
return top;
}
}
Reader Class now looks like this:
package main2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.Semaphore;
public class Reader extends Thread
{
private static final int whitespace = 45;
private static final int word = 48;
private static final int finished = -1;
private WordContainer wordContainer;
private Semaphore s;
private Semaphore s2;
private String[] wordsR;
private int state;
private BufferedReader reader;
private int nextFreeIndex;
public Reader(File words, WordContainer wordContainer, Semaphore s,
Semaphore s2)
{
state = whitespace;
try
{
reader = new BufferedReader(new FileReader(words));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
nextFreeIndex = 0;
wordsR = new String[1];
this.wordContainer = wordContainer;
this.s = s;
this.s2 = s;
}
public void startReading()
{
start();
}
public void run()
{
String nextWord = readNext();
while (nextWord != null)
{
nextWord = readNext();
wordContainer.put(nextWord);
s.release();
try
{
s2.acquire();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public String readNext()
{
int next;
StringBuffer nextWord = new StringBuffer();
while (true)
{
try
{
next = reader.read();
}
catch (IOException e)
{
next = -1;
}
char nextChar = (char) next;
switch (state)
{
case whitespace:
if (isWhiteSpace(nextChar))
{
state = whitespace;
}
else if (next == -1)
{
state = finished;
}
else
{
nextWord.append(nextChar);
state = word;
}
break;
case word:
if (isWhiteSpace(nextChar))
{
state = whitespace;
return nextWord.toString();
}
else if (next == -1)
{
state = finished;
return nextWord.toString();
}
else
{
nextWord.append(nextChar);
state = word;
}
break;
case finished:
return null;
}
}
}
private boolean isWhiteSpace(char nextChar)
{
switch (nextChar)
{
case '-':
case '"':
case ':':
case '\'':
case ')':
case '(':
case '!':
case ']':
case '?':
case '.':
case ',':
case ';':
case '[':
case ' ':
case '\t':
case '\n':
case '\r':
return true;
}
return false;
}
public void close()
{
try
{
reader.close();
}
catch (IOException e)
{
}
}
public String getWords()
{
return wordContainer.take();
}
}
Test Class
package test;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Semaphore;
import main2.Reader;
import main2.Sorter;
import main2.WordContainer;
import junit.framework.Assert;
import junit.framework.TestCase;
public class TestDistinctWordsWithThreads extends TestCase
{
public void test() throws IOException, InterruptedException
{
File words = new File("resources" + File.separator + "AV1611Bible.txt");
if (!words.exists())
{
System.out.println("File [" + words.getAbsolutePath()
+ "] does not exist");
Assert.fail();
}
WordContainer container = new WordContainer();
Semaphore s = new Semaphore(0);
Semaphore s2 = new Semaphore(0);
Reader reader = new Reader(words, container, s, s2);
Sorter sorter = new Sorter(container, s, s2);
reader.startReading();
sorter.startSorting();
reader.join();
sorter.join();
System.out.println(reader.getWords());
Assert.assertTrue(sorter.getNumberOfDistinctWords() == 14720);
/*
* String bible = reader.getWords(); System.out.println(bible); String[]
* bible2 = sorter.getSortedWords(); System.out.println(bible2);
* assertTrue(bible2.length < bible.length());
*/
}
}
Why don't you sinply try something like:
public int countWords(File file) {
Scanner sc = new Scanner(file);
Set<String> allWords = new HashSet<String>();
while(sc.hasNext()) {
allWords.add(sc.next());
}
return allWords.size();
}

Categories