Breaking Down a CCC Solution

The Canadian Computing Competition is a prestigious computer science competition available for KTBYTE and other students to show off their programming skills. You can see our comprehensive post of the competition here. In this post, we’ll break down the code we shared with you there to help you understand exactly how a CCC solution works.

If you’ve taken KTBYTE programming classes, but haven’t used “standard Java”, read data files, or worked outside of the KTBYTE Coder before, you’ll see that there’s only a few new things to learn about setting up a CCC solution program. The main work of solving any CCC problem will still come from your general knowledge of programming and debugging that you’ve been learning from the start. You’ll see that most Java code elements you’ve learned within the Processing system in our coder, like variables, loops, and arrays, work just the same in this system!

Here’s a solution to the CCC 2022 Junior Division problem 1, “Cupcake Party”:

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(;
        int reg = in.nextInt();
        int sm = in.nextInt();
        int ans = reg * 8 + sm * 3 - 28;

The first steps for any standard Java program is a class definition, and a function (also called a method) defined inside it named main – this is the starting point of the program, like setup is the starting point of a Processing program. Both of these definitions must be public, and the main method must be static. You’ll see more details on these keywords in our courses, but they basically allow the main method to act like other functions you can define in Processing or other languages. The main method requires the input variable String[] args. Technically this variable can be used to provide information to a program when it starts running, but in CCC problems it will not be used – it must be present, but the rest of your code can ignore it. Also, CCC solutions must use the class name Main, although other Java programs can be named whatever you like.

This may feel like a lot if you’re used to Processing rather than standard Java, but notice how the overall setup of a class definition, method definition, and even input variables all match up to what you’ve learned before. You have to learn a few additional terms and specific names to get things going, but it’s not actually reinventing how to write code at all!

Let’s look at other specific details from top to bottom.

  • Lines 1-2: import statements. Standard Java requires you to specify where to find certain classes and methods come from if they’re not part of the absolute core of the language. (For example, String is part of the core language, but Scanner is not.) Luckily, in CCC problems it’s very straightforward: Just import exactly these two things every time. util stands for utilities and includes useful data structures as well as Scanner, which we’ll use to read our data. io stands for input/output and provides tools for reading files.
  • Line 5: throws Exception (at the end of the method definition’s top line). This tells Java that if there’s any issue reading the data, the program is allowed to just go ahead and crash. This is required because Java does have features to try to recover from errors without crashing, but in a CCC problem we can rely on the system to ensure no input errors will come up in the first place.
  • Line 6: The Scanner class is designed to easily read text data. When we make a new Scanner, it must be set up to read from a specific source. specifies that “standard input” will be the source, which in many coding environments means you can type it in through the console. (See our overview post linked above for how this works in the replit coding website.) Once the scanner is created, we store it in a variable called in. Imagine the scanner as a robot holding your input text, just waiting for you to ask it what the text says.
  • Lines 7-8: The .nextInt() method is called twice. Each time, it causes the scanner to look for the next piece of text from the input and turn it into an int value. Just like any int value, we could store it, pass it into another function, do arithmetic on it, and so on. Since the problem description said the two int values in this problem would be the number of regular and small-sized boxes of cupcakes, we’ve chosen to store them in two int variables with appropriate names. Remember, .nextInt() is a method connected to the Scanner object we created earlier, so it is called “attached” to the in variable using the . dot operator. (For KTBYTE students who have worked in the coder, this is very similar to how Sprite methods are called.)
  • Line 9: The .close() method marks the end of the input. This step is optional, but many editors will give a warning if you don’t close your scanners, and it’s a nice way to make it clear in your code where the input data is finished being read. Your code will crash if you try to read more data from the scanner after called .close(), so be careful!
  • Line 11: This line makes a variable for the answer, and calculates its value based on the input data. Since this is a very simple problem, it only takes one line. Notice there is nothing special or different here, just standard Java code most KTBYTE students are already used to.
  • Line 12: Standard java uses System.out.println instead of a plain println for output, but the concept is exactly the same. Technically, we say this uses “standard output”, which usually appears in a system console in most coding setups, and is the style of output the CCC requires.

That’s it! Again, you’ll notice that while there are some new class names and method names to learn when you do your first problem, the way that statements work, variables are created, methods are called, and so on, all match other Java code you’ve seen.

Leave a Reply

Your email address will not be published. Required fields are marked *