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
2
votes
1answer
111 views

Using AbstractFactory

I am a newcomer to design patterns. I read some articles about the abstract factory pattern, and wrote the following simple example: ...
8
votes
3answers
3k views

FactoryPattern without If-else construct

Someone asked me if I can create factory pattern in java without using If-else construct. So I come with the following. Please provide your inputs if this seems a good example for using factories. <...
3
votes
3answers
2k views

Reading image from jar

I am using this static method in my class IconManager to read an image from jar file. imagepath is a relative path to the image. ...
1
vote
1answer
2k views

Array sort-rank class

I had a task where I was supposed to write a solution to sort entries(Name, score - in an array/list) in order of score and generate ranking based on higher score to lower scores. I tried doing that, ...
1
vote
2answers
1k views

Setting a OnClickListener in a loop

I am trying to set a OnClickListener to a image in a loop. If the params platform is "android" then use market app, instead of default browswer. Is there a better ...
3
votes
1answer
64 views

Code to find the proper index comparing 3 values for max one

I have an algorithm and the idea is to move over an array choosing as index the index of the neighboring cell that has the max value. I.e. if array[i + 1][j + 1] has the largest value among the 3 ...
3
votes
2answers
826 views

Better way to create directories using File

I have this code that generally creates directories within a directory. ...
8
votes
3answers
5k views

Approximate String Matching Interview Question

I was working on the challenge Save Humanity from Interviewstreet for a while then gave up, solved a few other challenges, and have come back to it again. The code below generates the correct ...
3
votes
2answers
4k views

Simple FIFO Job list in Java

For the first time I have to play with Threads in java. Basically, my code is a simple FIFO job queue. The main thread regularly puts jobs in the queue wich need to be executed. The code below is a ...
2
votes
1answer
101 views

Implementing logic in a different way

I need to write a function which will do the following functionalities I would get a string and a array as input parameters The string would be like one of the following for example ...
1
vote
3answers
4k views

Evaluating result for mastermind comparison

I'm implementing a Java version of the game Mastermind. My version uses numbers to represent colors, for example 2315. This function compares a string of numbers ...
1
vote
3answers
998 views

Java Exception Message

I have a few custom exception classes that I created simply for the sake of having my own exception message: ...
1
vote
2answers
176 views
1
vote
1answer
5k views

Force Stop by Error in httpClient.execute(httpPost);

It work's at Smartphone(Android 2.3.3) but Tablet(Android 4.0) => Force Close!! ...
1
vote
1answer
2k views

Implementing the midpoint displacement algorithm in Java

Thanks for taking the time to read this first off. I want to say that I have always been very interested in generating random terrain and since I'm new to any sort of graphical programming, I decided ...
2
votes
1answer
1k views

Count the number of cyclic words in an input

Cyclic Words Problem Statement We can think of a cyclic word as a word written in a circle. To represent a cyclic word, we choose an arbitrary starting position and read the characters in ...
2
votes
1answer
2k views

Sorting parallel ArrayList

I have a model class that contains 3 ArrayList which are in order by parallel of the same size. ...
6
votes
2answers
2k views

Java and stacks: correct implementation?

I created this very simple stack concept's implementation. Could you tell me if it is correct and clean? Do you see any bad coding habits? ...
7
votes
6answers
453 views

Arguments in constructors matching fields

What is your opinion in arguments in constructors matching members as in the following example ...
2
votes
3answers
8k views

Slicing up an image into rows and columns in Java

I have created some code in Java that slices up an image into rows and columns and then saves each image to the file system. This works but there are some improvements I would like to make I would ...
0
votes
1answer
154 views

Redesign a module to be generic

I am having trouble designing a module, can anybody help me? Because it will be hard to maintain this kind of module, I also think that this can test my skill of design pattern usage. Requirement ...
2
votes
3answers
533 views

ACM API based Java exercise

I came across an exercise (in the book "The Art and Science of Java" by Eric Roberts) that requires using only GArc and GLine classes to create a lettering library which draws your initials on the ...
1
vote
2answers
163 views

Review code for optimization and implementation

The below code does Read csv files and process it to do some sort of cacluations.This is more of small picture code , to introduce big picture. The csv file contains information of seeds of crop which ...
2
votes
2answers
453 views

Closing a ResultSet and rethrowing an Exception

Is this an acceptable way to make sure a java.sql.ResultSet is always closed, and also make sure that an Exception caught is propagated to the caller? Please don't hesitate to review other aspects ...
3
votes
1answer
947 views

Returning Binary Search Tree Nodes in Zig Zag manner

I tried to implement an answer for the algorithm question given below. I implemented two different solutions. I couldn't decide which one is better. ( Maybe none ) Note: Node class and ZigZag ...
1
vote
2answers
534 views

Collection-like classes in Java - taking advantage of varargs

I quite often need to implement collection-like classes and - to make using them more comfortable - would like to take advantage of a constructor with variable arguments. The following implementation ...
4
votes
1answer
2k views

JPA connection : is this code is efficient enough

By reading some tutorial i have written some peace of code to do crud operation . I just want to know how is this code is efficient or how can i make better ? Here i am giving code of 3 class 1. ...
2
votes
1answer
3k views

Recursion and iteration how can i optimize this code

I have written some code which will fetch contents from a resource which is actually stored in a tree format. Since it is in a tree format, there will be a parent-child relation and hence recursion. ...
3
votes
2answers
4k views

Random Prime generator

I have this method that will give a random prime for a given range (min inclusive an max not inclusive) all comments are welcome: ...
1
vote
1answer
5k views

Crossword game - designing class structure

I've planning to create crosswords game for Android platform. Basically, crossword is a matrix of TextFields (EditTexts) and <...
3
votes
2answers
174 views

how to write generic extensions for standard collection types in java?

I am a Java beginner and I am looking for an idiomatic way of writing a function that involves generics. I wrote this helper class (below) that pushes items into a sorted generic collection and I ...
2
votes
3answers
6k views

Parse an XML file using objects / methods

So I spent a while this morning creating an xml parser in java which is part of a job interview. I would love to have some people tear it apart so I can learn from any mistakes that I made so I can ...
3
votes
1answer
250 views

File to String Pattern for uploading to a server

I need to upload some images to a server. This is done with a json object. The json object has 2 values, a timestamp and a Base64 encoded string of the image. I am using the jackson library and was ...
3
votes
2answers
478 views

Lowest Common Ancestor

here is my implementation to find the lowest common ancestor in a binary tree. It works but i would like to know if i could have done anything better or if i have missed an specific case. would really ...
2
votes
1answer
1k views

Discrete random variable generator

There is my SSCCE to generate a value of discrete random variable. values is set of value the RV can take and procents is ...
5
votes
3answers
475 views

Readable code with many components (Swing)

Please help me to make more readable and simple code like this. What if I had 20 components? Is it right to organize code like this? ...
3
votes
3answers
2k views

Which one is more readable for mutually exclusive but mandatory command line parameters validation?

I have a small validation code that needs to make sure that exactly one of two parameter is set I consider two options, the "Naive" that has some repetitions and might look "clumsy" but is very ...
4
votes
2answers
145 views

Is using return appropriate here?

I wanted to get the boolean status from a function. Here is my sample code, wherein I used return in try and catch, to get the status of function. ...
4
votes
2answers
1k views

How can I make this primality test algorithm faster?

Is it possible to make this algorithm faster ? Algorithm represents primality test for Wagstaff numbers . ...
1
vote
1answer
341 views

Factory pattern for getting xml data

i am trying to implement factory pattern for getting XML Document from server (using javax.xml.parsers.DocumentBuilder) I have the classes below for now, could you give your opinion ? Does the ...
4
votes
1answer
9k views

Ranking and sorting on Array/List

My apology, I am quite new to posting a question in this forum. I had a task where I was supposed to write a solution to sort entries(Name, score - in an array/list) in order of score and generate ...
0
votes
3answers
2k views

Am I getting the builder pattern right?

Despite being the clever Java coder I am, I noticed I know way too little about various design patterns. The builder pattern caught my eye as something to learn, as I have certainly seen my fair share ...
0
votes
1answer
167 views

What is the better way to retrieve value from this cache implementaion?

I have the following methods in CacheUtil class ...
5
votes
2answers
437 views

Multithreading correctly done?

I rarely write multithreaded code, and am on shaky ground when doing so. I make mistakes. I would like a sanity check of something I am going to introduce in one of my apps. There will be exactly ...
3
votes
3answers
3k views

Is it good practice in Java to pass a StringBuffer to a method to get error messages

I have a class that has a method which takes a user as an argument and performs several validation checks on the user and returns a boolean to indicate if theyare valid. I want to be able to get ...
4
votes
3answers
2k views

Extending java.util.Random.nextInt(int) into nextLong(long)

In java.util.Random the Oracle implementation of nextInt(int) is as follows: ...
2
votes
1answer
683 views

Rewriting nested for loops to give better formatted output

The following is code from a university practical I am doing. It reads in a txt file of twenty clients, whose information is stored in the txt file like this: ...
2
votes
2answers
249 views

How to use inversion of control within a strategy when using the strategy pattern

I am creating a skeleton application to demonstrate use of the Strategy design pattern and inversion of control inside a Strategy. The application is the backend of a simple board game, which also ...
3
votes
2answers
435 views

Optimization of exponentiation

I'm writing some crypto code, and as part of it, we have to implement modular exponentiation. I have it working, both with the right-to-left binary method and with the Montgomery formula. While ...
3
votes
2answers
782 views

Closable BlockingQueue

I am working on legacy code, specifically a sort of BoundedBlockingQueue (mainly used as a pipe between different threads). As it is heavily used in the system and the current implementation features ...