1. What will the following display to the console? Work out the solution by hand first (don't run the code, until you have figured it out on paper).
String s1 = "car";
String s2 = "car";
String s3 = "Car";
String s4 = new String("car");
System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.println(s2.equals(s3));
System.out.println(s2.equalsIgnoreCase(s3));
System.out.println(s2 == s4);
System.out.println(s2.equals(s4));
2. What will the following display to the console? Work out the solution by hand (don't run the code, until after you have worked it out by hand).
StringBuilder sb1 = new StringBuilder("Cat");
StringBuilder sb2 = new StringBuilder(sb1.replace(2, 3, "Rat").append("Mat"));
sb2.append("Sat!");
System.out.println(sb1);
System.out.println(sb2);
Activity 17.1: My Name (10 pts)
This activity is adapted from a 36A activity, which you may recognize if you took this course from me.
Open a new Java class named MyName in Eclipse
We will create a program to greet the user by name.
Add a block comment at the top of your program with your names and section information.
/*
* @author
* CIS 36B, Activity 17.1
*/
At the top of the main method, let's declare our first StringBuilder variable - to store the first name of our user:
StringBuilder first_name;
Declare a second StringBuilder variable to store the last name of our user in the same _ style.
Declare a third StringBuilder variable to store the user's full name:
StringBuilder full_name;
Welcome the user with a statement like this one:
System.out.println("Hi! I want to learn your name!\n");
Now, prompt the user to enter their first name.
System.out.print("Please enter your first name: ");
Follow this prompt with a input.next() statement to capture the user input.
first_name = new StringBuilder(input.next());
Now prompt the user to enter his or her last name and store the input in the last_name variable.
We want to assign the full_name variable the value of the user's first and last name combined. Let's do so now using concatenation:
full_name = new StringBuilder(first_name.append(last_name));
Finally, let's greet the user by his or her full name.
System.out.println("Nice to meet you " + full_name + "!");
Run your program to ensure it is giving you the output you expect.
Next, let's calculate the length of the user's first and last names and output the result to the console:
System.out.println("The length of your first name is " + first_name.length() + " letters");
Now add a similar System.out.println statement to display the length of the user's last name AND full name (remember the full_name variable)
Next, let's return the user's initial's to them.
Let's declare a new StringBuilder variable called initials that you place at the top of your program with the other variable declarations.
StringBuilder initials = new StringBuilder();
Next, we are going to use the charAt() method to extract the initials from the user's first and last names.
A person's initials are composed of the first letters of their first and last names.
Therefore, we need the letters at position 0 in the first_name and last_name variables.
initials.append(first_name.charAt(???)); //finish this line to add full initials
Next, print the results to the console to display for the user.
System.out.println("Your initials are: " + initials);
Finally, we will add a title to the name (Mr, Ms or Mx):
Prompt the user to enter the preferred title:
System.out.print("What title do you prefer?\nEnter Mr, Ms or Mx: ");
String title = input.next();
if (title.substring(0,2).equalsIgnoreCase("Mr")) {
full_name.insert(0, "Mr. ");
}
Finally, print a valediction
System.out.println("\nGoodbye, " + full_name + "!");
When you are finished, the output of your program should look like below.
However, look closely and you will see mistakes in my output.
Please correct the mistakes before you turn the program.
Submit your MyName.java program to Canvas.
The output of your program should look like this (except user input will vary):
Hi! I want to learn your name!
Please enter your first name: Jennifer
Please enter your last name: Parrish
Nice to meet you Jennifer Parrish!
The length of your first name is 16 letters
The length of your last name is 7 letters
The length of your full name is 16 letters
Your initials are: JP
What title do you prefer?
Enter Mr, Ms or Mx: Ms
Goodbye, Ms. Jennifer Parrish!
This assignment has been answered 3 times in private sessions.
© 2024 Codify Tutor. All rights reserved