Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

72
votes
5answers
6k views

Guessing a number, but comments concerning

I always get marked down for my comments and I just wanted to see if these comments are acceptable or what I should include/where I should include them. ...
49
votes
3answers
3k views

Analyzing Minesweeper Probabilities

Calculating probabilities in Minesweeper might sound like an easy task, but I've seen so many probability calculators that's either incorrect, horribly slow, or with ugly code (or all of them) so I ...
32
votes
2answers
6k views

Weekend Challenge - Poker Hand Evaluation

Weekend Challenge #2 - Poker Hand Evaluation Very early I decided to support the usage of wild jokers, even though I knew that this was going to lead to trouble more work. I also wanted to support ...
100
votes
9answers
25k views

'100' is a magic number

Magic numbers are bad... I totally agree. But there's one magic number I find hard to fix: '100' is a magic number. Consider this code: ...
8
votes
4answers
4k views

Sound manager for Android

I created a singleton class for managing sound effects on Android. This class will only be instanced and loaded once at the beginning, and each activity will use the loaded songs. I don't know either ...
15
votes
2answers
2k views

Recursive and flexible approach to Tic-Tac-Toe

Description This is my code for the Weekend Challenge Reboot - Tic Tac Toe Ultimate. The game can be played here: http://www.zomis.net/ttt (along with some other variations that are also use the ...
6
votes
4answers
13k views

Random walk on a 2D grid

The program assignment: A drunkard in a grid of streets randomly picks one of four directions and stumbles to the next intersection, then again randomly picks one of four directions, and so on. ...
10
votes
3answers
5k views

Project Euler Problem 12 - triangle number with 500 divisors

I've just done Problem 12 of Project Euler: What is the value of the first triangle number to have over five hundred divisors? The \$N\$'th triangle number is the sum of all natural numbers ...
11
votes
1answer
147 views

YAuB - Micro Benchmark Follow-on

Following some great advice here from Simon, I realized that I had over-engineered things, and that the Task builder methods were a horrible Java8 abstraxction. In Simon's words: "From a usability ...
5
votes
3answers
454 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? ...
48
votes
8answers
33k views

Efficiently checking if a number is divisible by 3

I'm looking for code review, best practices and optimizations. ...
40
votes
2answers
813 views

Tool for creating CodeReview questions

To download the tool and for usage instructions, see the follow up to this question Description I have realized that there's a whole bunch of code I want to have reviewed, but copying file by file, ...
12
votes
4answers
26k views

Printing star greater symbol in Java

I need to print this in Java, but I feel that my code is too big: * ** *** **** *** ** * ...
6
votes
2answers
282 views

Prime generator for SPOJ

I have written code to solve the prime generator problem. I am given an input which contains in the first line the number of test cases, and for each test case, I am given a range and I have to print ...
5
votes
1answer
1k views

Swap items of a linked list in pairs

Here is the source of the question: GitHub Given a singly linked list, swap the list items in pairs (reconnect the pointers, not simply swap the values). For example: Before: A->B->C->D ...
22
votes
1answer
311 views

Follow-up to tool for posting CodeReview questions

Description This is a follow-up question to Tool for creating CodeReview questions. Things that has changed include: Removed replacing four spaces with one tab, all tabs and all spaces in the code ...
22
votes
4answers
2k views

Determining if an answer can be generated

How can I improve my code below for the following question? While it works, I am not satisfied with how it looks; I was hoping I can somehow keep it recursive, and have the ans filled up inside the ...
20
votes
6answers
10k views

Optimize Conway's Game of Life

I have coded up an implementation of Conway's Game of Life and I have a performance bottleneck in it which I wish to be optimized. The main logic is in the Universe ...
12
votes
2answers
2k views

Determining winning hand in poker

After having reviewed this question, I recalled that I haven't solved Project Euler's problem 54. So I did it, however, I fount out that implementing the exact rules is rather tricky. So I ...
14
votes
3answers
2k views

N-Queens - Brute force - bit by bit

A discussion in The 2nd Monitor made me realize I had never 'solved' the N-Queens problem. Additionally, as I read up on it, I realized that the 64-squares in a chess board would work well if ...
5
votes
3answers
71k 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 ...
30
votes
3answers
832 views

Advanced and Detailed Minesweeper Probabilities

In an earlier question, I showed my code for calculating the mine probability for each and every field in a Minesweeper board. But it doesn't stop there. In that very same Minesweeper Analyze project, ...
20
votes
4answers
2k views

Finding the sub-array with the maximum sum - my approach

This is the code I ended up with that implements the approach I described in a recent answer to another question about the same problem The basic idea here is to not loop through more things than ...
13
votes
3answers
4k views

Match Simple Sentence or Partial Sentence

Description Match a Simple Sentence or a partial sentence Suitable for matching People Names (to some extent) Product Titles (to some extent) Correct use of apostrophe (to some extent) Input/Result ...
13
votes
1answer
920 views

Cheapest hotel finder

Please help me make this code more object oriented. I also want to use a few more classes and separate the input processing code to another class. Question: A hotel chain operating in Goa wishes ...
12
votes
1answer
290 views

Yet Another Java CLI Calculator

In the spirit of April 2015 Community Challenge, I have created two Java-based calculators with the following codebase layout: An interface Calculeightor for ...
10
votes
2answers
538 views

Yet Another Java GUI Calculator

In the spirit of April 2015 Community Challenge, I have created two Java-based calculators with the following codebase layout: An interface Calculeightor for ...
7
votes
3answers
10k views

Optimizing Manhattan-distance method for N-by-N puzzles

By N-by-N puzzles I mean f.e. a 3x3 sliding puzzle with one blank space (so a 8-puzzle) or a 4x4 sliding puzzle (so a 15-puzzle). When I try to solve certain puzzles some take no time but others take ...
5
votes
2answers
7k views

Printing longest sequence of zeroes

I am doing a coding exercise in codility and I came across this question: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ...
2
votes
3answers
347 views

LeetCode 49: Group Anagrams

I'm exceeding the time limit for a 10,000 word test case provided on LeetCode: Given an array of strings, group anagrams together. For example, given: ...
13
votes
7answers
2k views

Quadratic expression calculator

I'm hoping to get some feedback on how to improve and optimise a program I wrote which takes three values as input (via the terminal) and uses them in the quadratic formula. I'm aware this is mostly ...
12
votes
3answers
2k views

The Beauty and the Strings

This originally appeared in Facebook's hacker cup 2013. Solving it was fun, though I find myself looping through similar data quite often. Challenge: Print the maximum beauty of strings. ...
17
votes
2answers
4k views

It's Friday! - Zeller's Congruence Revisited

A couple of date-related questions have come up recently, and on both occasions I have been taken back to my 'early' days where one of my first forays in to programming involved implementing Zeller's ...
10
votes
2answers
448 views

A Specific Combination

This code is going to be included in my Minesweeper Probabilities project. (so far it only exists on the develop-branch) The purpose is to return a specific combination, let's say for example that ...
5
votes
1answer
136 views

Android game inspired by Space Invaders and Moon Patrol

I'm creating a mini game for Android inspired by space invaders and Moon Patrol. I have completed the important part except for collision detection. Before I do collision detection I want to clean the ...
3
votes
2answers
1k views

Premium palindromic primes

Challenge: Write a program which determines the largest prime palindrome less than 1000. The answer is 929, and my program correctly finds and prints this, but actually ended up being more complex ...
3
votes
1answer
141 views

Swap items of a linked list in pairs - revision 2

The first revision is available here and the code is available on GitHub. I modified the method reversePairs and added mSize to ...
2
votes
1answer
72 views

Swap items of a linked list in pairs - revision 3

Here is the source of the question. Revision 1. Revision 2. When I started the second round of code review, I wanted to make sure that I had included all the possible input types. However, I ...
37
votes
5answers
21k views

Designing a coffee machine

I was recently rejected from what looked like a really promising string of interviews. I did very well in a questionnaire style review, and then they handed me this assignment (more or less): ...
41
votes
4answers
162k views

Simple Example of an Iterable and an Iterator in Java

I was a TA for a first-year Java programming course this year. As a very simple example of iterables/iterators, I wrote the following code for the students. I am curious if any styling or other ...
37
votes
10answers
6k views

Which FizzBuzz is better, and why?

I was in an interview and the guy asked me to do the typical FizzBuzz question of printing numbers 1-100, but for each factor of 3 print Fizz, each factor of 5 print Buzz, and each factor of both ...
45
votes
5answers
224k views

Removing elements on a List while iterating through it

I needed a way to remove elements on a List while iterating through it. Supposedly something like this (illegal code): ...
19
votes
3answers
13k views

TicTacToe logic in Java

I'm currently studying Java by myself. The book I'm using (while very good) lacks feedback (obviously). While trying to write this program, I found myself solving most of the problems I've ...
34
votes
6answers
34k views

Refactoring large switch statement [closed]

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 ...
46
votes
4answers
8k views

Ternary operation in Java - isn't this abuse?

I think the below code is difficult to understand. I also feel it abuses Java's ternary operator. ...
40
votes
5answers
3k views

Regularity in the “Rusty Towel of Mutual understanding”

I have the following Java class: ...
37
votes
8answers
7k views

The FizzBuzz challenge in Java 8 written in a short, readable and interesting way

I decided to take on the FizzBuzz challenge with as twist that I would use Java 8 concepts to make it a bit modular, yet still let it be a short, readable and understandable program. This in contrary ...
28
votes
7answers
62k views

Check for balanced parentheses

Given an expression string exp, write a program to examine whether the pairs and the orders of "{","}","(",")","[","]" are correct in exp. For example,...
14
votes
3answers
1k views

Simple micro-benchmarking library

I'm working on a simple micro-benchmarking library in Java. This library makes it easy to benchmark multiple reference implementations. You provide the inputs and trigger the calls, the library takes ...
29
votes
7answers
7k views

100 gunmen in circle kill next person

The question is here. 100 people are standing in a circle with gun in their hands. 1 kills 2, 3 kills 4, 5 kills 6 and so on till we are left with only one person. Who will be the last person alive....