Part 1
Printing to the screen
Every Java program starts inside the main method. To show something on the screen, use System.out.println(...).
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}println prints and then moves to a new line. print (without "ln") stays on the same line.
System.out.println("I love Java"); // goes to a new line after
System.out.print("1 ");
System.out.print("2 ");
System.out.print("3 ");
// output: 1 2 3 (all on one line)You can join text and numbers with +. Be careful: parentheses change the result!
System.out.println("Total value: " + (5 + 10)); // Total value: 15
System.out.println("Total value: " + 5 + 10); // Total value: 510 (!)
System.out.println("Result: " + 9 / 3); // Result: 3 (division runs first)
System.out.println(50 - 15); // 35 (no quotes = just math)Remember: text inside "double quotes" is printed exactly as written. Math without quotes is calculated first. When text comes first, Java glues values to it one by one, from left to right.
Practice time
These are the same kind of questions you solved before. Write each one as a small program.
Hint
Use
System.out.println("...").Hint
Same as question 1 — the text goes inside double quotes.
Hint
Put the math inside
printlnwith no quotes around it.Hint
Put the addition inside parentheses:
"..." + (5 + 10).Hint
Division runs before the text join. So what number comes after "Result: "?
Hint
When text comes first, Java joins left to right: first "Sum: " + 5, then + 10.
Hint
Use
System.out.print— without the "ln".Hint
Any text between double quotes works.
Hint
Each
printlnautomatically ends its line.Hint
*is the multiplication symbol. No quotes needed.
Part 2
Variables & data types
A variable is a named box that holds a value. You always write: type, name, equals, value.
String learning = "I am learning Java"; // text
int t = 50; // whole number
float pi = 3.14f; // decimal — needs the letter f
double price = 19.99; // decimal — no f needed
boolean isJavaFun = true; // true or false
char firstLetter = 'J'; // ONE character, single quotes
long population = 1000000; // very big whole number
System.out.println(learning);
System.out.println("price = " + price);You can put the result of math into a variable, then print it with a label.
int x = 5;
int y = 10;
int result = x + y;
System.out.println("Sum result: " + result); // Sum result: 15Naming rules
// OK: myCat my_name $money isAdult priceOfLipstick
// NOT OK: 2cats (cannot start with a digit)
// NOT OK: my-name (dash is not allowed — only _ and $ are)
// Style: camelCase for names, booleans start with is/has (isAdult, hasCar)Remember: float needs an f at the end (3.14f), char uses single quotes 'J', and String uses double quotes "Java".
Practice time
Just like your homework: create the variable, then print it.
Hint
Type comes first:
String learning = "...";Hint
intis for whole numbers.Hint
booleans hold only
trueorfalse, without quotes.Hint
A float value must end with
f.Hint
A char takes exactly one character in single quotes.
Hint
longis like int, but for very big numbers.Hint
Same pattern as question 1.
Hint
Three variables: x, y, and result = x + y. Then join text + result.
Hint
Product means multiplication:
a * b.Hint
Take the old value of x (4) and add 7 to it.
Hint
A name cannot start with a digit. Only
_and$are allowed as symbols.Hint
double is a decimal type that does not need the f letter.
Part 3
Math operators, % and casting
Java has five math operators: + - * / and %. The last one, modulus, gives the remainder after division — you used it a lot!
int number = 8;
int square = number * number; // 64
int cube = number * number * number; // 512
int remainder = 23 % 5; // 3 (23 / 5 = 4, remainder 3)
System.out.println("23 mod 5 = " + remainder);Order matters. Multiplication and division run before plus and minus. Parentheses always win.
int m = 5 + 20 * 2; // 45 (20*2 first)
int n = (5 + 20) * 2; // 50 (parentheses first)
int half = 7 / 2; // 3, NOT 3.5 — int division throws away the decimals!counter = counter + 1;
counter += 1;
counter++;
++counter; // all four do the same thing hereCasting changes one type into another. Going from decimal to int loses the decimal part.
float price = 12.99f;
int intPrice = (int) price; // 12 — decimals are cut off, not rounded
double average = (double) 7 / 2; // 3.5 — cast first, then divideint n = 47;
int tens = n / 10; // 4
int ones = n % 10; // 7Remember: x % 2 == 0 means x is even. a % b == 0 means a is exactly divisible by b. int divided by int is always an int.
Practice time
Mix of coding tasks and "trace it in your head" puzzles — just like the CihanOrnek drills.
Hint
Square = the number times itself.
Hint
Cube = number * number * number.
Hint
(number * 3 + 5)Hint
Parentheses first:
((a + b) * 3 + 8).Hint
Use the
%operator.Hint
7 × 6 = 42. What is left over?
Hint
4 × 4 = 16. What remains?
Hint
Do it step by step: 3 → 8 → ... → ?
Hint
Both numbers are ints, so the result is an int.
Hint
(5 / 2)is int division — the decimals disappear before the multiply.Hint
n / 10andn % 10.Hint
Long form,
+=, and the two++forms.Hint
Casting to int cuts off the decimals. It does not round.
Hint
Cast one side:
(double) sum / count.Hint
Multiplication before addition — unless parentheses say otherwise.
Part 4
User input with Scanner
Scanner lets your program ask the user for a value. First import it, then create it, then read.
import java.util.Scanner;
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a number: ");
int number = kb.nextInt();
System.out.println("You entered: " + number);String name = kb.nextLine(); // reads a whole line of text
double radius = kb.nextDouble(); // reads a decimal number
double area = 3.14159 * radius * radius;
System.out.println("Circle area: " + area);Remember: nextInt() for whole numbers, nextDouble() for decimals, nextLine() for text. Always print a friendly message before reading, so the user knows what to type.
Practice time
Run each one and type real values in the console.
Hint
Call
kb.nextInt()twice, into two variables.Hint
nextLine()reads text. Join with+.Hint
Use
doubleandnextDouble(). Area = pi * r * r.Hint
Two
nextInt()calls, then multiply.Hint
Read into a variable, then print text + variable.
Hint
int division loses decimals — cast the sum with
(double).Hint
A comparison like
age > 18IS a boolean value — assign it directly.Hint
Digits again:
n / 10andn % 10, then add them.Hint
Read an int, print with a label.
Hint
One reads a whole number, the other reads a whole line of text.
Part 5
If / else / else-if
An if lets your program make decisions. You compare values with > < >= <= == and !=.
int shoePrice = 20;
if (shoePrice < 25) {
System.out.println("I can buy these shoes for sport");
}
int mealCalories = 700;
if (mealCalories > 600) {
System.out.println("This meal is very high-calorie!");
} else {
System.out.println("This meal is healthy");
}For more than two roads, chain conditions with else if. Java checks them from top to bottom and runs only the first one that matches.
int hourOfDay = 14;
if (hourOfDay < 6) {
System.out.println("Night time");
} else if (hourOfDay < 18) {
System.out.println("Daytime");
} else {
System.out.println("Evening time");
}Chars are compared with == too. And a very useful pattern from your exercises: set a variable inside the branches, print once at the end.
char grade = 'A';
if (grade == 'A') {
System.out.println("Excellent");
} else if (grade == 'B') {
System.out.println("Good");
} else {
System.out.println("Invalid grade!");
}
int age = 10;
int ticketPrice;
if (age < 12) {
ticketPrice = 10;
} else {
ticketPrice = 20;
}
System.out.println("Ticket price: " + ticketPrice);Remember: one = assigns a value, two == compares. In an else-if ladder you don't need to repeat the lower bound — if the code reached else if (age < 12), age is already 4 or more.
Practice time
These come straight from your if/else homework set.
Hint
A single
ifis enough — no else needed.Hint
Use the
>operator.Hint
Three roads:
> 0,== 0, else.Hint
Classic if/else, two roads.
Hint
Same shape as question 4.
Hint
An else-if ladder with three roads.
Hint
Check the smallest range first, then let else-if handle the rest.
Hint
Three-road ladder again.
Hint
Same ladder pattern — pick your boundaries carefully.
Hint
Four roads: if, two else-ifs, and a final else.
Hint
Compare chars with
==and single quotes:grade == 'A'.Hint
Make
String message = "";first, fill it in the branches.Hint
Assign inside the branches; only one println at the bottom.
Hint
Ladder from the top: check
>= 90first, then you don't need upper bounds.Hint
Because else-if runs in order,
humanAge < 12already means "4 or more".
Part 6
Logical operators: &&, ||, !
Sometimes one condition is not enough. && means AND (both must be true), || means OR (at least one is true), ! means NOT.
int number = 30;
if (number % 3 == 0 && number % 5 == 0) {
System.out.println("divisible by both 3 and 5");
}
if (number % 2 == 0 && number % 4 != 0) {
System.out.println("divisible by 2 but not by 4"); // true for 6, 14, 30...
}A comparison is a boolean value. You can store it in a variable with a nice name.
int x = 8;
boolean isEven = (x % 2) == 0; // "is it even?" → true
int age = 70;
boolean isEligibleForRetirement = age > 65;
System.out.println(isEligibleForRetirement); // trueint temperature = 33;
int humidity = 75;
if (temperature > 30 && humidity > 70) {
System.out.println("The weather is stifling");
} else if (temperature < 15 && humidity < 30) {
System.out.println("The weather is dry and cool");
}Remember: % + logical operators is a power combo: even = % 2 == 0, odd = % 2 != 0, "divisible by a but not b" = % a == 0 && % b != 0.
Practice time
The mod + logic drills — your favourite question folder.
Hint
number % 2 == 0Hint
Multiple of 3 means remainder 0 when divided by 3.
Hint
Two
%checks joined with&&.Hint
Use
!=for the "not" part:% 4 != 0.Hint
Odd =
% 2 == 1. Join with&&.Hint
Two conditions per branch, joined with
&&.Hint
Same two-variable pattern as question 6.
Hint
Two conditions joined with
&&in each branch — you solved this one with funny messages.Hint
No if needed at all — assign the comparison directly.
Hint
boolean isEligibleForRetirement = age > 65;
Part 7
While loops
A while loop repeats code as long as a condition is true. You need three things: a counter, a condition, and a step that changes the counter — or the loop never ends!
int i = 0;
while (i < 5) {
System.out.println("*");
i++; // never forget this line!
}int number = 1;
int i = 1;
while (i <= 5) {
System.out.println(number);
number = number * 2;
i++;
}int i = 0;
while (true) { // runs forever...
System.out.println("Hello");
i++;
if (i == 5) {
break; // ...until break stops it
}
}Remember: if you forget i++, the condition never becomes false and the loop runs forever. This was the bug in one of the repo files — the loop body only counted and never printed!
Practice time
Solve each one with a while loop, even if a for loop feels easier.
Hint
Counter starts at 0, condition
i < 5, and i++ inside.Hint
Same shape, different condition number.
Hint
Start the counter at 1 and print the counter itself.
Hint
Scanner first, then
while (i <= n).Hint
Two variables: the number (× 2 each round) and a counter.
Hint
Start at 20 and subtract 2 each round:
i -= 2.Hint
Condition
i >= 1, stepi--.Hint
Put an
if (i % 3 == 0)inside the loop.Hint
if/else inside the loop, with
% 2.Hint
Print
i * iinstead of i.Hint
Count inside, and
breakwhen the counter hits 5.Hint
Make
int total = 0;before the loop and dototal += i;inside.
Part 8
For loops
A for loop puts the counter, the condition, and the step all on one line. Best choice when you know how many times to repeat.
for (int i = 1; i <= 5; i++) {
System.out.println(i); // 1 2 3 4 5
}
for (int i = 20; i >= 2; i -= 2) {
System.out.println(i); // 20 18 16 ... 2
}
for (int i = 1; i <= 16; i = i * 2) {
System.out.println(i); // 1 2 4 8 16
}The accumulator pattern: build up a total inside the loop.
int total = 0;
for (int i = 1; i <= 5; i++) {
total += i;
}
System.out.println("Total = " + total); // 15for (int i = 1; i <= 30; i++) {
if (i % 3 == 0) {
System.out.println("Fizz");
} else if (i % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(i);
}
}Remember: the step doesn't have to be i++ — it can be i--, i -= 2, even i = i * 2. And an if inside a loop is how you filter numbers.
Practice time
The biggest topic in your repo — so the biggest quiz.
Hint
for (int i = 1; i <= 5; i++)Hint
The loop body doesn't have to use i.
Hint
Either filter with
% 2 == 0, or start at 2 and step by 2.Hint
Accumulator + an if filter inside the loop.
Hint
if / else if / else inside the loop. Order matters!
Hint
Odd =
% 2 != 0.Hint
Use
System.out.print(i + " ")and an if.Hint
Two
%checks joined with&&.Hint
An else-if ladder inside the loop.
Hint
This one wants
||, not&&.Hint
Print only when
i % 4 != 0.Hint
Divisible by 2 and 5 is the same as divisible by 10.
Hint
if → print the message, else → print the number.
Hint
Descending loop +
% 2 != 0check.Hint
The step can be
i = i * 2.
Part 9
Strings & string methods
A String has built-in methods — small tools you call with a dot. These are the ones you practiced:
String name = "Thailand";
name.length(); // 8 — how many characters
name.charAt(0); // 'T' — character at a position (starts at 0!)
name.toUpperCase(); // "THAILAND"
name.toLowerCase(); // "thailand"
name.contains("ai"); // true
name.startsWith("T"); // true
name.endsWith("d"); // true
name.indexOf("a"); // 2 — position of the first 'a'
name.replace('a', '*'); // "Th*il*nd"
name.substring(0, 4); // "Thai" — from 0 up to (not including) 4Comparing text: use .equals(), never ==. For "ignore big/small letters" use .equalsIgnoreCase().
String name1 = "HasoMis";
String name2 = "HAsOmiS";
name1.equals(name2); // false — case matters
name1.equalsIgnoreCase(name2); // trueString email = "denizukad@gmail.com";
int atIndex = email.indexOf("@");
String username = email.substring(0, atIndex); // "denizukad"
String website = email.substring(atIndex + 1); // "gmail.com"String word = "Java";
for (int i = 0; i < word.length(); i++) {
System.out.println(word.charAt(i));
}Remember: positions start at 0. The last character is at length() - 1. In substring(start, end) the start is included, the end is not.
Practice time
Ask the user for the text with Scanner, like the original exercises did.
Hint
.length()Hint
.charAt(0)and.charAt(city.length() - 1)Hint
.toUpperCase()Hint
.equalsIgnoreCase()Hint
.contains("@")Hint
.replace('a', '*')Hint
.substring(1, s.length() - 1)Hint
.startsWith("S")gives a boolean directly.Hint
A for loop from 0 to length, with
.charAt(i).Hint
Find "@" with
.indexOf, then.substring(0, thatIndex).Hint
You need two indexOf results — one for "@", one for ".".
Hint
.substring(0, 3)Hint
.substring(word.length() - 2)— one argument goes to the end.Hint
.substring(0, word.length() - 1)Hint
word.substring(0, 2) + word
Part 10
Arrays
An array holds many values of the same type in one variable. Each value has a position (index) starting at 0.
String[] colours = {"Red", "Blue", "Green", "Yellow"};
System.out.println(colours[0]); // Red — first element
for (int i = 0; i < colours.length; i++) {
System.out.println(colours[i]);
}int[] numbers = {5, 12, 9, 21, 17, 6};
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 == 0) {
System.out.println(numbers[i]); // only 12 and 6
}
}int[] evens = {2, 4, 6, 8, 10, 12};
for (int i = evens.length - 1; i >= 0; i--) {
System.out.println(evens[i]); // 12 10 8 ...
}
int[] weights = {60, 63, 64, 59, 55, 52};
int total = 0;
for (int i = 0; i < weights.length; i++) {
total += weights[i];
}int[] a = new int[5]; // 5 empty slots, all 0
for (int i = 0; i < a.length; i++) {
a[i] = (i + 1) * 2; // fills with 2, 4, 6, 8, 10
}Remember: for arrays it is .length with no parentheses (Strings use .length() with parentheses). The last index is length - 1.
Practice time
Create the array yourself, then loop over it.
Hint
{"Red", "Blue", ...}then loop to.length.Hint
Just list them in the curly braces.
Hint
chars use single quotes:
{'A', 'B', ...}Hint
Same pattern as question 1.
Hint
Five chars in braces, then a loop.
Hint
An if inside the loop:
numbers[i] > 5.Hint
numbers[i] % 2 == 0Hint
Each element is a String, so it has
.length().Hint
Character.isUpperCase(letters[i])Hint
Start the loop at
length - 1and go down withi--.Hint
Accumulator +
% 2 != 0filter.Hint
.endsWith("r")Hint
% 3 == 0 && % 9 != 0Hint
A number ends in 0 when
% 10 == 0.Hint
Fill with
a[i] = (i + 1) * 2;. For a decimal average, remember the(double)cast.
Part 11
ArrayList
An ArrayList is like an array that can grow. You add items one by one. For numbers and chars you use the "big" type names: Integer, Double, Character.
import java.util.ArrayList;
ArrayList<String> cities = new ArrayList<>();
cities.add("Istanbul");
cities.add("Bangkok");
cities.add("Chiang Mai");
cities.size(); // 3 (not .length!)
cities.get(0); // "Istanbul"
System.out.println(cities); // [Istanbul, Bangkok, Chiang Mai]// 1. index loop
for (int i = 0; i < cities.size(); i++) {
System.out.println(cities.get(i));
}
// 2. for-each — the easy one
for (String city : cities) {
System.out.println(city);
}
// 3. forEach with a lambda
cities.forEach(city -> {
System.out.println(city);
});ArrayList<Integer> days = new ArrayList<>();
days.add(3); days.add(7); days.add(10); days.add(15); days.add(22);
for (int day : days) {
if (day > 10) {
System.out.println("day = " + day); // 15 and 22
}
}Remember: array → .length, String → .length(), ArrayList → .size(). Three different spellings — a classic exam trap!
Practice time
The Thailand questions — your repo's favourite theme.
Hint
new ArrayList<>()then.add(...)for each.Hint
For int values the list type is
Integer.Hint
for (String day : days)Hint
Same as question 3.
Hint
Decimals →
Double.Hint
for-each + if.
Hint
Compare Strings with
.equals(...), join with||.Hint
% 2 == 1Hint
city.length() > 7Hint
Index loop from
size() - 1down to 0, with.get(i).Hint
Accumulator variable +
% 2 == 0filter.Hint
list.forEach(item -> { ... });
Part 12
Set & HashMap
A Set is a collection with no duplicates — if you add the same thing twice, it keeps only one.
import java.util.Set;
import java.util.HashSet;
Set<String> cityNames = new HashSet<>();
cityNames.add("Bangkok");
cityNames.add("Bangkok"); // ignored — already there
cityNames.add("Phuket");
for (String city : cityNames) {
System.out.println(city); // Bangkok appears only once
}A HashMap stores pairs: a key and its value. You look values up by key.
import java.util.HashMap;
HashMap<String, String> names = new HashMap<>();
names.put("Deniz", "Turkey");
names.put("Somchai", "Thailand");
names.get("Deniz"); // "Turkey"
for (String key : names.keySet()) {
String value = names.get(key);
System.out.println(key + " lives in " + value);
}
// or with a lambda:
names.forEach((key, value) -> {
System.out.println(key + " -> " + value);
});Remember: List = keeps order, allows duplicates. Set = no duplicates. Map = key → value pairs. put to add, get to read, keySet() to loop.
Practice time
Short section, short quiz.
Hint
A Set silently drops the second "Bangkok".
Hint
The list keeps the duplicates, the set does not.
Hint
for-each over the set,
.equals()and||.Hint
.contains("t")Hint
student.get("name")Hint
System.out.println(map)prints all pairs.Hint
get returns just the value for that key.
Hint
for (String key : map.keySet())thenmap.get(key).Hint
Join key + text + value inside the loop.
Hint
map.forEach((key, value) -> { ... });
Part 13
Methods (functions)
A method is a named block of code you can reuse. Instead of copying the same lines again and again, you write them once and call the name.
public static returnType methodName(parameters) { ... }
// simplest: no parameters, returns nothing (void)
public static void printWelcome() {
System.out.println("Welcome to Java programming!");
}
// with parameters
public static void printDifference(int a, int b) {
System.out.println("Difference: " + (a - b));
}
public static void main(String[] args) {
printWelcome();
printDifference(10, 4); // Difference: 6
}A method can also return a value. Then void becomes the value's type, and the method ends with return.
public static int multiply(int a, int b) {
return a * b;
}
public static String checkEvenOdd(int number) {
if (number % 2 == 0) {
return "Even";
} else {
return "Odd";
}
}
int result = multiply(4, 3); // 12
System.out.println(checkEvenOdd(9)); // Oddpublic static String repeatLaugh(int times) {
String result = "";
for (int i = 0; i < times; i++) {
result += "ha";
}
return result;
}
// repeatLaugh(3) → "hahaha"The best trick from the Catan exercises: one method's return value becomes another method's parameter.
public static int calculateKnightPoints(int knightCards) {
return knightCards; // each knight = 1 point
}
public static void printKnightPoints(int points) {
System.out.println("Knight points: " + points);
}
printKnightPoints(calculateKnightPoints(5)); // Knight points: 5Remember: void = prints or does something, returns nothing. A return type (int, String...) = gives a value back that you can store or pass on. After return, the method stops.
Practice time
Sweden and Catan questions included — of course.
Hint
public static void, empty parentheses.Hint
One int parameter, join text + parameter.
Hint
Two parameters, separated by a comma.
Hint
if/else inside the method — and be careful not to call the method from inside itself!
Hint
A String parameter this time.
Hint
if / else if / else — three roads.
Hint
A for loop inside the method, using n as the limit.
Hint
Return type is
int, not void.Hint
return a * b;Hint
Return type String; a return in each branch.
Hint
Start with an empty String and add "ha" in a loop.
Hint
Accumulator inside,
return sum;at the end.Hint
settlements * 1 + cities * 2Hint
The answer is the smaller of the two values.
Hint
printKnightPoints(calculateKnightPoints(5));
Part 14
Classes & objects
A class is a blueprint. It says what data (fields) something has. From one class you can create many objects with new.
public class Phone {
String brand;
String model;
int storage;
}
// in main: ClassName variableName = new ClassName()
Phone phone1 = new Phone();
phone1.brand = "iphone";
phone1.model = "iphone 13";
phone1.storage = 36;
Phone phone2 = new Phone();
phone2.brand = "samsung";A constructor lets you fill the fields in one line. Inside it, this.name means "the field of this object".
public class Employee {
String name;
String position;
int salary;
public Employee(String name, String position, int salary) {
this.name = name;
this.position = position;
this.salary = salary;
}
}
Employee employee1 = new Employee("Deniz", "QA", 85000);To print an object nicely, add a toString() method — then println(object) just works. (IntelliJ can generate it for you.)
@Override
public String toString() {
return "Employee{name='" + name + "', position='" + position +
"', salary=" + salary + "}";
}
System.out.println(employee1);
// Employee{name='Deniz', position='QA', salary=85000}Objects can live inside lists, and everything you learned before still works:
List<Country> countries = new ArrayList<>();
countries.add(hungary);
countries.add(vietnam);
for (Country country : countries) {
if (country.population > 50000) {
System.out.println(country.capitalCity);
}
}Remember: class = the blueprint, object = one real thing made from it. new creates the object. A class can have more than one constructor (with different parameters) — that is called overloading.
Practice time
The 15 class questions in your repo all follow this pattern: make the class, create 3 objects, print them.
Hint
No constructor needed — use
phone1.brand = "...".Hint
The helper is
static voidand takes the whole object as its parameter.Hint
With toString,
System.out.println(book1)is enough.Hint
The constructor fills all fields in one line — no field assignments needed.
Hint
Exactly the pattern from the example above.
Hint
Remember: float fields take values like
8.5f.Hint
Same recipe: constructor, toString, three objects.
Hint
All three fields can be simple types: String, int, int.
Hint
Two constructors with different parameter lists — this is overloading.
Hint
The parameter and the field often have the same name —
this.namepicks the field.Hint
for-each over the list, then
country.population > 50000.Hint
Mix of the two styles: set fields by hand, print with println(object).