Participation Activity - Strings Participation Activity: char and String Data Types Objectives • Practice using the char and String data types. • Practice using Control Flow statements Overview The...

1 answer below »
Java Assignment help!


Participation Activity - Strings Participation Activity: char and String Data Types Objectives • Practice using the char and String data types. • Practice using Control Flow statements Overview The char data type is used to store characters defined by the Unicode standard, specifically UTF-16 that defines how the character is represented using 16 bits. A variable of char data type can be initialized in multiple ways as shown below: // Initialize using character literal char c = ‘A’; // Initialize using binary format char c = 0b0000000001000001; // Initialize using hexadecimal format char c = 0x0041; In addition to alphabets and numeric digits, the char data type can store special characters like white space, horizontal tab, newline, etc. These are represented using “escape sequence”. For instance ‘\s’ is the literal value for a white space, ‘\t’ is the literal value for tab, and ‘\n’ is the literal value for newline. Since character like ‘ (single quote), “ (double quote), \ (back slash) are reserved by Java, we would need to “escape” these characters by using a “\” symbol. Below are few examples: // Assign using “escape sequence” for newline character char c = ‘\n’; // Assign using “escape sequence” for “ (double quote) character char c = ‘\”’; // Assign using “escape sequence” for ‘ (single quote) character char c = ‘\’’; // Assign using “escape sequence” for \ (back slash) character char c = ‘\\’ and so on… The String data type is used to store multiple Unicode characters. A String variable can be initialized using assignment operator. Also, a String variable’s value can be updated using different operations, including assignment (=) or concatenation (‘+’) operator. Below are few examples: // Assign an empty string String s = “”; // Assign “cpts” string String s = “cpts”; // Assign using concatenation of characters and strings String s = “a” + ‘\s’ + “b”; Instructions Find a student partner to work on this Participation Activity. You may use Discord to find a student partner. The Participation Activity should still be submitted by each student. Standard (5 pts) 1. Write a Java program Strings.java that contains variables “a", “b", “c" of char data types, and variable “s” of String data type. 2. Assign values to the variables “a”, “b” and “c” as described below: - Assign character literal ’1’ (one) to variable “a” - Assign binary value of character literal ’3’ (three) to variable “b” - Assign hexademical value of character literal ’1’ (one) to variable “c" You would need to look up the binary and hexadecimal values for the literals ‘1’ and ‘3’. You can refer to the class slides or Unicode Standard code table available at: https://www.unicode.org/ charts/PDF/U0000.pdf 3. Concatenate the string literal “Course: CptS_” and the values of the three char variables “a”, “b" and “c” using concatenation (+) operator. Assign the result to variable “s”. Print the value of the String variable “s”. The output should be as shown below: Course: CptS_131 https://www.unicode.org/charts/PDF/U0000.pdf https://www.unicode.org/charts/PDF/U0000.pdf 4. Update the Java program to also print the text shown below using a String variable “t”. You can initialize the String variable with the text containing regular characters and “escape sequence” characters. A "quoted" String is 'much' better if you learn the rules of "escape sequences." Here is a sample output from steps 1-4: Bonus Point (1) 5. Update your Java program to also generate the output shown below using loops. \/ \\// \\\/// Note: Since this is a plus version question, no specific instructions are provided. The complete program (steps 1-5) should generate this output Submission Submit the Java source file String.java for your application. Make sure that it contains the header comment. Course: CptS_131 A "quoted" String is 'much' better if you learn the rules of "escape sequences." Course: CptS_131 A "quoted" String is 'much' better if you learn the rules of "escape sequences.” \/ \\// \\\///
Answered Same DaySep 20, 2021

Answer To: Participation Activity - Strings Participation Activity: char and String Data Types Objectives •...

Arun Shankar answered on Sep 20 2021
143 Votes
public class Strings {
    public static void main(String[] args)
    {
        char a, b, c, d;
        String s;

        
        a = '1';
        b = 0b110011;
        c = 0x31;
        
        s = "Course: CptS_" + a + b + c;
        
        System.out.println(s);
        
        String t = "A \"quoted\" String is\n" +
                "'much' better if you learn\n"...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here