JOOPs Through Java

Apply it

Mini Projects

Each project gives you a goal, the concepts it uses, clear steps and starter code. Build them in your own Java editor.

Student Report Card

easy

Store a student's marks in three subjects and print the total, average and grade.

Class and objectFieldsMethodsConditions

Steps

  1. 1Create a class Student with fields name, m1, m2, m3.
  2. 2Add a constructor that sets all four values.
  3. 3Add a method total() that returns the sum of the three marks.
  4. 4Add a method average() that returns total() / 3.0.
  5. 5Add a method grade() that returns A, B, C or Fail using if-else on the average.
  6. 6In main, create one Student and print a neat report card.
Starter code
class Student {
    String name;
    int m1, m2, m3;

    Student(String name, int m1, int m2, int m3) {
        // TODO: use this.
    }

    int total() { return 0; }
    double average() { return 0; }
    String grade() { return ""; }
}

public class Main {
    public static void main(String[] args) {
        Student s = new Student("Asha", 78, 85, 91);
        // TODO: print name, total, average, grade
    }
}

Simple Bank Account

practice

Build a safe bank account where the balance can only change through deposit and withdraw.

Encapsulationprivate fieldsValidationGetters

Steps

  1. 1Create class Account with a private double balance and a String holder.
  2. 2Add deposit(double amount) that ignores amounts of zero or less.
  3. 3Add withdraw(double amount) that refuses if the amount is more than the balance.
  4. 4Add getBalance() so the outside world can read but not overwrite the balance.
  5. 5In main, do a few deposits and withdrawals and print the balance after each one.
Starter code
class Account {
    private String holder;
    private double balance;

    Account(String holder, double opening) {
        // TODO
    }

    public void deposit(double amount) { }
    public void withdraw(double amount) { }
    public double getBalance() { return 0; }
}

public class Main {
    public static void main(String[] args) {
        Account a = new Account("Ravi", 1000);
        a.deposit(500);
        a.withdraw(2000); // should be refused
        System.out.println("Balance = " + a.getBalance());
    }
}

Shape Area Calculator

practice

Use one parent type to calculate the area of different shapes.

Abstract classInheritanceOverridingPolymorphism

Steps

  1. 1Create an abstract class Shape with an abstract method area().
  2. 2Create Circle, Rectangle and Triangle that extend Shape.
  3. 3Override area() in each with the right formula.
  4. 4Put all three objects in a Shape[] array.
  5. 5Loop over the array and print each area — one loop, many behaviours.
Starter code
abstract class Shape {
    abstract double area();
}

class Circle extends Shape {
    double r;
    Circle(double r) { this.r = r; }
    // TODO: override area()
}

public class Main {
    public static void main(String[] args) {
        Shape[] shapes = { new Circle(3) };
        for (Shape s : shapes) {
            System.out.println(s.area());
        }
    }
}

Library Management (mini)

challenge

Track books, issue them to students and handle mistakes without crashing.

ClassesInterfacesArrayListException handling

Steps

  1. 1Create a class Book with id, title and a boolean issued.
  2. 2Create an interface Manageable with add(Book b) and issue(int id).
  3. 3Create Library implements Manageable, storing books in an ArrayList<Book>.
  4. 4issue(int id) should throw an exception when the id does not exist or the book is already issued.
  5. 5In main, wrap the calls in try-catch and print friendly messages.
  6. 6Print the full list of books with their status at the end.
Starter code
import java.util.ArrayList;

class Book {
    int id; String title; boolean issued;
    Book(int id, String title) { this.id = id; this.title = title; }
}

interface Manageable {
    void add(Book b);
    void issue(int id);
}

class Library implements Manageable {
    private ArrayList<Book> books = new ArrayList<>();
    public void add(Book b) { }
    public void issue(int id) { }
}

public class Main {
    public static void main(String[] args) {
        Library lib = new Library();
        lib.add(new Book(1, "Java Basics"));
        try {
            lib.issue(1);
            lib.issue(1);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}