Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write a code fragment that will use the operator to concatenate two strings f

ID: 3846351 • Letter: 1

Question

1) Write a code fragment that will use the operator to concatenate two strings first name and lastname together with a space between them. The strings are defined as follows: string name first name string first name wJohn string lastname nsmith (Note that the above is one way to initialize strings.) 2) Write a code fragment that will use the operator to append the string username to the string message and store it into the string buffer where the username is first bracketed by the strings and ".The strings are defined as follows: string user James string text Welcome to our Wareagle Chat Group string buffer; After executing your code fragment, message buffer must contain the following string: (*James*) Welcome to our Wareagle Chat Group 3) Write the code fragment that will use the above code in (2 to allow the program to prepend more user name and message into message buffer created in (2) string user2 Kate string text2 Hi there After executing your code fragment, buffer should contain the following string: (*Kate*) Hi there! Paul )Welcome to our Wareagle Chat Group 4) Write the code fragment that will use the above code in (3)to allow the program to In' character between two messages text 3 and text4 and stores them in buffer. string user3 Matt string text 3 War Eagle string text, 4 Hey! After executing your code fragment, buffer should contain the following string: (*Matt War Eagle !InHey (*Kate*) Hi there (*Paul*) Welcome to our Wareagle Chat Group Print out this string in buffer and describe what happens.

Explanation / Answer

(1)

The filename is a.java

compiled by command -> javac a.java

to execute follow command -> java a

Here the program is -

import java.util.*;
import java.io.*;
class a
{
public static void main(String args[])
{
String firstname="John";
String lastname="Smith";
  
String name=firstname+" "+lastname ;// " " is appended to show space
  
System.out.println(name);//printing full name
}
}

(2)

The filename is b.java

compiled by command -> javac b.java

to execute follow command -> java b

Here the program is-

import java.io.*;
import java.util.*;
public class b
{
public static void main(String args[])
{
String user="James";
String text="Welcome to our wareagle chat group!";
  
String buffer="(*"+user+"*)"+text;
  
System.out.println(buffer);
}
}

(3)

The filename is c.java

compiled by command -> javac c.java

to execute follow command -> java c

Here the program is -

import java.util.*;
import java.io.*;
public class c
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a new username ");
String new_user=sc.nextLine();//to read a new string or to add a new user
/*we can do above tast as new_user="Paul" too */
String text="Welcome to our Wareagle Chat Group !";
String user2="Kate";
String text2="Hi there!";
  
String buffer="(*"+user2+"*)"+text2+"(*"+new_user+"*)"+text;
System.out.println(buffer);
}
}

(4)

The filename is d.java

compiled by command -> javac d.java

to execute follow command -> java d

Here the program is -

import java.util.*;
import java.io.*;
public class d
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a new username ");
String new_user=sc.nextLine();//to read a new user
/*we can do above tast as new_user="Paul" too */
String text="Welcome to our Wareagle Chat Group !";
String user2="Kate";
String text2="Hi there!";
String user3="Matt";
String text3="War Eagle";
String text4="Hey!";
/* if we want to print " " then we have to use " " in the process to concate strings */
String buffer="(*"+user3+"*)"+text3+" "+text4+"(*"+user2+"*)"+text2+"(*"+new_user+"*)"+text;
System.out.println(buffer);
}
}