Java for Beginners · Module 6: Objects and Revision
🎉 You Have Reached the Last Lesson
You started by asking “What is Java?” and you now know the building blocks of the language. This final lesson does two things: it gives you a revision map of the whole series, and four mini-programs that combine many lessons at once. The best way to learn programming is to type these programs yourself, change them, and break them.
| Module | Lessons | You can now… |
| 1. Getting Started | 1 – 5 | explain Java, install the JDK, write and run Hello World |
| 2. Building Blocks | 6 – 11 | use variables, types, casting, input and scope |
| 3. Operators | 12 – 15 | write any expression and know the order it is evaluated in |
| 4. Control Flow | 16 – 20 | make decisions and repeat work with loops |
| 5. Data and Methods | 21 – 28 | use arrays, Strings, methods, recursion, ArrayList and exceptions |
| 6. Objects and Revision | 29 – 30 | create classes and objects, and combine everything |
Figure 1: The six modules of the series.
1️⃣ Revision Map
Module 1: Getting Started
- Lesson 1: What is Java?
- Lesson 2: Installing the JDK
- Lesson 3: JDK, JRE, JVM and WORA
- Lesson 4: JVM Architecture and JIT
- Lesson 5: Hello World Explained
Module 2: Building Blocks
- Lesson 6: Comments, Identifiers, Keywords
- Lesson 7: Variables and Literals
- Lesson 8: Primitive Data Types
- Lesson 9: Type Conversion and Casting
- Lesson 10: Input with Scanner
- Lesson 11: Escape Characters and Scope
Module 3: Operators
- Lesson 12: Arithmetic and ++ / —
- Lesson 13: Relational and Logical Operators
- Lesson 14: Bitwise and Shift Operators
- Lesson 15: Assignment, Ternary, Precedence
Module 4: Control Flow
- Lesson 16: if, if-else, else-if
- Lesson 17: switch
- Lesson 18: while and do-while
- Lesson 19: for Loops and Patterns
- Lesson 20: break, continue, labels
Module 5: Data and Methods
- Lesson 21: Arrays
- Lesson 22: Multidimensional Arrays
- Lesson 23: Strings and the String Pool
- Lesson 24: String Methods and StringBuilder
- Lesson 25: Methods and Overloading
- Lesson 26: Recursion
- Lesson 27: ArrayList and Wrappers
- Lesson 28: Exception Handling
Module 6: Objects and Revision
One-page cheat sheet
| Topic | Remember |
|---|---|
| Types | int, long, double, char, boolean, byte, short, float. String is a class. |
| Casting | Widening is automatic. Narrowing needs (type). int / int drops the fraction. |
| Operators | % remainder, && / || short-circuit, == versus =, use brackets when unsure. |
| Decisions | if / else if / else, switch on int, char, String. Always use braces. |
| Loops | for when the count is known, while when it is not, do-while for at least once. |
| Arrays | Index 0 to length – 1. a.length. Arrays.toString. Copy with clone(). |
| Strings | Immutable. Compare with equals(). Use StringBuilder in loops. |
| Methods | Return type, parameters, pass by value, overloading. |
| Recursion | Base case plus a smaller recursive case. |
| ArrayList | Resizable, holds wrappers: ArrayList<Integer>. |
| Exceptions | try / catch / finally. Specific catches first. |
| OOP | Class = blueprint, object = instance, constructor, this, private + getters. |
2️⃣ Mini-Program 1: Marks Report
Combines Scanner (10), arrays (21), loops (19), methods (25), casting (9) and the else-if ladder (16).
import java.util.Scanner;
public class MarksReport {
static char grade(double pct) {
if (pct >= 90) return 'A';
else if (pct >= 75) return 'B';
else if (pct >= 60) return 'C';
else if (pct >= 40) return 'D';
return 'F';
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many subjects? ");
int n = sc.nextInt();
int[] marks = new int[n];
int total = 0, best = 0;
for (int i = 0; i < n; i++) {
System.out.print("Marks for subject " + (i + 1) + ": ");
marks[i] = sc.nextInt();
total += marks[i];
if (marks[i] > marks[best]) best = i;
}
double pct = (double) total / n;
System.out.println("Total = " + total + " / " + (n * 100));
System.out.printf("Percentage = %.2f%%%n", pct);
System.out.println("Best = subject " + (best + 1) + " (" + marks[best] + ")");
System.out.println("Grade = " + grade(pct));
}
}
How many subjects? 4
Marks for subject 1: 88
Marks for subject 2: 92
Marks for subject 3: 76
Marks for subject 4: 95
Total = 351 / 400
Percentage = 87.75%
Best = subject 4 (95)
Grade = B
- The array is sized from the user’s answer, so it fits any number of subjects.
beststores the index of the highest mark, and the loop updates it whenever a higher mark appears.- The cast
(double) totalkeeps the fraction in the percentage.%%prints a percent sign, and%nis a line break.
3️⃣ Mini-Program 2: ATM Menu
Combines do-while (18), the modern switch (17), if (16) and decimal arithmetic.
import java.util.Scanner;
public class AtmMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double balance = 5000;
int choice;
do {
System.out.println("\n1. Balance 2. Deposit 3. Withdraw 0. Exit");
System.out.print("Choice: ");
choice = sc.nextInt();
switch (choice) {
case 1 -> System.out.println("Balance: " + balance);
case 2 -> {
System.out.print("Amount: ");
double amt = sc.nextDouble();
if (amt > 0) {
balance += amt;
System.out.println("Deposited. New balance: " + balance);
} else {
System.out.println("Invalid amount");
}
}
case 3 -> {
System.out.print("Amount: ");
double amt = sc.nextDouble();
if (amt > 0 && amt <= balance) {
balance -= amt;
System.out.println("Please collect cash. New balance: " + balance);
} else {
System.out.println("Insufficient balance or invalid amount");
}
}
case 0 -> System.out.println("Thank you!");
default -> System.out.println("Invalid choice");
}
} while (choice != 0);
}
}
1. Balance 2. Deposit 3. Withdraw 0. Exit
Choice: 1
Balance: 5000.0
1. Balance 2. Deposit 3. Withdraw 0. Exit
Choice: 2
Amount: 1500
Deposited. New balance: 6500.0
1. Balance 2. Deposit 3. Withdraw 0. Exit
Choice: 3
Amount: 9000
Insufficient balance or invalid amount
1. Balance 2. Deposit 3. Withdraw 0. Exit
Choice: 3
Amount: 2000
Please collect cash. New balance: 4500.0
1. Balance 2. Deposit 3. Withdraw 0. Exit
Choice: 7
Invalid choice
1. Balance 2. Deposit 3. Withdraw 0. Exit
Choice: 0
Thank you!
- do-while shows the menu at least once, and repeats until the user chooses 0.
- Each case with several statements uses braces. The two inner
amtvariables are fine, because each lives in its own block (Lesson 11). - Money in real banking software uses
BigDecimal, as noted in Lesson 8. Adoubleis fine for learning.
4️⃣ Mini-Program 3: Text Tools
Combines Strings (23, 24), methods (25), for-each (19) and StringBuilder.
public class TextTools {
static boolean isPalindrome(String s) {
String clean = s.toLowerCase().replaceAll("[^a-z0-9]", "");
String rev = new StringBuilder(clean).reverse().toString();
return clean.equals(rev);
}
static int countVowels(String s) {
int count = 0;
for (char c : s.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) >= 0) count++;
}
return count;
}
static String capitalizeWords(String s) {
StringBuilder sb = new StringBuilder();
for (String w : s.trim().split("\\s+")) {
sb.append(Character.toUpperCase(w.charAt(0))).append(w.substring(1).toLowerCase()).append(" ");
}
return sb.toString().trim();
}
public static void main(String[] args) {
String[] tests = {"Madam", "A man, a plan, a canal: Panama", "Java"};
for (String t : tests) {
System.out.println("\"" + t + "\" palindrome? " + isPalindrome(t));
}
System.out.println("vowels in 'Object Oriented Programming' = " + countVowels("Object Oriented Programming"));
System.out.println(capitalizeWords(" welcome to jAVA for beginners "));
}
}
“Madam” palindrome? true
“A man, a plan, a canal: Panama” palindrome? true
“Java” palindrome? false
vowels in ‘Object Oriented Programming’ = 9
Welcome To Java For Beginners
replaceAll("[^a-z0-9]", "")removes everything that is not a letter or digit, using a pattern (a regular expression). That is why the sentence with commas and spaces still counts as a palindrome.StringBuilder.reverse()reverses the cleaned text.capitalizeWordssplits on runs of spaces, upper-cases the first letter of each word, lower-cases the rest and joins them back.
5️⃣ Mini-Program 4: Number Tools
Combines methods (25), recursion (26), ArrayList (27), loops (19) and a pattern.
import java.util.ArrayList;
public class NumberTools {
static boolean isPrime(int n) {
if (n < 2) return false;
for (int d = 2; d * d <= n; d++) {
if (n % d == 0) return false;
}
return true;
}
static boolean isArmstrong(int n) {
int original = n, sum = 0, digits = String.valueOf(n).length();
while (n > 0) {
sum += (int) Math.pow(n % 10, digits);
n /= 10;
}
return sum == original;
}
static long factorial(int n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
public static void main(String[] args) {
ArrayList<Integer> primes = new ArrayList<>();
for (int i = 1; i <= 50; i++) {
if (isPrime(i)) primes.add(i);
}
System.out.println("primes up to 50: " + primes);
System.out.print("Armstrong numbers up to 999: ");
for (int i = 1; i < 1000; i++) {
if (isArmstrong(i)) System.out.print(i + " ");
}
System.out.println();
System.out.println("15! = " + factorial(15));
for (int row = 1; row <= 4; row++) {
for (int s = 0; s < 4 - row; s++) System.out.print(" ");
for (int k = 0; k < row; k++) System.out.print("* ");
System.out.println();
}
}
}
primes up to 50: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
Armstrong numbers up to 999: 1 2 3 4 5 6 7 8 9 153 370 371 407
15! = 1307674368000
*
* *
* * *
* * * *
- An Armstrong number equals the sum of its digits, each raised to the number of digits: 153 = 1³ + 5³ + 3³.
Math.powreturns adouble, so the result is cast toint(Lesson 9).- 15! needs
long, because it is far larger than anintcan hold (Lesson 26).
| Mini-program | Topics practised |
| Marks report | Scanner, arrays, loops, methods, casting, if-else ladder |
| ATM menu | do-while, switch, if, double arithmetic |
| Text tools | Strings, StringBuilder, methods, char tests |
| Number tools | methods, loops, recursion, ArrayList, patterns |
Figure 2: What each mini-program revises.
6️⃣ What to Do Next
- Type every program yourself. Do not copy and paste. Typing builds the habit.
- Change one thing and predict the result before you run it. Then read the error messages when you are wrong.
- Solve the practice questions at the end of each lesson.
- Build something small: a quiz, a library list, a student record system using classes and an ArrayList.
- Move on to the next topics: object-oriented programming in depth (inheritance, polymorphism, interfaces), the Collections Framework, file handling and lambdas.
✅ Final Takeaways
- Every program is built from the same few pieces: variables, operators, decisions, loops, methods and objects.
- Read error messages from the top. They tell you what and where.
- Small programs that combine several ideas are the fastest way to revise.
- Consistent practice beats long study sessions.
🎓 Final challenges: (1) ExtendMarksReportso it asks for each subject’s name and prints a full report card. (2) Turn the ATM into a classAccountwith private balance and methods, and let the menu use an object. (3) Write a quiz program that stores questions in anArrayListand counts correct answers. (4) Write a program that reads a sentence and prints the longest word. (5) Create aStudentclass with a constructor and anArrayList<Student>, then print the topper.
All 30 lessons in this series (open course page)
Module 1 · Getting Started
- Lesson 1: What is Java? Features and Uses
- Lesson 2: Installing the JDK and Running Your First Program
- Lesson 3: JDK, JRE, JVM and WORA Explained
- Lesson 4: JVM Architecture and JIT Compilation
- Lesson 5: Hello World Program Explained Line by Line
Module 2 · Building Blocks
- Lesson 6: Comments, Identifiers and Keywords
- Lesson 7: Variables and Literals
- Lesson 8: Primitive Data Types in Java
- Lesson 9: Type Conversion and Casting
- Lesson 10: Taking Input with the Scanner Class
- Lesson 11: Escape Characters and Variable Scope in Java
Module 3 · Operators
- Lesson 12: Arithmetic Operators and Increment/Decrement in Java
- Lesson 13: Relational and Logical Operators in Java
- Lesson 14: Bitwise and Shift Operators in Java
- Lesson 15: Assignment, Ternary Operator and Operator Precedence in Java
Module 4 · Control Flow
- Lesson 16: if, if-else and else-if Ladder in Java
- Lesson 17: switch Statement in Java (Classic and Modern)
- Lesson 18: while and do-while Loops in Java
- Lesson 19: for Loop and Nested Loops (Patterns) in Java
- Lesson 20: break, continue and Labelled Loops in Java
Module 5 · Data and Methods
- Lesson 21: Arrays in Java (One-Dimensional)
- Lesson 22: Multidimensional Arrays in Java
- Lesson 23: Strings and the String Pool in Java
- Lesson 24: String Methods and StringBuilder in Java
- Lesson 25: Methods and Method Overloading in Java
- Lesson 26: Recursion Basics in Java
- Lesson 27: ArrayList and Wrapper Classes in Java
- Lesson 28: Exception Handling Basics in Java