Week 10: Java – Interfaces & Sorting with Comparable/Comparator
Q1. SmartSpeaker – Using Interfaces
Objective: Demonstrate multiple interfaces (WiFiEnabled, BluetoothEnabled) and class implementation.
import java.util.Scanner;
interface WiFiEnabled {
void connectToWiFi(String networkName);
}
interface BluetoothEnabled {
void connectToBluetooth(String deviceName);
}
class SmartSpeaker implements WiFiEnabled, BluetoothEnabled {
public void connectToWiFi(String networkName) {
System.out.println("Connected to WiFi network: " + networkName);
}
public void connectToBluetooth(String deviceName) {
System.out.println("Connected to Bluetooth device: " + deviceName);
}
public void playMusic(String songName) {
System.out.println("Playing song: " + songName);
}
}
public class q1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
SmartSpeaker speaker = new SmartSpeaker();
System.out.print("Enter WiFi Network Name: ");
String wifiNetworkName = sc.nextLine();
System.out.print("Enter Bluetooth Device Name: ");
String bluetoothDeviceName = sc.nextLine();
System.out.print("Enter Song Name: ");
String songName = sc.nextLine();
speaker.connectToWiFi(wifiNetworkName);
speaker.connectToBluetooth(bluetoothDeviceName);
speaker.playMusic(songName);
sc.close();
}
}
Key Points:
SmartSpeakerimplements multiple interfaces.- Interfaces define contracts; the class provides concrete implementations.
- Supports polymorphism, as
SmartSpeakercan be treated asWiFiEnabledorBluetoothEnabled.
Q2. Book Sorting – Using Comparable
import java.util.*;
class Book implements Comparable<Book> {
String title;
String author;
double price;
Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
public int compareTo(Book other) {
return Double.compare(this.price, other.price); // ascending price
}
public String toString() {
return "Title: " + title + ", Author: " + author + ", Price: " + price;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Book> books = new ArrayList<>();
System.out.print("How many books? ");
int n = sc.nextInt();
sc.nextLine();
for (int i = 1; i <= n; i++) {
System.out.println("Book " + i + ":");
System.out.print("Title: ");
String title = sc.nextLine();
System.out.print("Author: ");
String author = sc.nextLine();
System.out.print("Price: ");
double price = sc.nextDouble();
sc.nextLine();
books.add(new Book(title, author, price));
}
Collections.sort(books); // uses compareTo()
System.out.println("\nBooks sorted by price (ascending):");
for (Book b : books) System.out.println(b);
sc.close();
}
}
Key Points:
- Implements
Comparable<Book>to define natural ordering (by price). Collections.sort(list)uses thecompareTo()method.Double.compare()is safer than manual</>checks.
Q3. Employee Sorting – Using Comparator
import java.util.*;
class Employee {
private int id;
private String name;
private int age;
public Employee(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() { return id; }
public String getName() { return name; }
public int getAge() { return age; }
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Age: " + age;
}
}
class NameComparator implements Comparator<Employee> {
public int compare(Employee e1, Employee e2) {
return e1.getName().compareToIgnoreCase(e2.getName());
}
}
class AgeComparator implements Comparator<Employee> {
public int compare(Employee e1, Employee e2) {
return Integer.compare(e1.getAge(), e2.getAge());
}
}
public class q3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Employee> employees = new ArrayList<>();
System.out.print("Enter the number of employees: ");
int n = sc.nextInt();
sc.nextLine();
for (int i = 1; i <= n; i++) {
System.out.println("Employee " + i + ":");
System.out.print("ID: "); int id = sc.nextInt(); sc.nextLine();
System.out.print("Name: "); String name = sc.nextLine();
System.out.print("Age: "); int age = sc.nextInt(); sc.nextLine();
employees.add(new Employee(id, name, age));
}
Collections.sort(employees, new NameComparator());
System.out.println("\nSorting by Name:");
for (Employee e : employees) System.out.println(e);
Collections.sort(employees, new AgeComparator());
System.out.println("\nSorting by Age:");
for (Employee e : employees) System.out.println(e);
sc.close();
}
}
Key Points:
- Implements
Comparator<Employee>to define custom orderings (by name or age). Collections.sort(list, comparator)allows multiple sorting strategies.- Supports dynamic sorting without modifying the
Employeeclass.
Week 10 Concepts Summary
-
Interfaces
-
Define a contract of methods without implementation.
- A class can implement multiple interfaces.
-
Comparable
-
For natural ordering of objects.
- Override
compareTo()in the class. -
Comparator
-
For custom ordering.
- Implement separate classes (or lambda expressions in modern Java).
-
Collections.sort()
-
Works with both
ComparableandComparator. -
Polymorphism & Flexibility
-
Use interfaces or comparators for dynamic behavior.