Hot answers tagged

15 votes
Accepted

Detecting Tiles in Java

The entirety of the if/else statements are redundent if we consider that if we divide the current location by the tile_size we get the correct tile number ...
  • 11.5k
14 votes

Simple Java MIDI player followup

Well, your post is over a year old, but hopefully this can still be of some use to you! Javadoc No need to say the method name again - the method name is automatically part of the Javadoc. What are ...
  • 614
14 votes
Accepted

Gravity model for a simulator

Math.pow(x, 2) -> x*x A pet peeve of mine is people calling Math.pow() just to square a number. To me it seems faster and simpler to just multiply the value by ...
  • 28.4k
14 votes
Accepted

Printing the month name for the month number

As you have probably noticed, there is a lot of repetition which we can take advantage of. We can declare an array containing all the string representations of the months like so. ...
  • 401
12 votes
Accepted

Ping-Pong game in Java

Model vs UI Your classes have basic separation of concerns. But as you can see by yourself some model elements like "Ball" or "Box" or "Paddle" or even "Game" ...
  • 3,112
11 votes
Accepted

JPaint (Java painting app)

The code is damn long, so just a few notes: Use initializer expressions like private JRadioButton setRed = new JRadioButton("Red", true); Use static factory ...
  • 13.5k
11 votes
Accepted

Beginner Minesweeper game

Some simple pointers to get things started... Magic numbers You have hard-coded your side, limit (how is it used, and why does ...
  • 19.2k
11 votes
Accepted

Velocity, Distance and Time Calculator

...
10 votes

Add JPanel to JFrame

I would recommend: You're using a GridLayout(2, 3) -- good. I'd make it actually a GridLayout(2, 0, GAP, GAP) where GAP is a ...
10 votes
Accepted

Workout tracker

Okay, lets begin at the very beginning (since that's a very good place to start. Allegedly). Swing is the Java GUI framework (now EOL, so probably not worth learning really). As with most GUI ...
10 votes

Simple Java calculator using Swing

You don't need so many anonymous handlers, would be better to have one just track the source of the event and follow the logic you want. You haven't yet answered whether printing to the console is a ...
  • 9,809
10 votes
Accepted

Ballistics and arithmetic calculator

Just a few tips: Let your IDE format the code, it's zero work and the result is consistent. You should always separate the UI and the computation. Here, it may be a bit pointless as the computation ...
  • 13.5k
10 votes

"Lights off" puzzle

I wonder how this generalizes to e.g., a 30x30 game. Would it be 100 times longer? public static final int W = 400; Why public? ...
  • 13.5k
10 votes
Accepted

Beginner GUI Calculator

At a glance, You should use an array for the buttons. JButton[] buttons = new JButton[10]; Then loop to instantiate. While we're looping, we can add some lambda ...
  • 9,809
10 votes
Accepted

Get initials from full name

Other than the style (yeah, you mentioned it) I think that you can: First a small change: replace this ...
  • 506
10 votes

Learning by translating - Follow the Rubberduck - Part 2: Beta

public static final String RUBBERDUCK_PATH = "RetailCoder.VBE/UI"; This value shouldn't be compiled into the binary. It should be a configurable setting that's ...
10 votes
Accepted

Simon Says: "Optimizations!"

Use interfaces when possible ArrayList<Score> scores = new ArrayList<Score>(); ArrayList<Score> sessionScores = new ArrayList<Score>(); ...
  • 8,589
10 votes
Accepted

Medical diagnosis selector

Layout Don't hard-code dimensions and positions. On a Mac, everything looks wrong. (I don't have your images, so I'm going to ignore them for the rest of this review.) Furthermore, the user of the ...
10 votes
Accepted

Bare bones painter app in Java

A few notes: Is there a reason you aren't using JavaFX? See this answer as to why you should be using it. Since I mostly rewrote your code, I'll do the best I can to explain all the components of ...
  • 21.8k
9 votes

Ping-Pong game in Java

Thanks for sharing this. Looks quite reasonable to me. I have only a few thing I'd change: unnecessary inheritance Your Game class extends ...
8 votes

Workout tracker

One big point is separation of concerns. Mixing GUI-Code with IO-Code is not recommended. Some pseudo Code. Make classes for Workout where just the information of your Workout is Stored. ...
  • 276
8 votes
Accepted

Simple calculator implemented with lambdas

Don't use String labels of GUI elements in flow control It's not good to use the String labels of GUI elements to make decisions. It would be slightly better if you made ...
  • 110k
8 votes
Accepted

Beginner text editor

You are asking all the right questions :) code structure - is it a good idea, to put everything in the one class, or it would be better to divide it between couple of classes? No, putting ...
  • 25k
8 votes

Beginner Minesweeper game

Use the Random class: Random rand = new Random(); ... random = rand.nextInt(side * side); ...
8 votes

"Lights off" puzzle

Note: Each successive tip assumes you've already implemented all of the ones before it, so I recommend reading this in order and implementing it in order. Generic advice NEVER USE ...
  • 5,473
8 votes
Accepted

Java Snake game

Welcome to Code Review! There's lots that can be improved about this code, so lets get started. Use the Random class instead of Math.random() Instead of this ...
  • 8,589
8 votes
Accepted

Java Langton's Ant simulation

Bugs Your simulation is buggy — and not just because an ant is involved! It looks like you intend for each call of AntFrame.SimulationThread.run() to execute one ...
7 votes
Accepted

Factman - A two player numbers game

Large Methods Your methods are definitely too long. As a rule of thumb, if a method doesn't fit on the screen, it becomes hard to understand and maintain. Try to extract code to their own methods, ...
  • 25k
7 votes

Workout tracker

Don't put everything in your main method. It means that you can't: Reuse the code without editing it. Test the code without running the entire application. The UI should not be coupled with the ...
7 votes
Accepted

Replicating a file system

EditorMain ...
  • 5,473

Only top scored, non community-wiki answers of a minimum length are eligible