Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an application that allows a user to enter the names and birthdates of up

ID: 3768648 • Letter: W

Question

Write an application that allows a user to enter the names and birthdates of up to 10 friends. Continue to prompt the user for names and birthdates until the user enters the sentinel value "ZZZ" for a name or has entered 10 names, whichever comes first. When the user is finished entering names, produce a count of how many names were entered, and then display the names. In a loop, continuously ask the user to type one of the names and display the corresponding birthdate or an error message if the name has not been previously entered. The loop continues until the user enters "ZZZ" for a name. Save the application as BirthdayReminder.java.

Explanation / Answer

import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class BirthdayRemainder {

   String birthDay, name;

   public BirthdayRemainder() {
       // TODO Auto-generated constructor stub
   }

   /**
   * @param birthDay
   * @param name
   */
   public BirthdayRemainder(String birthDay, String name) {
       this.birthDay = birthDay;
       this.name = name;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "BirthdayRemainder [birthDay=" + birthDay + ", name=" + name
               + "]";
   }

   /**
   * @return the birthDay
   */
   public String getBirthDay() {
       return birthDay;
   }

   /**
   * @param birthDay
   * the birthDay to set
   */
   public void setBirthDay(String birthDay) {
       this.birthDay = birthDay;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   public static void main(String[] args) {
       Scanner scanner;
       try {

           scanner = new Scanner(System.in);
           BirthdayRemainder birthdayRemainder[] = new BirthdayRemainder[10];
           int i;
           for (i = 0; i < 10; i++) {
               System.out.print("Enter the Name:");
               String name = scanner.nextLine();
               if (name.equalsIgnoreCase("ZZZ")) {
                   break;

               }

               System.out.print("Enter the Birthday:");
               String birthDay = scanner.nextLine();
               birthdayRemainder[i] = new BirthdayRemainder(birthDay, name);

           }

           System.out.println("No of names entered:" + i);

           for (int j = 0; j < i; j++) {
               System.out.println("Name " + j + ": "
                       + birthdayRemainder[j].getName());

           }
           do {
               System.out.print("Enter the name to get Birthday:");
               String searchName = scanner.nextLine();
               if (searchName.equalsIgnoreCase("ZZZ")) {
                   break;

               }
               boolean searchFlag = false;
               for (int j = 0; j < i; j++) {

                   if (birthdayRemainder[j].getName().equalsIgnoreCase(
                           searchName)) {
                       System.out.println("Birthday " + ": "
                               + birthdayRemainder[j].getBirthDay());
                       searchFlag = true;
                       break;
                   }

               }
               if (!searchFlag) {
                   System.out.println("Name Not found!");
               }

           } while (true);
       } catch (Exception e) {
           // TODO: handle exception
       }
   }

}

OUTPUT:

Enter the Name:srinu
Enter the Birthday:10/12/2011
Enter the Name:vasu
Enter the Birthday:11/12/2010
Enter the Name:akki
Enter the Birthday:11/1/2000
Enter the Name:zzz
No of names entered:3
Name 0: srinu
Name 1: vasu
Name 2: akki
Enter the name to get Birthday:rajesh
Name Not found!
Enter the name to get Birthday:akki
Birthday : 11/1/2000
Enter the name to get Birthday:vasu
Birthday : 11/12/2010
Enter the name to get Birthday:zzz

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote