Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 91 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,87 +1,131 @@
# Java Examples Collection

> Designed for the AUEB CS BSc Java Course
>> https://eclass.aueb.gr/courses/INF176/
Designed for the AUEB CS BSc Java Course:
https://eclass.aueb.gr/courses/INF176/

This repository contains a collection of Java programming examples used for teaching core object-oriented programming (OOP) concepts in an engaging and applied way. Each example is organized as a separate package/module, and includes working code, documentation, and console interaction.
This repository contains small, runnable Java examples and course projects used
to teach core programming and object-oriented programming concepts. Topic-based
examples live under `gr/aueb/cs/examples`, while larger mini-projects live under
`gr/aueb/cs/projects`.

## 📁 Project Structure
## Project Structure

Each top-level package contains a complete, runnable Java example:
### `gr/aueb/cs/examples/introduction`

---
Introductory Java examples covering variables, control flow, simple classes, and
basic simulation.

### 🎲 `montyhall/`
### `gr/aueb/cs/examples/strings`

A full implementation of the Monty Hall game:
Examples for working with Java strings and common string operations.

- OOP structure (`Door`, `MontyHallGame`, `Strategy`)
- Interactive version (console-based)
- Simulator to run thousands of trials
- Visual comparison of strategies (switch vs stay)
### `gr/aueb/cs/examples/inheritance`

➡️ Teaches: classes, interfaces, composition, simulations, user interaction
Examples for inheritance, `final`, dynamic binding, and polymorphism.

---
### `gr/aueb/cs/examples/abstraction`

### 🚶 `walkroutes/`
Examples for abstract classes and salary-based employee models.

A pedestrian routing system on a city grid:
### `gr/aueb/cs/examples/interfaces`

- Location graph representation
- Obstacle-aware pathfinding
- Modular packages: `model`, `routing`, `map`, `simulation`
Small demonstrations of interfaces and interface-based design.

➡️ Teaches: graphs, modular design, file input, BFS-style logic
### `gr/aueb/cs/examples/collections`

---
Examples using Java collections such as lists, queues, iterators, and traversal
patterns.

### 🎯 `demo/`
### `gr/aueb/cs/examples/generics`

Demonstrates the difference between:
Examples for generic classes, generic methods, bounded type parameters,
comparators, sorting, and type erasure.

- `Math.random()` (static, simple, no seed)
- `Random` class (object-based, supports seeding)
- Repeatable pseudo-randomness
### `gr/aueb/cs/examples/exceptions`

➡️ Teaches: randomness, testing, debugging with seeded values
Examples for exception handling, throwing exceptions, and safer error handling.

---
### `gr/aueb/cs/examples/files`

### 🏨 `hotelbooking/` *(optional extension)*
Examples for reading and writing files, UTF-8 text, URLs, and CSV-style input.

Simulates a basic hotel booking system with multiple room types.
### `gr/aueb/cs/examples/unit`

➡️ Teaches: inheritance, encapsulation, polymorphism
A small calculator example with a matching unit test.

---
### `gr/aueb/cs/examples/profiling`

### 🎬 `movies/` *(optional extension)*
Simple performance and profiling examples comparing data structures, primitive
types, wrapper types, and buffer capacity choices.

Loads movie reviews from a file and filters recommendations by genre, rating, or sentiment.
### `gr/aueb/cs/examples/graphics`

➡️ Teaches: file I/O, `ArrayList`, filtering, basic NLP
Graphical Java examples, including a tag cloud visualization.

---
### `gr/aueb/cs/examples/sound`

## 🧪 How to Run
Audio-related examples, including a simple alarm demo.

Use the terminal or your favorite IDE (e.g., IntelliJ, Eclipse):
### `gr/aueb/cs/examples/review`

Review examples for dynamic dispatch, constructors, exceptions, anonymous
classes, and randomness.

### `gr/aueb/cs/projects`

Larger examples and student-friendly mini-projects:

- `montyhall`: Monty Hall game logic, strategies, console UI, and simulation.
- `walkroute`: Walking route demos using linked structures, search, and agents.
- `captcha`: A simple CAPTCHA generator and checker.
- `javagotchi`: A virtual pet project.
- `javagotchiWeb`: A Spring Boot web version of the Javagotchi project.

## How to Run

From the repository root, compile and run a single class by using its package
path. For example:

```bash
# Compile from src root
javac montyhall/app/Main.java
java montyhall.app.Main
javac gr/aueb/cs/examples/introduction/Variables.java
java gr.aueb.cs.examples.introduction.Variables
```

Another example:

```bash
javac gr/aueb/cs/examples/inheritance/Polymorphism.java
java gr.aueb.cs.examples.inheritance.Polymorphism
```

# Or run another example
javac demo/RandomComparison.java
java demo.RandomComparison
For examples that define multiple classes in the same package, compile the
source file and then run the public class:

```bash
javac gr/aueb/cs/examples/interfaces/Demo.java
java gr.aueb.cs.examples.interfaces.Demo
```

The Spring Boot project has its own README:

```text
gr/aueb/cs/projects/javagotchiWeb/README.md
```

## Running Tests

The `gr/aueb/cs/examples/unit` package contains small JUnit 5 tests for the
calculator example. Run them from the repository root with:

```bash
mvn test
```

## ✏️ Contribution
## Contributing

To contribute to this repo, [this cheatsheet](/git_cheatsheet.md) may help with the first steps.
To contribute to this repository, the [Git cheatsheet](git_cheatsheet.md) may
help with the first steps.

## 📄 License
## License

This project is licensed under the [MIT License](LICENSE).
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

package gr.aueb.cs.abstraction;
package gr.aueb.cs.examples.abstraction;

public abstract class Employee {
private String firstName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.abstraction;
package gr.aueb.cs.examples.abstraction;

abstract public class JuniorSalariedEmployee extends SalariedEmployee {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

package gr.aueb.cs.abstraction;
package gr.aueb.cs.examples.abstraction;

public class SalariedEmployee extends Employee {
private double weeklySalary;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.collections;
package gr.aueb.cs.examples.collections;
// // Based on the example in the book of Deitel (pp 834, 9th edition)
// https://github.com/pdeitel/JavaHowToProgram11e_EarlyObjects/blob/7bf604beb846d3bb99b52d8b330da5e354a0224a/examples/ch16/fig16_02/CollectionTest.java
// Collection interface demonstrated via an ArrayList object.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.collections;
package gr.aueb.cs.examples.collections;

import java.util.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.collections;
package gr.aueb.cs.examples.collections;

import java.util.ArrayList;
import java.util.Iterator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.collections;
package gr.aueb.cs.examples.collections;
// Working on the example of Deitel (pp 837, 9th edition)
// re. Lists, LinkedLists and ListIterators.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.collections;
package gr.aueb.cs.examples.collections;

import java.util.Comparator;
import java.util.PriorityQueue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.exceptions;
package gr.aueb.cs.examples.exceptions;

import java.util.InputMismatchException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.exceptions;
package gr.aueb.cs.examples.exceptions;

import java.util.Scanner;

Expand Down
11 changes: 11 additions & 0 deletions gr/aueb/cs/examples/exceptions/ThrowTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gr.aueb.cs.examples.exceptions;

public class ThrowTest {
public static void main(String[] args) {
try {
throw new RuntimeException("Σφάλμα!");
} catch (RuntimeException e) {
System.out.println("Πιάστηκε εξαίρεση: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.files;
package gr.aueb.cs.examples.files;

import java.io.*;

Expand All @@ -8,7 +8,7 @@ public static void main(String[] args) throws IOException {
// δεν είναι ασφαλές για UTF-8.
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream("greek.txt"), "UTF-8" // δοκιμάστε "ISO-8859-7" για πείραμα
new FileInputStream("gr/aueb/cs/examples/files/greek.txt"), "UTF-8" // δοκιμάστε "ISO-8859-7" για πείραμα
)
);

Expand All @@ -19,4 +19,4 @@ public static void main(String[] args) throws IOException {

reader.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.files;
package gr.aueb.cs.examples.files;

import java.io.*;

Expand All @@ -7,7 +7,7 @@ public static void main(String[] args) throws IOException {
// Δημιουργία Writer με ρητή κωδικοποίηση UTF-8
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("greek.txt"), "UTF-8"
new FileOutputStream("gr/aueb/cs/examples/files/greek.txt"), "UTF-8"
)
);

Expand All @@ -18,6 +18,6 @@ public static void main(String[] args) throws IOException {
writer.write("Αυτό είναι δοκιμαστικό αρχείο σε UTF-8.");
writer.close();

System.out.println("Το αρχείο greek.txt γράφτηκε με επιτυχία σε UTF-8.");
System.out.println("Το αρχείο gr/aueb/cs/examples/files/greek.txt γράφτηκε με επιτυχία σε UTF-8.");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.files;
package gr.aueb.cs.examples.files;

import org.w3c.dom.*;
import javax.xml.parsers.*;
Expand All @@ -7,7 +7,7 @@
public class LibraryReader {
public static void main(String[] args) throws Exception {
// Βήμα 1: Φόρτωση και parsing του XML
File xmlFile = new File("library.xml");
File xmlFile = new File("gr/aueb/cs/examples/files/library.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.files;
package gr.aueb.cs.examples.files;

public class RepresentationsTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.files;
package gr.aueb.cs.examples.files;

import java.net.URL;
import java.io.BufferedReader;
Expand Down
3 changes: 3 additions & 0 deletions gr/aueb/cs/examples/files/greek.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Καλημέρα
Πώς είσαι;
Αυτό είναι δοκιμαστικό αρχείο σε UTF-8.
10 changes: 10 additions & 0 deletions gr/aueb/cs/examples/files/library.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<library>
<section name="Programming">
<book title="Effective Java" author="Joshua Bloch" year="2018" />
<book title="Clean Code" author="Robert C. Martin" year="2008" />
</section>
<section name="Computer Science">
<book title="Introduction to Algorithms" author="Cormen, Leiserson, Rivest, Stein" year="2022" />
</section>
</library>
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.generics;
package gr.aueb.cs.examples.generics;

public class BoundedTypeParamsTest {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.generics;
package gr.aueb.cs.examples.generics;

public class GenericMethodTest {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.generics;
package gr.aueb.cs.examples.generics;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.generics;
package gr.aueb.cs.examples.generics;

import java.util.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.generics;
package gr.aueb.cs.examples.generics;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.graphics;
package gr.aueb.cs.examples.graphics;

import java.awt.*;
import java.awt.geom.AffineTransform;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.inheritance;
package gr.aueb.cs.examples.inheritance;

class Sup {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed some classes have default visibility while other stay public. Is there a reason for that? Just afraid it might confuse some students.

void who() { System.out.println("who() in Sup"); }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.inheritance;
package gr.aueb.cs.examples.inheritance;

public class Final {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gr.aueb.cs.inheritance;
package gr.aueb.cs.examples.inheritance;

class X {
int a;
Expand Down
Loading