2 PARTS: A) Write an application that allows a user to enter the names and birth
ID: 3807984 • Letter: 2
Question
2 PARTS:
A)
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 asBirthdayReminder.java.
B)
Create the BirthdayReminderTest class with the main method and include an object that uses the BirthdayReminder class
Explanation / Answer
FileName: BirthdayReminderTest.java
Compile: javac BirthdayReminderTest.java
Execute: java BirthdayReminderTest
Program:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class BirthdayReminderTest
{
public static void main(String []args)
{
Scanner in = new Scanner(System.in);
ArrayList BRObjectList = new ArrayList<BirthdayReminder>();
//BRObjectList.add(new BirthdayReminder("Krunal","28-Sept"));
for(int i=0; i<10 ; i++)
{
System.out.println("Enter a Name:");
String name = in.nextLine();
if(name.equals("ZZZ"))
{
System.out.println("Exit..");
break;
}
System.out.println("Enter a Birthday:");
String birthday = in.nextLine();
BirthdayReminder BRObj = new BirthdayReminder(name,birthday);
BRObjectList.add(new BirthdayReminder(name,birthday));
}
System.out.println("Entered Names...");
Iterator<BirthdayReminder> birthdayIterator = BRObjectList.iterator();
while (birthdayIterator.hasNext()) {
System.out.println(birthdayIterator.next().Name);
}
System.out.println("Now guess:");
while(true)
{
System.out.println("Enter Name to find the Birthday");
String GuessedName = in.nextLine();
Iterator<BirthdayReminder> birthdayIterator1 = BRObjectList.iterator();
while (birthdayIterator1.hasNext()) {
BirthdayReminder element = birthdayIterator1.next();
if(GuessedName.equals(element.Name))
{
System.out.println("Birthday is:" + element.Birthday);
}
else if(GuessedName.equals("ZZZ"))
{
System.out.println("Exit:");
exit();
}
else
{
System.out.println("No Name Found:");
}
}
}
}
}
class BirthdayReminder
{
public String Name;
public String Birthday;
public BirthdayReminder(String name, String birthday)
{
this.Name = name;
this.Birthday = birthday;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.