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)

0
votes
1answer
17 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 ...
2
votes
1answer
17 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. ...
3
votes
0answers
25 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 ...
1
vote
2answers
39 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 ...
6
votes
2answers
55 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
61 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
42 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
29 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
16 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
69 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
55 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
4 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
67 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
46 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
70 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 ...
5
votes
0answers
46 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
902 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
43 views

Getter and setter class vs interface [on hold]

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
86 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 ...
2
votes
1answer
41 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
71 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
285 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
726 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
36 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
47 views

Parcelable City [on hold]

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
85 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
66 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
53 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 ...
5
votes
3answers
238 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
111 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
50 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
102 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 ...
7
votes
3answers
94 views

Checking brackets nesting in a string

I took a training challenge on Codility that checks for the proper nesting of brackets in a string. The brackets to be checked are {,},(,),[,]. I have written the following Java program which passes ...
6
votes
2answers
72 views

Fast edit of Excel file

I have this code to edit certain cells in my Excel file using Apache POI, but it is really slow. How can I improved the performance? Ideally I would like to edit 20000 rows in less than one minute. ...
4
votes
2answers
80 views

Trie (tree form) in Java

I created a Java Trie as part of a project someone challenged me to do. I'm not sure that this is the most efficient way to go about producing a Trie, I know that this works but I was hoping someone ...
3
votes
2answers
72 views

Permutation of a string eliminating duplicates

This code lists the permutations of the string, and eliminates duplicates if any. I'm looking for code review, best practices, optimizations etc. Also verifying complexity: O(n! * n) as time ...
2
votes
1answer
52 views

Bloom filter implementation

Implementation of Bloom filter with add and contain functionality. I'm looking for code review, best practices, optimizations etc. Also verifying complexity: O(1) public class BloomFilter<E> ...
7
votes
3answers
189 views

Sieve of Erathostenes optimization

I am making a program to find primes between the given range of numbers. Unfortunately, the algorithm I created is too slow. Do you have any ideas of how to optimize the code? import java.io.*; ...
3
votes
1answer
45 views

Is it ok to pull out specific arguments from a Java dynamic proxy invocation?

I have a Java dynamic proxy with an invoke method that looks like this: @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result; try { ...
1
vote
2answers
92 views

Print the elements of an array into a table

I am trying to print the elements of an array into a table, but I'm having trouble formatting the array elements itself. Perhaps there is a better solution than what I'm currently using? import ...
0
votes
1answer
69 views

Reversing a 64 bit Integer [closed]

Example: 00010111 -> 11101000 public long reverse(long x) { long r = 0; int i = 0; while (x > 0) { int bit = 0x0001 & x; r += bit << (63 - i); x ...
1
vote
2answers
49 views

Calculate all possible combinations of given characters

I was asked in my textbook Lectures on Discrete Mathematics for Computer Science to construct a program that would take an alphabet ({a,b,c} or any combination of characters {1,4,s,a}) as well as a ...
5
votes
2answers
115 views

Print routes on the stairway when only 1 or 2 steps are possible

Given a staircase with N steps, you can go up with 1 or 2 steps each time. Output all possible ways you go from bottom to top. I'm looking for code review, best practices, optimizations etc. ...