Labs ICT
Pro Login

Exercises

Alright, here is where the real learning happens. I am going to give you two problems and you are going to solve them. Do not worry — I will give you enough hints so you are not totally lost. But the code? That is all you.

Try to figure it out on your own first. If you get stuck, peek at the hints. If you are still stuck after that, come back to the examples page and review. The whole point is to struggle a little — that is how things stick.

Exercise 1: Circle Circumference

Here is a simple one to warm up. Write a program that calculates the circumference of a circle. You have been given a radius of 7.5. The formula is 2 * pi * radius. Use 3.14159 as your value of pi.

public class Main {
  public static void main(String[] args) {
    double radius = 7.5;
    double circumference;

    // Calculate circumference here
    // circumference = 2 * 3.14159 * radius;

    System.out.println("Radius: " + radius);
    System.out.println("Circumference: " + circumference);
  }
}

Hint: Uncomment the calculation line and it should work. But try writing it yourself from scratch instead of just uncommenting. You will learn more.

Try it Yourself →

Exercise 2: Find the Largest Number

This one is a bit trickier. You have three numbers — 12, 25, and 18. Your job is to find the largest among them. You will need to compare numbers using if statements.

Think about how you would do this in real life. You look at each number and keep track of the biggest one you have seen so far. Your code can do the same thing.

public class Main {
  public static void main(String[] args) {
    int a = 12;
    int b = 25;
    int c = 18;
    int max;

    // Find the largest number
    // Write your logic here

    System.out.println("Numbers: " + a + ", " + b + ", " + c);
    System.out.println("Largest: " + max);
  }
}

Hint: Start by assuming a is the largest. Then compare it with b — if b is bigger, update max. Then compare with c. Or you can compare all three at once using &&.

Bonus challenge: Change the program so it works with four numbers instead of three. Or make it find the smallest number too.

Try it Yourself →

How Did It Go?

If you solved both exercises, nice work. Seriously. You have taken the basics and applied them to real problems, which is exactly what programming is all about.

If you got stuck, that is okay too. Go back to the examples, look at how the calculator and grade system work, and try again. There is no rush. Every programmer has been exactly where you are right now.