Questions tagged [java]

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 bytecode and run in a virtual machine (JVM) enabling a "write once, run anywhere" (WORA) methodology.

Filter by
Sorted by
Tagged with
6
votes
2answers
5k views

Check if a value exists or catch an exception

I am parsing a response from server, and in case it contains the fields "chunk_number"(J_ID_CHUNK_NUMBER) and "total_chunk_number"(...
2
votes
1answer
180 views

Strange algorithm results

I came across this thread and I am working on the same problem. My best algorithm passes 6/11 cases while the algorithm in the other thread does indeed pass 8/11 cases. Running time trials with random ...
1
vote
1answer
8k views

Parsing: Entity Classes to DTO

I'm building a GWT application for GAE, using JDO for Datastore access. I use and Entity class for data mapping, so it contains lots of Persistence annotations: ...
1
vote
1answer
3k views

How do you intercept all put/remove/modify calls to a Map using a ForwardingMap?

I tried extending ForwardingMap because I wanted to intercept all calls to put/putAll/remove/clear and also intercept calls on Iterators of KeySet/EntrySet/Values such as remove/Map.Entry.setValue() ...
3
votes
3answers
4k views

Algorithm for computing LCM (least common multiple)

I'm trying to implement this algorithm for computing LCM (from the cut-the-knot website). This is my Java code: ...
3
votes
1answer
322 views

Is the code properly refactored or still some more improvements needs to be done?

I have got code here which I have tried to refactor on my own - but do any of you experts feel that it needs to be refactored more? What are the points should I cover? I would appreciate it if some ...
4
votes
4answers
3k views

Longest substring with no repetitions

This is the code I came up with.It seems correct. I am interested in: 1) Improvements on code 2) How can it be better than this? i.e. this is O(N^2) Could I have ...
5
votes
2answers
5k views

Confirm code is correct - crossover methods in Java

I did this code for somebody but need it to be double checked before I pass it onto them. Code seems fine but I need someone to confirm I have coded the crossover methods correctly. Would be great if ...
1
vote
1answer
315 views

CPU under full load java web app (tomcat)

I've tomcat running on my Macbook pro and a web application. Making a request to a servlet (below i'm showing post method code) and tipping top command in terminal show a java process with 200/300% ...
3
votes
2answers
506 views

My first OOP piece… how have I done?

This is my first OOP coding project and I would appreciate any constructive criticism as to how I could have used the benefits of Java and OOP to better effect. How would you have implemented this ...
1
vote
2answers
208 views

Question on where to put code logic

I am developing a small compiler like tool. In the process, I wrote a Class<?> wrapper, JavaClass: ...
8
votes
1answer
2k views

Parse Java source code

I have a bunch of simple interfaces like this one (pretty enough formed, but not guarantee) ...
5
votes
1answer
769 views

Jon Bentley's bitmap sort implementation

I've implemented Jon Bentley's bitmap sorting algorithm from Column 1 of his Programming Pearls book. This code supports user-defined integer range (works from ...
2
votes
2answers
184 views

How can I improve this code to display contact numbers?

I am making an Address Book like application . I have a contactType enum. public enum ContactType { //just a cut down version MOBILE,PHONE,FAX,EMAIL; } ...
2
votes
1answer
113 views

Externalize to a function

I have the following code block which I'd like to refactor into a method, I just don't see a good way to get the following code into a method which doesn't have multiple responsibilities. Here is the ...
4
votes
1answer
5k views

EPUB reader for Android

How do I improve this code for reading an epub file? The code is as follow: ...
10
votes
4answers
1k views

Vegetable factory

I'm new to Java. I'm trying to build a simple hierarchy of classes. ...
5
votes
1answer
5k views

Singleton class extending a parent class to utilise shared functionality

I have a singleton class which extends from an abstract java class. Two singleton classes extend from ItemImageThreadManager, the reason for this is to use shared ...
6
votes
1answer
188 views

Determine which level of the text that it is currently parsing

So I have this code in my program, and there are multiple variations of it. Just looking at it, I feel like there must be a way to make it more efficient, but I can't think of anything. Just looking ...
2
votes
3answers
10k views

Exception handling continuing the excecution

This code is for continuing the execution after an exception, and this is ugly: ...
7
votes
1answer
2k views

Java Slick StateBasedGame managing resources across states

I was wondering if it's normal/efficient to have this many static collections in my state based game? I started doing it for ease of access but it looks sloppy to me and I was just wondering what ...
1
vote
1answer
335 views

Java API code with Spring

Model: ...
2
votes
1answer
5k views

Getting permutations of a set

I'd like some feedback on my implementation to get all the permutations of a set. Eg: ...
3
votes
1answer
3k views

Writing to an HttpURLConnection in a loop

I have this method which sends binary data to server. The code works fine, but still, it's structure is kinda stupid: output and input connections are opened like thousands times (depends on data size)...
2
votes
2answers
98 views

applyFactors refactoring

I have inherited the Java function below, and it works the way it should, but you have to look at it for a minute to figure out exactly what is going on. Is there a more succinct or elegant way to ...
3
votes
2answers
1k views

Java: Worries about my implementation of MVC in a GUI Application

This is a question I have in regards to my implementation of the MVC design paradigm that I came up with. The MVC type I am using is where everything must go through the controller. No communication ...
5
votes
2answers
345 views
6
votes
1answer
122 views

Refactoring code duplication so the same textArea is not used in 4 different classes

Looking for the best way to do this. Currently in 4 different classes I have: ...
2
votes
4answers
209 views

looking for OO input , is any of this code good OO or am i doing it all wrong?

Some code im working with for a stock system , i have two ways to do this, looking for review on if any of them are correct and if so which one is better OO than the other ...
2
votes
1answer
311 views

How can i clean and organize up my View (MVC) better

So i got the grip on the basic MVC patterns in java using Observer / Observable method. Now in interest of keeping it clean and readable i would like some pointers before i move on regarding how to ...
6
votes
1answer
3k views

Can this simple percentage calculator be further optimized?

public double getValueWithPercentage(double number, double percentage) { return number + ((percentage / 100.0) * number); } Are there any tricks to optimize ...
3
votes
2answers
6k views

Boolean flags encoded as integer implemented with EnumSet

I'm a beginner in Java so I would appreciate a review of following simple class - in fact it's my first real usage of enums. The background: I'm parsing MySQL internal client-server protocol. One of ...
6
votes
5answers
659 views

Simple function in Java

This is a JUnit asserts for this function: ...
1
vote
2answers
5k views

How to delete last element in java.util.Set efficiently?

...
3
votes
2answers
12k views

Queue Implementation

First time I'm using stackexchange. I'm in 8th standard. I tried to implement Queue in Java. I wrote something like this> Right now I'm not able to test it because I don't have JVM in my machine. So I ...
3
votes
3answers
325 views

connect in a background until success

We have some required services down temporarily when our server start. So we need some reconnection logic for them until they are finally up. There is a requirement to have also syncronious way for ...
4
votes
5answers
700 views

Synonymiser refactoring

I am interested in seeing how people would refactor this code to make it more readable, remove local variables, etc. The code checks a string against a number of arrays. The first (0) field of each ...
2
votes
1answer
149 views

Improvement of this method

I'm coding a small web app with no real functionality. It's like a pilot program for something bigger. What I want is construct a program that works as a clock on/off for a restaurant. Actually I'm ...
3
votes
3answers
243 views

Struggling to refactor code to remove duplication

I really need some help removing some duplicated code. I've linked the methods causing me issues. http://pastebin.com/cZJihM4J As you can see each method does similar things but retrieve different ...
3
votes
1answer
1k views

Concurrent event counter/averager utility classes

For a personal project, I'm working on a Java profiler that specifically targets ThreadPoolExecutor and its subclasses and provides statistics about throughput of executed tasks. To support this I've ...
2
votes
1answer
109 views

Separate construction phase from constructed phase

I am sure you can find many weak spots, please let me know about them. My main question is if this is a sensible approach to separate the construction phase from the constructed phase. Basically I ...
2
votes
1answer
430 views

Converting timezone for date using java

I have written this method for converting a date time zone. How do i reduce the execution time of this method further. ...
6
votes
1answer
325 views

Does this code fall into a design pattern?

CustomDialog is the parent abstract class for OkExclamationDialog and ExclamationDialog with ...
6
votes
1answer
393 views

Merging two array of classes

I have this function that should merge two array of classes when id of array1 is equal to id of array2. For simplicity I ...
2
votes
2answers
2k views

How to make a int not be used twice when it randomly generates them? [closed]

I've coded a simple quiz game for Android, and currently Im having troubles with making questions not appear after they've been shown, i.o. I dont want it to ask me the same question twice.. This is ...
2
votes
1answer
7k views

Validating files and returning errors messages/boolean values

I am writing simple file validator for my java ee app and I am stack with my class api. I need specific error descriptions, but also I would like to have boolean values indicating whether file is ...
5
votes
1answer
205 views

Indented if/else approach

I have a flow where if a certain condition is true then I should take another approach. That condition can be triggered by different scenarios. But each condition test must be done only if the last ...
1
vote
2answers
14k views

Bouncing ball - for swing event handling tutorial

This is a sample program that I intend to post as part of a series of beginner level Java tutorials. Please provide any feedback on improvements that would make example more clear or illustrate/...
4
votes
1answer
249 views

Super class knowing the type of its children? Surely there is a better way

I'm working on a small project that does mathematical calculations based on selected data in a JTable. The goal is to make the program fairly adaptable for adding new mathematical calculations in the ...