L03

Due by the end of today by midnight

Starter Files:


SUBMIT   /   Submission Summary

Good Programming Practices

Activity 1
Download and run the program App (linked above).

This program was produced by an AI as a response to the experiment is L00. It was generated from the comment at its start. (I also made some further suggestions and a couple of minor adjustments.)

In terms of completing the task, it is excellent. There are cosmetic differences, for sure, but I think it actually looks a bit nicer than what I made for the experiment.

Nevertheless, as an example of programming, it is not great. I gave this program to GitHub's gpt-5-mini and asked it to "[a]nalyze this file for poor programming practices." It came up with this report:

Summary of poor practices (with fixes):
  • Magic numbers everywhere (card sizes, overlap, colors, offsets) — extract constants.
  • Duplication of layout/math in paint/update/click — centralize into methods to avoid inconsistencies.
  • Hit-testing mismatch: isPointInCard uses (cardWidth/4)/2 instead of the overlap variable — bug; use same overlap logic.
  • Card drawing duplicated (paint vs paintCentre) — factor into one method.
  • Recreates Random frequently (new Random()) — use one shared instance.
  • CardPanel fields package-visible (number, isHovered) — make private with accessors.
  • South/centre panels do custom painting but cards are not Components — consider JComponent per card for clearer event handling.
  • Fixed sizing (setSize, hard-coded preferredSize) — prefer pack()/relative layouts and respond to resizing.
  • No validation for count > 20 (infinite loop risk) — validate input.
  • System.exit in listener and generic class name (App) — prefer controlled shutdown and more descriptive naming.

I'd like to add that it's not just the numbers that should be extracted into constants. The colours should also be listed at the top (with appropriate names) to make them easy to modify.

In the rest of this lab, you are going to fix some of these problems.

Please don't fix more than what's shown below before you submit. I'll be looking for the indicated changes, and having more changes to look thru will just complicate my job.

Do feel free to try to fix more problems later, on your own (with or without the help of AI).

As is good programming practice, you should test your changes as you make them. Don't try to make all the changes below before testing them. That way lies confusion and despair!

Activity 2
Fix the magic numbers. I'd like you to add the following constant declarations and use the new names in the correct locations in the code.
private static final int CARD_WIDTH = 100; private static final int CARD_HEIGHT = 140; private static final int OVERLAP = CARD_WIDTH / 4; // pixels overlapped between cards private static final int HOVER_OFFSET = 10; private static final int MIN_NUMBER = 1; private static final int MAX_NUMBER = 20;
Activity 3
Make constants for the colours as well. Find every place in App where it creates a new Color and create a constant for that colour. Give it an appropriate name. Use the name in the code.
You do not need to replace the uses of Color.BLACK. Black is being used for the edges of the cards and the numbers. It's unlikely anyone would want to change those colours.
Activity 4
Find and replace every instance of new Random with a reference to a shared instance.
Activity 5
Lastly, we're going to simplify the method paintCentre.

NOTE that, other than the lines that declare and set x, y, width and height, the method paintCentre has exactly the same code as paint:

Also note that x, y, width and height, (along with g) are exactly the values that paint needs in order to do its job.

Update the code for paintCentre so that it calls paint with the correct arguments to get the card drawn where it's supposed to be drawn.

Submit this/these files:


SUBMIT   /   Submission Summary