Wednesday, 5 February 2025

Write application to accept 3 friend’s information like name, address, Date of birth, cell-phone number in array of object. Display accepted details.

import java.util.Scanner;


class Friend {

    String name;

    String address;

    String dob;

    String phone;


    void displayDetails() {

        System.out.println("\nName: " + name);

        System.out.println("Address: " + address);

        System.out.println("Date of Birth: " + dob);

        System.out.println("Cell-phone: " + phone);

    }

}


public class FriendsInfo1 {

    public static void main(String[] args) {

       // final int SIZE = 3; 

        Friend[] friends = new Friend[3];


        Scanner scanner = new Scanner(System.in);


        //System.out.println("Enter details of " + SIZE + " friends:");

        for (int i = 0; i < 3; i++) {

            friends[i] = new Friend(); // Initialize each Friend object


            System.out.print("Enter Name: ");

            friends[i].name = scanner.nextLine();

            System.out.print("Enter Address: ");

            friends[i].address = scanner.nextLine();

            System.out.print("Enter Date of Birth (DD/MM/YYYY): ");

            friends[i].dob = scanner.nextLine();

            System.out.print("Enter Cell-phone Number: ");

            friends[i].phone = scanner.nextLine();

        }


        System.out.println("\nDisplaying Friends' Information:");

        for (int i = 0; i < 3; i++) {

            System.out.println("\nFriend " + (i + 1) + " Details:");

            friends[i].displayDetails();

        }


        scanner.close(); // Close scanner at the end

    }

}