Java is a popular programming language and runtime environment which allows programs to run unchanged on most platforms. Not to be confused with JavaScript.

learn more… | top users | synonyms (1)

5
votes
1answer
32 views

AI bot Java dungeon game

I have written a script that allows a user to walk around a dungeon pick up gold and once they have picked it all up they can exit the game through an exit. What I am doing now is writing a bot to do ...
1
vote
0answers
10 views

Class naming conventions

I have the following scenario: The (web-) application is built with Java/Spring. I have several REST-endpoints which deliver data prepared to be consumed by a frontend by datatables.net. Each of this ...
4
votes
4answers
82 views

Save space occupied by 2D array

My goal is to save space occupied by 2D array (sea) that has 3 different values. Ocean sea; //Ocean class has member 'private int[][] oceanMatrix;' public final static int EMPTY = 1; public final ...
4
votes
3answers
101 views

Compute bounding rectangle given 2 points quickly and efficiently

Is there a faster way to compute the bounding area between 2 points? // Minified Version public static Rectangle computeBounds(Point2D.Double p1, Point2D.Double p2) { double dx = p2.x - p1.x, dy ...
3
votes
0answers
30 views

Equation evolution

I'm coding something to simulate evolution, but with an equation that is tested to find the digitsum of an algorithm. Basically what it does is that it creates GENERATION Organisms, which have DNABITS ...
1
vote
1answer
30 views

Find last zero in infinite stream of 0's followed by 1's

Given an infinite stream with the property, such that after all 0's there are only all 1's, find the index of the last 0. I'm looking for code review, best practices, optimizations etc. Verifying ...
7
votes
1answer
58 views

Project Euler “Even Fibonacci numbers” in Java 8

I'm looking for general advice on my code on the following problem: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 ...
7
votes
4answers
385 views

Maths test program

I have written this simple program that is a basically a maths test. I would like to know is there any areas I can improve on and if I have any bad habits that would be useful to break now. I realise ...
3
votes
2answers
37 views

Java Ghost4J PDF to Image Conversion

The primary goal of this code is to convert a PDF file to images. I also create a directory under C:/Media based on a combination of the PDF name and current date and insert the uploaded PDF to the ...
1
vote
3answers
79 views

Lowest common ancestor in recursive tree traversal

LCA = Lowest Common Ancestor The following code finds the lowest common ancestor in tree of nodes, where a node can have two parents, left and right. The tree looks like this: J-K / \ ...
0
votes
6answers
242 views

Which Java string value is preferred to return: blank or null?

I came across this snippet and am wondering if I should return blank in this case. Which is more practical to return: blank or null? public String getInfo() { String result = ""; String uri ...
2
votes
2answers
55 views

Path from source to destination, with increasing values

Given a source and destination, in an integer matrix, find path, such that the next position in the path has a greater than or equal to value than the previous position. In other words ...
5
votes
2answers
62 views

How to optimize population projections for speed?

I am currently working on a problem of an online judge (not a contest). The program must calculate how many years will require for the population A (which is always less than B) to be higher than B. ...
5
votes
0answers
51 views

Model simulation using Java annotations

A couple of days ago I posted my code that models systems made up of moving parts. I got several great tips and ideas and here is my latest version. My actions are now methods annotated with @Action ...
2
votes
2answers
54 views

Produce a nearly sorted (or K sorted) array

Given an array of n elements, where each element is at most k away from its target position, devise an algorithm that sorts in O(n log k) time. For example, let us consider k is 2, an element at ...
7
votes
2answers
82 views

Math expression solver

Recently I've stumbled upon an interesting challenge for me. You should develop C application, that solves math expressions. Operations such as (+,-,*,/) should be supported, as well as (cos, sin, ...
4
votes
3answers
74 views

Reducing repetition and utlizing EDT

I am working on the largest Java program I have undertaken to date and feel uncertain in my current code practices. I think that I state and repeat things that don't need to be which can become a ...
3
votes
2answers
49 views

Returning all the LIS of given array of int

I have this assignment where I'm supposed to code an algorithm for finding the quantity of Longest Increasing Subsequences from an array of (different) integers and print them all. I developed one ...
-1
votes
0answers
32 views

Java: best practice for if statements [on hold]

This is probably subjective but I'm wondering what the best/most used practices are when it comes to if/else if/else statements in Java regarding the following: often times I'll see something like ...
0
votes
0answers
17 views

fish program that needs work done on goneFishing method [on hold]

Hi I was wondering how you call a method with parameters inside another method. For example I am trying to call my likes() method inside my goFishing method but its not working heres my code: import ...
6
votes
1answer
72 views

How to fill an ArrayList of ArrayLists with a Left Join?

I have a class Employee that contains an ArrayList of Projects. I'm storing the Employees in one table, and the Projects in another. I'm trying to find the best way to create an ArrayList of ...
-2
votes
0answers
100 views

setOnCheckedChangeListener [on hold]

Help me complete this code: by Setting up and implementing an OnCheckedChangeListener: final CheckBox statusCheckBox = (CheckBox) itemLayout.findViewById(R.id.statusCheckBox); //CheckBox stattusView ...
18
votes
6answers
3k views

Refactoring large switch statement

Lately I have been trying to extract till I drop. Well not necessarily till I drop, but I've been trying to be more strict and look at some metrics of my code. I have now come along an old class of ...
0
votes
0answers
5 views

Use global variables or methods in an API's frontend [migrated]

I am currently designing a graphics library in Java and now it's come to making the frontend I am curious why I have never seen libraries using global variables for their settings/properties - in-fact ...
4
votes
2answers
72 views

Long Thread & EDT

I’m trying to make a simple gui app that starts a “long” process that can be started and stopped. The process generates random numbers and displays them in a JList. As numbers are being displayed ...
5
votes
1answer
55 views

Is this merge sort code good enough?

public class MergeSort { public static void sort(int[] array, int lo, int hi) { if (hi <= lo) { return; } int mid = lo + (hi - lo) / 2; sort(array, ...
2
votes
1answer
47 views

Interval search tree

An interval is a data structure that represents a range (start & end, from & to, or min & max, etc.). An Interval Tree stores these intervals in a sorted tree structure that makes ...
7
votes
1answer
72 views

Card layout in Java

I have a JPanel (mainPanel) with CardLayout which contains 4 JPanels (childPanel). Each childPanel contains: a JPopupMenu with 4 JMenuItems to switch between the childPanels ...
1
vote
1answer
40 views

Class Design for Unidirectional one to many relationship [on hold]

I joined a new project in support/enhancement role. I had a walk-through of code and found the classes (and relationships) are designed as follows: Suppose class Department has many Employees and in ...
-1
votes
0answers
23 views

Continuous Fractions - [on hold]

My understanding of continuous fractions was that the will always give a representation of a decimal in fraction form. I thought that continuous fraction would always return value less than or equal ...
8
votes
1answer
65 views

Using Bukkit conversations for a coding console: Proper structure?

I have a class that serves to carry data between my JavaScript evaluation class and the player, via the Bukkit Conversations API, available in full on GitHub (you will need Bukkit and Rhino as ...
13
votes
2answers
917 views

Java %= and general feedback

I'm doing a program for a course in school (intro software development) and I'm looking for some general feedback as well as any ways this bit of code could be cleaner. I'm also wondering if it is bad ...
-4
votes
0answers
45 views

Getter and setter class vs interface [closed]

This is a design and implementation oriented question. As I am learning more techniques everyday I am getting confused with so many choices. The typical model class: class User implements ...
5
votes
3answers
89 views

Feedback on thread safety of the classes

I have following classes. Are they properly guarded for thread safety? Do you see any issues with any of the classes? @ThreadSafe public class RetirementAccount { public static final int TYPE1 ...
3
votes
1answer
45 views

Join/ connect all levels of Binary tree without any aux storage

This program connects all nodes of the binary tree at the same level. A node of a tree contains a left, right and a sibling pointer which would connect it to the next node at the same level. This ...
1
vote
0answers
25 views

Iterator for binary tree - pre, in, and post order iterators

Implemented iterator for a binary tree and "pre" "in" and "post" order flavors. I'm looking for code review, best practices, optimizations etc. public class IterateBinaryTree<E> { private ...
5
votes
2answers
78 views

Is my N-drome (variation of palindrome) checking program efficient?

An N-Drome is a string that is the same forwards and backwards when split into n-character units. For example, a string would be a 1-drome if and only if it were a standard palindrome. An example ...
0
votes
0answers
17 views

Removing comments from code with a program [migrated]

I'm working on a problem that requires me to remove comments from Java code and print out the results. I tried to use regex and came up with this. private static void stripComments(Scanner input) { ...
9
votes
3answers
286 views

Is decimal, hexadecimal, octadecimal, binary converter efficient?

I have made a method which takes two parameters: the value to convert and the base. Based upon those two parameters my method is adjusted (with several helper methods) to change the desired decimal ...
11
votes
3answers
732 views

Find longest sequence horizontally, vertically or diagonally in Connect Four game

I'm new to programming and also to Java and working on problems in Intro to Java by Robert Sedgewick. Here is my question: Connect Four: Given an N-by-N grid with each cell either occupied by ...
5
votes
1answer
38 views

Monadic Immutable Linked List in the Least Functional Language Evar

I've written a List monad as an example for a related question. This is a rather frustrating experience as I wanted to use Java, which (as of Java 7) still lacks lambda expressions and lacks ...
0
votes
1answer
48 views

Parcelable City [closed]

I am new to the Parcelable interface that I have implemented in my classes. At first they were Serializable, but now I have changed them to Parcelable. Have I done this the right way, and am I doing ...
6
votes
2answers
86 views

A monad in Java (FriendSpace and People)

After asking a similar question on Stack Overflow, I'm picking up on the answer there to improve my monad. I'm not trying to solve the general case, just come up with one to see how it works. If I'm ...
4
votes
1answer
69 views

How can I reduce the number of nested if statements in this code?

The application I'm building can accept two types of updates: Application Update and Parameter Updates. If there is an application update, then parameter updates can be ignored. This is the code that ...
7
votes
1answer
90 views

Drawing a triangle and some concentric circles

For class, I had to use Java to draw a triangle and some concentric circles. Here's my code (questions follow): import javax.swing.*; import java.awt.*; import java.awt.geom.*; public class ...
4
votes
2answers
54 views

Numeric expression parser - calculator

In particular, if someone could describe a better way to go from the tokenized list to the Expression tree, it would be super helpful. I would like to get rid of the casting in the parser but am not ...
6
votes
3answers
243 views

Code reduction possible with modulo operator?

I'm trying to model a puzzle in order to resolve it with the Choco solver. One of the constraint I'm coding is cyclical (it's triplet which follow themselves) like the following example: ...
6
votes
3answers
114 views

Factory for classes unknown at compile time

I have a class called Machine. It contains a list of parts and a list of actions. The list of parts will contain instances of the Part class subclasses and the list of actions will contain instances ...
8
votes
2answers
51 views

Count the one islands in the matrix

What is an island? A group of connected 1s forms an island. For example, the below matrix contains 5 islands: {{1, 1, 0, 0, 0}, {0, 1, 0, 0, 1}, {1, 0, 0, 1, 1}, {0, 0, 0, 0, 0}, {1, 0, 1, 0, 1}} ...
8
votes
1answer
105 views

Ready? Set. Fight!

Description This code is for 'fighting' two objects against each other. I am normally using it to make my AIs fight each other in one game or another. In case you are wondering: Yes, I am using this ...