1. Engineering
  2. Computer Science
  3. cse110...

Question: cse110...

Question details
cse110

A) Fri Window Help asuedu 10-16223 CSE 110 -Lab 2 Lab Topic: Strings . Working on String cls and some of is methods Familianiaation wih hasie data types e Using the Scanser Clas Printing outpat . Rnd the comect way of string conmparisons . Gerting familiw with Control St ate ments (1, else) Coding Guidelines When dec laring a variable, you usualily want to ininialine i Remember you cannot initialue a namber with a dring riuble names Uhe labs or spaces to indent code within blocks (code sarrounded by braces) This includes c lasses, methods, and oode asoci ated with ifs, switches and oops Use white space to muke your progrars more readahle Us comments ahr the ming bne of clases, nethode and blackstoidmify to which · block it belongs MacBook Air
AS Engineerig 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 lengthO and toUpperCase0 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 print in instead of print to show the prompts Sample Run 1 please tnt@r firธt name : magnus Flease enter last name: carlser Ful nane tin capitals): MAGNUS CARLSEN Length of full name : 14 String comparison using aign does NOT WOEK String comparison usinq equala method WoR Sample Run 2 Please enter Last namc 8 Pull nane (in eapitala: WEALEY Length of full name a string comparison uaingsign dois NOT MoD 9 tri ing comparison us ing qualat nethod works
ht s.asu.edu ASL Engineering Instruction Create a class called Lab2. Use the same setup for setting up your class nd main method as you did in previous labs and assignments. Be sure to name your file Lab2java. At the beginning of each programming assignment you must have a comment block with the ollowing infomation: I AUTHOR: <Please put your name here> FILENAME: Lab2.java 11 SPECIFICATION: <Describe your progran> I FOR: CSE 110 Lab 2 11 TIME SPENT: <Estinate time to complete this work> Your code should also have the class definition and one main function as follows: ll class nane should natch the file public class Lab2 t ue aust have a saln sethed to nn the progra public static vald ain(Stringt] args) ( 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 tine (except that the code location is poinled out) When we examine this programming task, we see that we will need thee variables of Strine type: first Name, Jast Name and ful1Name To store the length of full name, we also need an inte ger variable rae Lengt h of type int For the user input, we will use Scanner you learnt in Labl. In total, you should have at keast5 variables, which store 3 strings 1 indeger, anud I Scanner respectivdly An example is showed as follows String firstNae
a myasucourses.asu.edu cswebdav/pid- S Engin String lasthane Scanner scan new Scanner(System. in) / Dont forget to inport il Use Scanner to ask the user fer fiest sne System.out.printin( Please enter first nane: ) . firstNane scan.next Line . W Use Scanner to ask the user for last nane 12. System.out.print in( Please enter last name) 13 lastane scan.nextline() To use Scanner, don t forget to import Seanner from java.util pacakage. This code shippet should be on the top of your program and outside class definition. r All imports have to be outslde class inport java.util.Scanner W class name showld eatch the file nane public class Laba i we must have a sain sethed to rus the progras pubiic static vold saln(Stringt) angs) something here ulation Parti: Concatenation Now that we have both first and lasl name, we need to form the full name from them. Remenber that tring concalenation can be done using sign bet ween variables. Form tu l1tame by adding firstHahe to las t Nane separated by space. rr Md firstsane to lastNm variaeles ui.g-+ sign. 4on.t terget the space. f store the result in the ful INane var Lable Part2 Convert to upper case Now convent ful 1 Name to upper case variable Remember we use touppercase D methoul in String class to do so. Part3: Find length of a String Remember the method lwingtht) in Sring class is wesl to find mimiter or characters in a ruig vanable. Uselernthnk, find ile lengdi ofnilitate andsliye rewill in-
ASl Enginooring Il/ Examplet helle Length() gives you an Integer 5 I/ Find the Jength of fulLNane and store it as namLength varlable Part4: Display results Print out the full Name and name Length on screen. Use System, out printin ( to do that. Always look at the Sample Output section (below) to make sure your output look like the expected output t I Print fulisaner it should be In upper case Print aamelength, ths shoutd be nniber of characters nfullNane varlable, Lec lading space 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 t) method Follow the code below to see the difference between using both If you are using the template, the only thing you need to do is put in the print functions and observe the difference between ss and squalso Sering tielet new Stringcksele) sering title2-eseuri · 1f ( title! , title2 ) { if ( tirlei , equals(title2》
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution