Java Programs Collection &
Interface
Answers for the 10 tasks
Generated by ChatGPT
1. Drawing Behaviour with
Interface
• interface Shape {
void draw();
}
class Circle implements Shape {
public void draw() {
[Link]("Drawing Circle");
}
}
class Rectangle implements Shape {
public void draw() {
[Link]("Drawing Rectangle");
}
}
public class DrawingDemo {
public static void main(String[] args) {
Shape s1 = new Circle();
Shape s2 = new Rectangle();
[Link]();
[Link]();
}
}
2. Appliance Control via Interface
• interface Appliance {
void switchOn();
void switchOff();
}
class Fan implements Appliance {
public void switchOn() { [Link]("Fan is ON"); }
public void switchOff() { [Link]("Fan is OFF"); }
}
class Light implements Appliance {
public void switchOn() { [Link]("Light is ON"); }
public void switchOff() { [Link]("Light is OFF"); }
}
class AC implements Appliance {
public void switchOn() { [Link]("AC is ON"); }
public void switchOff() { [Link]("AC is OFF"); }
}
public class ApplianceControl {
public static void main(String[] args) {
Appliance[] appliances = { new Fan(), new Light(), new AC() };
for (Appliance a : appliances) {
[Link]();
}
for (Appliance a : appliances) {
[Link]();
3. ArrayList with Duplicates
Removal, Sorting
• import [Link].*;
public class ArrayListDemo {
public static void main(String[] args) {
List<Integer> list = new
ArrayList<>([Link](101,105,11,12,10,10,5,5,15,15));
[Link]("Original: " + list);
// Remove duplicates by converting to TreeSet (sorts too)
Set<Integer> uniqueSorted = new TreeSet<>(list);
[Link]("Unique Sorted: " + uniqueSorted);
// If you need list form
List<Integer> result = new ArrayList<>(uniqueSorted);
[Link]("Result List: " + result);
}
}
4. Loose Coupling and Currency List
• import [Link].*;
interface CurrencyStorage {
void addCurrency(String currency);
List<String> getAll();
}
class CurrencyList implements CurrencyStorage {
private List<String> list = new ArrayList<>();
public void addCurrency(String currency) {
[Link](currency);
}
public List<String> getAll() {
return list;
}
}
public class LooseCouplingDemo {
public static void main(String[] args) {
// Array of 10 global currency names
String[] currencies = { "USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF",
"CNY", "INR", "BRL" };
CurrencyStorage storage = new CurrencyList(); // interface reference (loose
coupling)
for (String c : currencies) [Link](c);
[Link]("Currencies in ArrayList via List reference: " +
[Link]());
}
5. LinkedList Fruits Manipulation
• import [Link].*;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<String> fruits = new
LinkedList<>([Link]("Apple","Banana","Cherry","Date","Elderberry"));
[Link]("Original: " + fruits);
[Link]();
[Link]();
[Link]("After removing first and last: " + fruits);
// Add two more at start
[Link]("Fig");
[Link]("Grapes");
[Link]("After adding at start: " + fruits);
}
}
6. Contact List with ArrayList and
LinkedList
• import [Link].*;
public class ContactManager {
public static void operate(List<String> contacts) {
[Link]("Alice");
[Link]("Bob");
[Link]("Charlie");
[Link]([Link]().getSimpleName() + " initial: " +
contacts);
// Search
[Link]("Contains Bob? " + [Link]("Bob"));
// Remove
[Link]("Alice");
[Link]("After removal: " + contacts);
// Display
for (String c : contacts) [Link]("Contact: " + c);
}
public static void main(String[] args) {
operate(new ArrayList<>());
[Link]("-----");
operate(new LinkedList<>());
}
}
7. Unique User IDs with HashSet
• import [Link].*;
public class UniqueUserIDs {
public static void main(String[] args) {
List<String> usernames =
[Link]("john","jane","alice","bob","charlie");
Set<String> userIds = new HashSet<>();
Random rand = new Random();
for (String user : usernames) {
String id;
do {
int num = 100 + [Link](900); // 3-digit random (100-999)
id = user + num;
} while (); // repeat if duplicate
[Link]("Generated ID for " + user + ": " + id);
}
[Link]("All unique IDs: " + userIds);
}
}
8. Country Names with
LinkedHashSet
• import [Link].*;
public class LinkedHashSetDemo {
public static void main(String[] args) {
LinkedHashSet<String> countries = new LinkedHashSet<>();
[Link]("USA");
[Link]("India");
[Link]("Japan");
[Link]("Germany");
[Link]("Brazil");
[Link]("Contains India? " + [Link]("India"));
[Link]("Size: " + [Link]());
[Link]("Iterating:");
for (String c : countries) [Link](c);
[Link]("Japan");
[Link]("After removal: " + countries);
[Link]("Is empty? " + [Link]());
[Link]();
[Link]("After clear, is empty? " + [Link]());
}
}
9. Grocery Items with TreeSet
• import [Link].*;
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<String> groceries = new TreeSet<>();
[Link]("Milk");
[Link]("Eggs");
[Link]("Bread");
[Link]("Apple");
[Link]("Carrot");
[Link]("Grocery List: " + groceries);
[Link]("First: " + [Link]());
[Link]("Last: " + [Link]());
[Link]("Contains Bread? " + [Link]("Bread"));
[Link]("Eggs");
[Link]("After removal: " + groceries);
[Link]();
[Link]("After clear: " + groceries);
}
}
10. Convert to TreeSet for Sorting
& Removing Duplicates
• import [Link].*;
public class ConvertCollections {
public static void main(String[] args) {
ArrayList<String> names = new
ArrayList<>([Link]("Alice","Bob","Alice","David","Bob"));
HashSet<Integer> marks = new HashSet<>([Link](90,85,90,75));
LinkedList<String> cities = new
LinkedList<>([Link]("Paris","London","Paris","Tokyo"));
TreeSet<String> sortedNames = new TreeSet<>(names);
TreeSet<Integer> sortedMarks = new TreeSet<>(marks);
TreeSet<String> sortedCities = new TreeSet<>(cities);
[Link]("Sorted Unique Names: " + sortedNames);
[Link]("Sorted Unique Marks: " + sortedMarks);
[Link]("Sorted Unique Cities: " + sortedCities);
}
}