Java (not to be confused with JavaScript) is a class-based, object-oriented, strongly typed, reflective language and run-time environment (JRE). Java programs are compiled to byte-code and run in a virtual machine (JVM) enabling a "write once, run anywhere" (WORA) methodology.
1
vote
0answers
11 views
Initialize a hashmap in a more compact way without external libraries
Is there a way to make this code more compact? With less lines without using libraries?
I am using Java 7
...
5
votes
2answers
45 views
Knight moves in chess game
I am trying to learn Java by doing some (easy) ACM ICPC problems. The problem consist to check if the knight in a chess game can move from a point A(r1, c1) to B(r2, c2) with one move only. Here the ...
3
votes
2answers
53 views
Java Dice Roller
I've written a basic dice program in Java which takes a number of faces then randomly selects a number within that boundary. Here is the code:
...
0
votes
0answers
4 views
Use same RecyclerView.Adapter with different ViewHolder
I would use the same RecyclerView.Adapter with two or more different fragments. Every fragment use different view items layout so I must use different RecyclerView.ViewHolder for bind the data.
For ...
-1
votes
0answers
16 views
Solving travelling salesman problem using Ant colony optimization in Java
I wrote this program for doing ant colony TSP (travelling salesman problem) like four years ago, and I decided to refactor the code, which led me to this:
Ant.java:
...
1
vote
0answers
7 views
Retrieving data from cursor associated with a list item inside an anonymous listener method declaration
I've run into a problem twice just recently. Though I'm getting around it I can't help but feel that it's a rather unconventional method and that there's a better one.
This app is an exercise in ...
2
votes
2answers
56 views
Inversion count algorithm
I implemented the inversion count algorithm using the merge sort approach for this question.
The question begins with a number t = number of test cases and then for each test case you input size of ...
2
votes
1answer
31 views
Iterate faster through the Android file system and find specific files using java multithreading
I'm trying to extract specific files and visualise them in a list/grid. For that purpose I'm starting a Service to do the background job, and when it finishes it fires an Intent which will be captured ...
2
votes
0answers
37 views
Brute force shortest path in Java
I had to ask myself a couple years ago: before Edsger Dijkstra invented his famous shortest path algorithm, how brute force approach would seem. Below is my version. It's super slow, but I find it ...
5
votes
5answers
620 views
Menu to save and load football team information
I am a newb, and whilst I'm sure my formatting can be improved, I really want to know, how I can re-use my code for this simple program. The objective is simple, football game with a menu, that allows ...
3
votes
1answer
42 views
Java Properties Wrapper
I've decided to make a wrapper class for the java.util.Properties class since, in its current state, it only allows for storing and reading ...
2
votes
1answer
42 views
Destroying a process if it takes too much time
I am executing a shell script from my Java program using Process and I want to kill/destroy that process if my script takes long time.
I came up with this code and ...
4
votes
2answers
745 views
Funny String Java Solution
I'm a little rusty in Java, but I'm using it to get ready for interviews. Here's the problem description from HackerRank:
Problem Statement
Suppose you have a string \$S\$ which has length ...
-3
votes
0answers
16 views
Pong Code Java Applet [on hold]
I wrote a very simple pong applet game. Right now, the ball basically is just bouncing off the sides, but I want the platform to move by using keylisetner, but it ...
1
vote
1answer
39 views
Java “pulse” between threads
I have two threads. One runs a simulation and one runs a UI.
In the UI, it is possible to trigger a "tick" of the simulation. There are a few constraints:
Ticks run on the simulation thread
Each ...
-2
votes
0answers
41 views
Pattern matching in generic lists in Java using naive and Knuth-Morris-Pratt algorithms
Given a sequence of objects \$\langle x_1, x_2, \dots, x_n \rangle\$ and a pattern sequence \$\langle y_1, y_2, \dots, y_m\rangle\$, the pattern matching problem asks, whether there exists an ...
3
votes
0answers
22 views
Swing Component Resizer Class
I've created a class to help resize Swing components while maintaining their aspect ratio within a container.
The class works by creating an instance and adding a container's components to it, then ...
2
votes
3answers
65 views
Optional Consumer for ifNotPresent
As Optional have only ifPresent and not have another function to be run in false case so I did the below class for that, please check it and give me your feedback:
...
4
votes
2answers
67 views
Java Max-Stack implementation to return maximum value in the stack
I have written the below program to write a Java stack implementation. I have added a method which returns the maximum value in the stack, e.g, a pop. The implementation for this is based on the Max ...
1
vote
1answer
61 views
Routing Java objects using conditional consumers
As per a previous question here and answers I got, I used it to get a new implementation. I found that I have to make two implementations match once "to ignore other consumers after matching once" and ...
12
votes
1answer
212 views
Three in a row: Tic, Tac, Toe
The other day, I started thinking of a new personal project to start that I wanted to do in Java. As I started to do it, I realized that I was constantly deleting classes, creating new ones, merging ...
8
votes
5answers
190 views
Limit an integer to certain number of digits and suffix '+'
I am having a problem describing what the following code does. Below is my attempt at its javadoc.
...
3
votes
1answer
100 views
Reuse a base class [Activity] handler in all sub classes for background work
We are using the Thread of a common base class but using its handler in all its sub classes. The code is working fine but is it the right way to go about?
Our base class looks like this:
...
10
votes
1answer
215 views
Find sum of K largest elements in an array
The problem statement is to write code that returns the sum of the K largest elements in a given array. The array is not sorted already.
My solution is based on the idea of quicksort, by finding ...
3
votes
2answers
73 views
ConsumingRouter to consume based on condition
Started as to make a stream splitter to split stream based on condition, but finally found that I did ConsumingRouter, to consume based on condition and while have to provide consumers before using ...
6
votes
1answer
36 views
Exposing configuration elements to program
I have a config file stored in a java properties file:
output_path=/some/path/somewhere
num_threads=42
# ad infinitum
I have an singleton enum which controls the ...
5
votes
1answer
71 views
Java InfiniteStream with Queue
I did InfiniteStream by implementing Stream and Consumer. You can initiate the stream then ...
-2
votes
0answers
22 views
Determining the area of a circle with class areaCircle [closed]
This seems to be throwing several errors. I am sure it is most likely a simple error since this is only the seventh or eighth program I have compiled, but I am unable to see where the error is. I was ...
5
votes
5answers
648 views
Car Savings Calculator
I've created a second 'calculator-like' program, this time using the JOptionPane rather than typing in the console. At the moment the code looks to have a lot of repetition so I'm looking to simplify ...
5
votes
1answer
72 views
Better Toast function
I made this method for a better Toast function. But I have doubts about it.
Because this Toast forced me to make 2 methods and ...
10
votes
2answers
259 views
Unique value finder
For our first assignment in our algorithm class we are suppose to solve several different questions using information from a 2D array. We are also suppose to optimize our code to get more marks on our ...
1
vote
1answer
40 views
Key Value Store for Android on top of Sqlite3
I wrote this simple key-value interface on top of Sqlite3 to use in my Android app. I am using this to persist Java objects as strings converted using Gson.
The public API has 3 methods.
...
10
votes
1answer
84 views
Roman Numerals in Java
I wrote a class that can convert to and fro from Roman Numerals and Decimals. I would appreciate critical points on everything, especially:
Shortening my code.
Choosing a better approach.
Using ...
1
vote
0answers
15 views
Counting Bloom filter library in Java - follow-up
This question is the second iteration of Counting Bloom filter library in Java. I did nothing more but fixing the remove method in order to fix an issue mentioned ...
11
votes
2answers
288 views
Simple object oriented design of student system
I have created a simple system to get hands on experience with OOPS design and some features in Java:
The system is:
A ClassOfStudents contains Students
A ...
4
votes
1answer
39 views
Reading words until an empty line
This following code executes correctly.I was wondering if there are better ways to write this code instead of using while(true) and break.For example using expressions,flags, do-while,etc.
...
4
votes
3answers
115 views
Calculating the distance between two letters
I completed a sample programming challenge and wanted to find out how I could make it more efficient. I'm trying to get better at writing more efficient code, so I would be interested in hearing ideas ...
3
votes
2answers
101 views
Path sum in binary tree
Root to leaf path sum equal to a given number
Given a binary tree and a number, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals the ...
6
votes
4answers
79 views
Java Recursive Depth First Search
I've created a recursive depth-first search implementation in Java as an example for an article I am writing on my website. It needs to be concise in order to fit on the page easily (independent of ...
3
votes
2answers
20 views
SGTIN96 encode for EPC on RFID
This is a class to encode and decode SGTIN96 product identifiers, typically used to write EPC on RFID tags. Refer to EPC(TM) Generation 1 Tag Data Standards Version 1.1 Rev.1.27.
...
2
votes
1answer
43 views
Binary Bayes network classifier in Java - Part I/II - follow-up
TERMINOLOGY
We are given a directed acyclic graph (dag) \$G = (V, A)\$, where \$V\$ is the set of nodes and \$A \subseteq V \times V\$ is the set of directed arcs, and a weight function \$p \colon V ...
4
votes
5answers
129 views
DFS in Binary Tree
I have written this code for DFS in a binary tree and would like improvements on it.
...
5
votes
1answer
69 views
Palindrome using stack
I recently started learning Java, algorithms, and data structures. I am trying to check if a string is palindrome. I would like a review to see where and how I could improve.
...
2
votes
0answers
48 views
CSV to JSON conversion in Java
Since I've looked far and wide for a good example of this to no avail, I have created my own using the JSONArray and JSONObject ...
4
votes
0answers
32 views
Grid with images in RecyclerView
I've created a Grid with a RecyclerView similar to the GoogleIO 2014's generic media player one.
GoogleIO generic music player
Mine
The thing is that the ...
1
vote
0answers
42 views
Binary Bayes network classifier in Java - Part II/II
This is the continuation of Binary Bayes network classifier in Java - Part I/II
TERMINOLOGY
We are given a directed acyclic graph (dag) \$G = (V, A)\$, where \$V\$ is the set of nodes and \$A ...
8
votes
1answer
131 views
Binary Bayes network classifier in Java - Part I/II
I was working on the binary Bayesian network classifier I asked about earlier.
See also Part II/II.
See also the next iteration of Part I.
TERMINOLOGY
We are given a directed acyclic graph (dag) ...
-4
votes
0answers
27 views
how do i inprove upon my recursive binary search in java to prevent any possible user errors [closed]
how do i prevent the random error that i keep running in to.
im really unsure. also im trying to increment numcalls as a counter to count how manny times the recursive call... is called.
...
8
votes
1answer
227 views
Generic domain independent Monte Carlo Tree Search methods library
I've written this small generic library for the purpose of my Bachelor’s thesis. It's fully functional and unit tested and I want to get as many opinions as possible regarding overall code quality ...
4
votes
0answers
53 views
+100
Displaying NFC card data in Binary, Hex, String, etc
I am working on an Android application for reading NFC cards. I came to a point when I needed to design an abstract class to streamline the process of creating ...