Q1) (30 Points) Using the do while repetition statement, write a Java applicatio
ID: 3911143 • Letter: Q
Question
Q1) (30 Points) Using the do while repetition statement, write a Java application that prints a person's friend list. The program will ask the user whether she/he wants to continue adding more frien(s) to the list or not. The process of adding friend(s) will continue if the user enters "c" and will exit if the user enters "e" following which the friend list will be displayed. (A comma should separate each name). Save your program as Hw3_1.java. For this program, it is important that you understand the concatenation operator " that concatenates (or joins) two strings together. Here is an example source code. You will need to make some modifications to make it work. public static void main(Stringl]args) (//Beginning of main funeton Scanner keyboard- new Scanner( System.in); String friendList-". String friendName String option-* do System.out println"Enter your friend's name" friendName -String.valueOf( keyboard.nextLine0) System.out.printin(Exit? Type e to exit. Otherwise, type c to continue. option- String.valueOf(keyboard.nextLine)) while (Toption.equalsignoreCase"e"): Your code to dsplay the Fiendlis End of main fiunction Here is a sample of the program's behavior Enter your friend name: Quan Do (input) Exit? Type e to exit. Otherwise, type c to continue. c (input) Enter your friend name: Jeff Gillis (input) Exit? Type e to exit. Otherwise, type c to continue. c (input) Enter your friend name: Bobbie Smith (input) Exit? Type e to exit. Otherwise, type c to continue. e (input) Your friend list: Quan Do,Jeff Gillis,Bobbie Smith (outputExplanation / Answer
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String friendList = "";
String friendName = "";
String option = "";
do{
System.out.print("Enter your friend's name: ");
friendName = String.valueOf(keyboard.nextLine());
System.out.print("Exit? Type e to exit. Otherwise, type c to continue. ");
option = String.valueOf(keyboard.nextLine());
// ADDING current name to list
friendList += friendName;
// adding a comma
if(!option.equalsIgnoreCase("e"))
{
friendList += ", ";
}
}while(!option.equalsIgnoreCase("e"));
// printing output
System.out.println(" Your friend list: "+friendList);
}
}
/*SAMPLE OUTPUT
Enter your friend's name: Quan Do
Exit? Type e to exit. Otherwise, type c to continue. c
Enter your friend's name: Jeff Gillis
Exit? Type e to exit. Otherwise, type c to continue. c
Enter your friend's name: Bobbie Smith
Exit? Type e to exit. Otherwise, type c to continue. e
Your friend list: Quan Do, Jeff Gillis, Bobbie Smith
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.