Critical Thinking Exercise Design a complete Java program th
Critical Thinking Exercise: Design a complete Java program that asks the user for two names. Dis- play the two names the user enters-this is called echo printing. Swap the values of the names. For example, name1 will become name2, and name2 will become name1. Display the two names after they have been swapped. Use Lab 2.5, Exercise 16 for reference. After all data has been entered and the names have been swapped, display the values of the names. Be sure to leave blank lines for readability and comments to identify the program author, to describe the program, and to describe program statements. You need the import statement for input and output. Write your design in the following space. Your design should be a list of Java comments with- out any code.
Solution
/* Java Program Example - Swap Two Strings */
import java.util.Scanner;
public class JavaProgram
{
public static void main(String[] input)
{
String str1, str2, strtemp;
Scanner scan = new Scanner(System.in);
System.out.print(\"Enter First String : \");
str1 = scan.nextLine();
System.out.print(\"Enter Second String : \");
str2 = scan.nextLine();
System.out.println(\"\ Strings before Swapping are :\");
System.out.print(\"String 1 = \" +str1+ \"\ \");
System.out.print(\"String 2 = \" +str2+ \"\ \");
strtemp = str1;
str1 = str2;
str2 = strtemp;
System.out.println(\"\ Strings after Swapping are :\");
System.out.print(\"String 1 = \" +str1+ \"\ \");
System.out.print(\"String 2 = \" +str2+ \"\ \");
}
}
