0% found this document useful (0 votes)
2 views1 page

Les 6

The document contains Java OOP exercises that include the implementation of various classes such as Book, Student, Car, Product, and BankAccount. Each exercise provides starter code to demonstrate object-oriented programming concepts like class creation, methods, and data handling. The exercises aim to enhance understanding of Java programming through practical examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Les 6

The document contains Java OOP exercises that include the implementation of various classes such as Book, Student, Car, Product, and BankAccount. Each exercise provides starter code to demonstrate object-oriented programming concepts like class creation, methods, and data handling. The exercises aim to enhance understanding of Java programming through practical examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Java OOP Exercises

- Starter Code
Exercise 1: Book Class

public class Main {


public static void main(String[] a
Book b1 = new Book("The Hobbit
Book b2 = new Book("1984", "Ge
System.out.println(b1.getBookI
System.out.println(b2.getBookI
}
}

Exercise 2: Student List

import java.util.*;

class Student {
String name;
int age;
double grade;

Student(String name, int age, doub


this.name = name;
this.age = age;
this.grade = grade;
}

boolean isPassing() {
return grade >= 5.5;
}
}

public class Main {


public static void main(String[] a
ArrayList<Student> students =
students.add(new Student("Alic
students.add(new Student("Bob"

for (Student s : students) {


System.out.println(s.name
}
}
}

Exercise 3: Car Class

class Car {
String brand;
int year;
int mileage;

Car(String brand, int year, int mi


this.brand = brand;
this.year = year;
this.mileage = mileage;
}

void drive(int km) {


mileage += km;
}

String getInfo() {
return brand + " (" + year + "
}
}

public class Main {


public static void main(String[] a
Car car1 = new Car("Toyota", 2
car1.drive(300);
System.out.println(car1.getInf
}
}

Exercise 4: Product List

import java.util.*;

class Product {
String name;
double price;

Product(String name, double price)


this.name = name;
this.price = price;
}

public String toString() {


return name + " - $" + price;
}
}

public class Main {


public static void main(String[] a
ArrayList<Product> products =
products.add(new Product("Appl
products.add(new Product("Milk

double total = 0;
for (Product p : products) {
System.out.println(p);
total += p.price;
}
System.out.println("Average pr
}
}

Exercise 5: Bank Account

class BankAccount {
String accountHolder;
double balance;

BankAccount(String accountHolder,
this.accountHolder = accountHo
this.balance = balance;
}

void deposit(double amount) {


balance += amount;
}

void withdraw(double amount) {


if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insuff
}
}

double getBalance() {
return balance;
}
}

public class Main {


public static void main(String[] a
BankAccount acc = new BankAcco
acc.deposit(50);
acc.withdraw(30);
System.out.println("Balance: $
}
}

You might also like