CSE 110 – Lab 2 Solved

30.00 $

Category:

Description

Lab Topic: Strings
● Working on “String” class and some of its methods.
● Familiarization with basic data types.
● Using the Scanner Class
● Printing output
● Find the correct way of string comparisons.
● Getting familiar with Control Statements (If, else)
Coding Guidelines
● When declaring a variable, you usually want to initialize it.
● Remember you cannot initialize a number with a string.
● Remember variable names are case sensitive.
● Use tabs or spaces to indent code within blocks (code surrounded by braces). This
includes classes, methods, and code associated with ifs, switches and loops.
● Use white space to make your program more readable.
● Use comments after the ending brace of classes, methods, and blocks to identify to which
block it belongs.
Problem Description
You will have to write a Java program that asks the user to enter two strings, firstname and
lastname, and concatenates the strings to make a full name. The program will use methods of
class “String” like length() and toUpperCase() on the full name and compare strings by equals()
method and if-else statements.
Sample Output
Below is an example of what your output should roughly look like when this lab is completed.
The red words are user inputs.
Note: When the program runs in submission system, you will NOT see any input like the red text
above. If you use print to show prompts, your output might be on the same line. To avoid this
issue, please use println instead of print to show the prompts.
Sample Run 1:
Please enter first name: magnus
Please enter last name: carlsen
Full name (in capitals): MAGNUS CARLSEN
Length of full name: 14
String comparison using “==” sign does NOT work
String comparison using “equals” method works
Sample Run 2:
Please enter first name: wesley
Please enter last name: so
Full name (in capitals): WESLEY SO
Length of full name: 9
String comparison using “==” sign does NOT work
String comparison using “equals” method works
Instruction
Step 1: Getting Started
Create a class called Lab2. Use the same setup for setting up your class and main method as you
did in previous labs and assignments. Be sure to name your file Lab2.java.
At the beginning of each programming assignment you must have a comment block with the
following information:
/*—————————-——————————
// AUTHOR: <Please put your name here>
// FILENAME: Lab2.java
// SPECIFICATION: <Describe your program>
// FOR: CSE 110 – Lab #2
// TIME SPENT: <Estimate time to complete this work>
//—————————-——————————-*/
Your code should also have the class definition and one main function as follows:
1. // class name should match the file name
2. public class Lab2 {
3. // we must have a main method to run the program
4. public static void main(String[] args) {
5. // Your main logic goes here
6. }
7. }
In the examples of the remaining steps, we ignore the signatures of class definition and main
function. Please make sure you put code inside the main function all the time (except that the
code location is pointed out).
Step 2: Declaring Variables and User Input
When we examine this programming task, we see that we will need three variables of String
type: firstName, lastName and fullName.
To store the length of full name, we also need an integer variable nameLength of type int. For
the user input, we will use Scanner you learnt in Lab1. In total, you should have at least 5
variables, which store 3 strings, 1 integer, and 1 Scanner respectively.
An example is showed as follows.
1. // declare variables of different types
2. String firstName = “”;
3. String lastName = “”;
4. String fullName = “”;
5. int nameLength = 0;
6. Scanner scan = new Scanner(System.in); // Don’t forget to import
7.
8. // Use Scanner to ask the user for first name
9. System.out.println(“Please enter first name: “);
10. firstName = scan.nextLine();
11. // Use Scanner to ask the user for last name
12. System.out.println(“Please enter last name: “);
13. lastName = scan.nextLine();
To use Scanner, don’t forget to import Scanner from java.util pacakage. This code snippet
should be on the top of your program and outside class definition.
1. // All imports have to be outside class
2. import java.util.Scanner;
3.
4. // class name should match the file name
5. public class Lab2 {
6. // we must have a main method to run the program
7. public static void main(String[] args) {
8. // something here…
Step 3: Full Name, String Manipulation
Part1: Concatenation
Now that we have both first and last name, we need to form the full name from them. Remember
that string concatenation can be done using ‘+’ sign between variables. Form fullName by
adding firstName to lastName separated by space.
1. // Example: (“abc” + ” ” + “def”); gives you “abc def”
2. // Add firstName to lastName variables using “+” sign, don’t forget the space.
3. // store the result in the “fullName” variable
4. // –>
Part2: Convert to upper case
Now convert fullName to upper case variable. Remember we use toUpperCase() method
in String class to do so.
1. // Example: “abc”.toUpperCase(); gives you “ABC”
2. // Convert “fullName” variable to upper case and store it back to itself
3. // –>
Part3: Find length of a String
Remember the method length() in String class. It is used to find number of characters in a
string variable. Use length() to find the length of fullName and store result in
nameLength variable.
1. // Example: “hello”.length(); gives you an integer 5.
2. // Find the length of “fullName” and store it as “nameLength” variable.
3. // –>
Part4: Display results
Print out the fullName and nameLength on screen. Use System.out.println() to do
that. Always look at the Sample Output section (below) to make sure your output look like the
expected output.
1. // Print “fullName”, it should be in upper case
2. // –>
3. // Print “nameLength”, this should be number of characters
4. // in “fullName” variable, including space
5. // –>
Step 4: String Comparison
For String data types; you can compare two variables to check if both hold the same value or not.
There is a fast way to do that using “==” sign. However, this method does not guarantee to give
correct results for all String variables. The better and more accurate way for String comparison is
by using equals() method. Follow the code below to see the difference between using both
ways of comparison.
If you are using the template, the only thing you need to do is put in the print functions and
observe the difference between “==” and “equals()”.
1. // Define two String variables, title1 and title2 using
2. // String constructor to initialize them
3. String title1 = new String(“cse110”);
4. String title2 = “cse110”;
5.
6. // Compare the two strings and print which one of the two ways works
7. // follow code below:
8. if ( title1 == title2 ) {
9. // Print “String comparison using “==” sign works”
10. // –>
11. } else {
12. // Print “String comparison using “==” sign does NOT work”
13. // –>
14. }
15. if ( title1.equals(title2) ) {
16. // print “String comparison using “equals” method works”
17. // –>
18. } else {
19. // print “String comparison using “equals” method does NOT work”
20. // –>
21. }