[Solved] CSE 110 Lab 2

$25

File Name: CSE_110__Lab_2.zip
File Size: 131.88 KB

SKU: [Solved] CSE 110 – Lab 2 Category: Tag:
5/5 - (1 vote)

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). Thisincludes 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 whichblock it belongs.Problem DescriptionYou will have to write a Java program that asks the user to enter two strings, firstname andlastname, and concatenates the strings to make a full name. The program will use methods ofclass String like length() and toUpperCase() on the full name and compare strings by equals()method and if-else statements.Sample OutputBelow 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 textabove. If you use print to show prompts, your output might be on the same line. To avoid thisissue, please use println instead of print to show the prompts.Sample Run 1:Please enter first name: magnusPlease enter last name: carlsenFull name (in capitals): MAGNUS CARLSENLength of full name: 14String comparison using == sign does NOT workString comparison using equals method worksSample Run 2:Please enter first name: wesleyPlease enter last name: soFull name (in capitals): WESLEY SOLength of full name: 9String comparison using == sign does NOT workString comparison using equals method worksInstructionStep 1: Getting StartedCreate a class called Lab2. Use the same setup for setting up your class and main method as youdid 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 thefollowing 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 name2. public class Lab2 {3. // we must have a main method to run the program4. public static void main(String[] args) {5. // Your main logic goes here6. }7. }In the examples of the remaining steps, we ignore the signatures of class definition and mainfunction. Please make sure you put code inside the main function all the time (except that thecode location is pointed out).Step 2: Declaring Variables and User InputWhen we examine this programming task, we see that we will need three variables of Stringtype: firstName, lastName and fullName.To store the length of full name, we also need an integer variable nameLength of type int. Forthe user input, we will use Scanner you learnt in Lab1. In total, you should have at least 5variables, which store 3 strings, 1 integer, and 1 Scanner respectively.An example is showed as follows.1. // declare variables of different types2. String firstName = ;3. String lastName = ;4. String fullName = ;5. int nameLength = 0;6. Scanner scan = new Scanner(System.in); // Dont forget to import7.8. // Use Scanner to ask the user for first name9. System.out.println(Please enter first name: );10. firstName = scan.nextLine();11. // Use Scanner to ask the user for last name12. System.out.println(Please enter last name: );13. lastName = scan.nextLine();To use Scanner, dont forget to import Scanner from java.util pacakage. This code snippetshould be on the top of your program and outside class definition.1. // All imports have to be outside class2. import java.util.Scanner;3.4. // class name should match the file name5. public class Lab2 {6. // we must have a main method to run the program7. public static void main(String[] args) {8. // something hereStep 3: Full Name, String ManipulationPart1: ConcatenationNow that we have both first and last name, we need to form the full name from them. Rememberthat string concatenation can be done using + sign between variables. Form fullName byadding firstName to lastName separated by space.1. // Example: (abc + + def); gives you abc def2. // Add firstName to lastName variables using + sign, dont forget the space.3. // store the result in the fullName variable4. // >Part2: Convert to upper caseNow convert fullName to upper case variable. Remember we use toUpperCase() methodin String class to do so.1. // Example: abc.toUpperCase(); gives you ABC2. // Convert fullName variable to upper case and store it back to itself3. // >Part3: Find length of a StringRemember the method length() in String class. It is used to find number of characters in astring variable. Use length() to find the length of fullName and store result innameLength 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 resultsPrint out the fullName and nameLength on screen. Use System.out.println() to dothat. Always look at the Sample Output section (below) to make sure your output look like theexpected output.1. // Print fullName, it should be in upper case2. // >3. // Print nameLength, this should be number of characters4. // in fullName variable, including space5. // >Step 4: String ComparisonFor 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 givecorrect results for all String variables. The better and more accurate way for String comparison isby using equals() method. Follow the code below to see the difference between using bothways of comparison.If you are using the template, the only thing you need to do is put in the print functions andobserve the difference between == and equals().1. // Define two String variables, title1 and title2 using2. // String constructor to initialize them3. String title1 = new String(cse110);4. String title2 = cse110;5.6. // Compare the two strings and print which one of the two ways works7. // follow code below:8. if ( title1 == title2 ) {9. // Print String comparison using == sign works10. // >11. } else {12. // Print String comparison using == sign does NOT work13. // >14. }15. if ( title1.equals(title2) ) {16. // print String comparison using equals method works17. // >18. } else {19. // print String comparison using equals method does NOT work20. // >21. }

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CSE 110 Lab 2
$25