Question

In: Computer Science

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

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.

My issue in this is the loop portion. I am not sure if I have done it properly. I also need help trying to comment this code so I can explain it better. Could someone help analyze my code so that I can properly do this JAVA assignment? Please include comments as well inside the code.

Here is what I have so far:

import java.util.*;

public class BirthdayReminder{

public static void main(String[] args)

{

final int NUM_NAMES = 10;

String sentinal = "ZZZ";

int count = 0;

String name = null;

String birthdate = null;

String[] names = new String[NUM_NAMES];

String[] birthdates = new String[NUM_NAMES];

Scanner input = new Scanner(System.in);

System.out.println("Enter a name or " + sentinal + " to quit > ");

name = input.nextLine();

while(name.compareTo(sentinal)!=0 && count < NUM_NAMES)

{

System.out.println("Enter birthdate (mm/dd) > ");

birthdate = input.nextLine();

names[count] = name.trim();

birthdates[count] = birthdate.trim();

System.out.println("Enter a name or " + sentinal + " to quit > ");

name = input.nextLine();

++ count;

}

System.out.println("\nCount of names is " + count);

System.out.println("\nNames are:" + count);

for(int x = 0; x < count; ++x)

System.out.println(names[x]) ;

boolean repeat = true;

boolean found;

while(repeat)

{

found=false;

System.out.println("\nEnter a name or " + sentinal + " to quit > ");

name = input.nextLine().trim();

if(name.compareTo(sentinal)==0)

repeat = false;

else

{

for(int x = 0; x < count; ++x)

{

if(names[x].compareTo(name)==0)

{

found =true;

birthdate = birthdates[x];

break;

}

}

if(found)

System.out.println("Birthdate of " + name + " is " + birthdate);

else

System.out.println("Birthdate of " + name + " is not found");

}

}

}

}

Solutions

Expert Solution

Here is the solution,

The code is working fine. I have just did small addition in the program where after entering all the names, the user then enters any name and associated birthdate gets displayed. It is possible that the user can enter values in small case letters and the name stored in the array is in capital. So to void this i have added the code where both the names i.e from the array and the user entered name gets converted to lowercase and then the comparison take place.

I have added comments for better understanding.

here is the code:-

// your code starts here
import java.util.*;   
public class BirthdayReminder{
  
public static void main(String[] args)
  
{
  
// variable declarations
// variable NUM_NAMES for MAX entries
final int NUM_NAMES = 10;
// variable sentinal with value 'ZZZ'
String sentinal = "ZZZ";
// variable to count the names
int count = 0;
// name variable to accept name value
String name = null;
// birthday variable to accept birthdate value
String birthdate = null;
// array for stroring names
String[] names = new String[NUM_NAMES];
// array for storing birthdate
String[] birthdates = new String[NUM_NAMES];   
Scanner input = new Scanner(System.in);   
// accepting name
System.out.println("Enter a name or " + sentinal + " to quit > ");
name = input.nextLine();
  
// loop to continue to accept names
// with condition name != 'ZZZ'(to quit) and count<10
// loop breaks if user enter name as 'ZZZ' or count becomes 10
while(name.compareTo(sentinal)!=0 && count < NUM_NAMES)
{
// accepting birthdate
System.out.println("Enter birthdate (mm/dd) > ");
birthdate = input.nextLine();
//storing value of name in names array
names[count] = name.trim();
// storing value of birthdate in birthdates array
birthdates[count] = birthdate.trim();
// accepting name for next record or 'ZZZ' to quit
System.out.println("Enter a name or " + sentinal + " to quit > ");
name = input.nextLine();
// icrementing count variable to count the names
++ count;
}
// diplaying the count of the names
System.out.println("\nCount of names is " + count);
System.out.println("\nNames are:" + count);   
// loop to display names in the array
for(int x = 0; x < count; ++x)
System.out.println(names[x]) ;
// declaring boolean values
// repeat to repeat the while loop till repeat becomes false
boolean repeat = true;
// found variable to be used if the name is found in names array
boolean found;
while(repeat)
{
// assigning false to found (found is used as a flag value)
found=false;
// accepting name
System.out.println("\nEnter a name or " + sentinal + " to quit > ");
name = input.nextLine().trim();
// comparing with 'ZZZ' to quit
if(name.compareTo(sentinal)==0)
repeat = false;
else
{
// if not 'ZZZ' then iterate through the names array to find the name
for(int x = 0; x < count; ++x)
{
// if name found
// converting both the values (from array and from the user) to lowercase
// to avoid uppercase/lowercase problem
if(names[x].toLowerCase().compareTo(name.toLowerCase())==0)
{
// make found = true
found =true;
// get the associated birthdate from birthdates array and break the loop
birthdate = birthdates[x];
break;
}
}
// outside the for loop
// check if found = true or false
if(found)
// if true print the birthdate
System.out.println("Birthdate of " + name + " is " + birthdate);
else
// else print appropriate message "not found"
System.out.println("Birthdate of " + name + " is not found");
}
}
}
}
// code ends here

Here is the sample output:-

here is the screenshot of the code:-

Thank You.


Related Solutions

C++ Vector Write a program that allows the user to enter the last names of the...
C++ Vector Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes. Your program should output the...
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
Write an application that allows a user to enter any number of student quiz scores, as...
Write an application that allows a user to enter any number of student quiz scores, as integers, until the user enters 99. If the score entered is less than 0 or more than 10, display Score must be between 10 and 0 and do not use the score. After all the scores have been entered, display the number of valid scores entered, the highest score, the lowest score, and the arithmetic average.
Create an application that allows the user to enter the total for an order and the...
Create an application that allows the user to enter the total for an order and the name of the customer. If the order is less than 500 dollars, the customer gets no discount. If the order is greater than or equal to 500 and less than 1000 dollars, the customer gets a 5 percent discount. If the order is greater than or equal to 1000 dollars, the customer gets a 10 percent discount. The application should display the name of...
Write a C++ console application that allows your user to enter the total rainfall for each...
Write a C++ console application that allows your user to enter the total rainfall for each of 12 months into an array of doubles. The program should validate user input by guarding against rainfall values that are less than zero. After all 12 entries have been made, the program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts.
Write an application that allows a user to enter any number of student quiz scores until...
Write an application that allows a user to enter any number of student quiz scores until the user enters 99. If the score entered is less than 0 or more than 10, display an appropriate message and do not use the score. After all the scores have been entered, display the number of scores entered, the highest score, the lowest score, and the arithmetic average. Save the file as QuizScoreStatistics.java. I am struggling with looping in Java and do not...
Using Pseudocode: Design a program that allows the user to enter 20 names into a String...
Using Pseudocode: Design a program that allows the user to enter 20 names into a String array. Sort the array in ascending (alphabetical) order and display its contents.
Show: Create an application that allows the user to enter the number of calories and fat...
Show: Create an application that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *9 The percentage of...
Android Studio. Java. Please create an application that -> An activity that allows user to enter...
Android Studio. Java. Please create an application that -> An activity that allows user to enter name, gender, date of birth, state of residence (selected from a pre-defined list: CA, AZ, NV, OR), email address and favorite website. This activity has a button "Show Data" that displays detail entered
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT