GTU OOP Program - 14
14). 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; class Loan { double amount; Loan(double amount) { this.amount = amount; } public String toString() { return "Loan with Amount " + this.amount; } } class Date { public String toString() { return "Date: " + java.time.LocalDateTime.now(); } } class Circle { double radius; Circle(double r) { this.radius = r; } public String toString() { return "circle with radius " + this.radius; } } class Program_14 { public static void main(String[] args) { ArrayList<Object> arr_list = new ArrayList<Object>(); arr_list.add(new Loan(1000)); arr_list.add(new Date()); arr_list.add(new String("Kanu Thakor")); arr_list.add(new Circle(5)); for (int i = 0; i < arr_list.size(); i++) { System.out.println((arr_list.get(i)).toString()); } } }
Output
Comments