Write an application that prompts the user for three first names and concatenate
ID: 3844063 • Letter: W
Question
Write an application that prompts the user for three first names and concatenates them in every possible two-name combination so that new parents can easily compare them to find the most pleasing baby name. Save the file as Baby NameComparison.java. a. Create a program that contains a String that holds your favorite movie quote and display the total number of spaces contained in the String. Save the file as CountMovieSpaces.java. b. Write an application that counts the total number of spaces contained in a movie quote entered by the user. Save the file as CountMovieSpaces2.java.Explanation / Answer
import java.util.Scanner;
public class BabyNameComparison {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = null;
try {
// prepare scanner reading data from keyboard
scanner = new Scanner(System.in);
// names array declaration with size 3
String[] firstNames = new String[3];
// prompt to enter the three name
System.out.println("Please enter first three baby names:");
for (int i = 0; i < 3; i++)
firstNames[i] = scanner.nextLine();
// print posibilities
System.out
.println("Every posible two-name from three baby first names:");
System.out.println(firstNames[0] + " " + firstNames[1]);
System.out.println(firstNames[1] + " " + firstNames[2]);
System.out.println(firstNames[0] + " " + firstNames[2]);
} catch (Exception e) {
// TODO: handle exception
}
}
}
OUTPUT:
Please enter first three baby names:
Akshaya
Latha
Madhavi
Every posible two-name from three baby first names:
Akshaya Latha
Latha Madhavi
Akshaya Madhavi
public class CountMovieSpaces {
/**
* @param args
*/
public static void main(String[] args) {
String movieQuote = "They say that the best blaze burns brightest when circumstances are at their worst.";
/* declare an integer and initialize to zero to count spaces. */
int spaceCount = 0;
/* Iterate over the movieQuote string to find spaces. */
for (int i = 0; i < movieQuote.length(); i++) {
if (movieQuote.charAt(i) == ' ')
// increment the count of spaceCOUNT by one
spaceCount++;
}
// print movie quote
System.out.println("Movie Quote: " + movieQuote);
// print spaces count
System.out.println("Number of spaces in quote: " + spaceCount);
}
}
OUTPUT:
Movie Quote: They say that the best blaze burns brightest when circumstances are at their worst.
Number of spaces in quote: 13
import java.util.Scanner;
public class CountMovieSpaces2 {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
// prompt to enter the movie quote
System.out.println("Enter the movie quote:");
String movieQuote = scanner.nextLine();
/* declare an integer and initialize to zero to count spaces. */
int spaceCount = 0;
/* Iterate over the movieQuote string to find spaces. */
for (int i = 0; i < movieQuote.length(); i++) {
if (movieQuote.charAt(i) == ' ')
// increment the count of spaceCOUNT by one
spaceCount++;
}
// print movie quote
System.out.println("Movie Quote: " + movieQuote);
// print spaces count
System.out.println("Number of spaces in quote: " + spaceCount);
} catch (Exception e) {
// TODO: handle exception
}
}
}
OUTPUT:
Enter the movie quote:
They say that the best blaze burns brightest when circumstances are at their worst.
Movie Quote: They say that the best blaze burns brightest when circumstances are at their worst.
Number of spaces in quote: 13
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.