-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractical34.java
More file actions
49 lines (39 loc) · 1.35 KB
/
practical34.java
File metadata and controls
49 lines (39 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Write a program that creates an Array List and adds a Loan object , a Date object , a string, and a Circle object to the list, and use a loop to display all elements in the list by invoking the object’s to String() method.
import java.util.ArrayList;
import java.util.Date;
class Loan {
double amount;
public Loan(double amount) {
this.amount = amount;
}
@Override
public String toString() {
return "Loan object [amount=" + amount + "]";
}
}
class Circle {
double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public String toString() {
return "Circle object [radius=" + radius + "]";
}
}
public class practical34 {
public static void main(String[] args) {
// Create an ArrayList of type Object to hold different types of objects
ArrayList<Object> list = new ArrayList<>();
// Add various objects to the list
list.add(new Loan(5000.0));
list.add(new Date());
list.add("Hello, I am a String!");
list.add(new Circle(3.5));
// Use a loop to display all elements by invoking their toString() method
System.out.println("Elements in the ArrayList:");
for (Object obj : list) {
System.out.println("- " + obj.toString());
}
}
}