1 / 2100%
// ------------------------------------------------------------
// AUTHOR: Brandon Evans
// FILENAME: Lab2 Manipulating Strings
// SPECIFICATION: Using strings to enter a full name and change it to
uppercase. Find the length of the name. Compare strings using Equals()
method and learn how to use if-else statements.
// FOR: CSE 110- Lab #2
// TIME SPENT: 1hr
//-------------------------------------------------------------
// All imports has to be outside class
import java.util.Scanner;
// class name should match the file name
public class Lab2 {
// we must have a main method to run the program
public static void main(String[] args) {
// declare variables of different types:
Scanner Scan = new Scanner(System.in);
String firstName = "";
String lastName = "";
String fullName = "";
int nameLength = 0;
// Use Scanner to ask the user for first name
System.out.println("Please input first name: ");
firstName = Scan.nextLine();
// Use Scanner to ask the user for last name
System.out.println("Please input last name: ");
lastName = Scan.nextLine();
//Add firstName to lastName variables using "+" sign, don't forget the
space.
// store the result in the fullName variable
//-->
fullName = firstName + " " + lastName;
// Convert fullName variable to upper case
//-->
fullName.toUpperCase();
// Find the length of "fullName and store it
// in "nameLength" variable.
//-->
nameLength = fullName.length();
// Print "fullName", it should be in upper case
//-->
System.out.println("The Full Name ( in CAPS): " +
fullName.toUpperCase());
// Print "nameLength", this should be number of characters
// in "fullName" variable, including space
//-->
System.out.println("Length of the Full Name:" + nameLength);
// Define two String variables, title1 and title2 using
// String constructor to initialize them
String title1 = new String("cse110");
String title2 = "cse110";
// Compare the two strings and print which one of the two ways works
// follow code below:
if (title1 == title2) {
// Print "String comparison using "==" sign works"
//-->
System.out.println("String comparison using \"==\" sign works");
} else {
// Print "String comparison using "==" sign does NOT work"
//-->
System.out.println("String comparison using \"==\" sign does NOT
work");
}
if (title1.equals(title2)) {
// print "String comparison using "equals" method works"
//-->
System.out.println("String comparison using \"equals\" method
works");
} else {
// print "String comparison using "equals" method does NOT work"
//-->
System.out.println("String comparison using \"equals\" method does
NOT work");
}
}
}
Powered by TCPDF (www.tcpdf.org)
Students also viewed