Question
Jan Villaroel
Topic: Computer Science Posted 1 year ago
Write a program that includes expressions and statements for: a. Declare a string variable and prompt the user to get your own name and initial the string variable.

Using Java:

Write a program that includes expressions and statements for:
a. Declare a string variable and prompt the user to get your own name and initial the string variable.
b. Get a substring consisting of the first letter. Print it out and check the result.
c. Get a substring consisting of the first and last letter.
d. Get a substring consisting of all but the first and last letter.
e. Get the first half of the string (discarding the middle letter if the string has an odd length).
f. Get the second half of the string (discarding the middle letter if the string has an odd length)
g. Get the substring consisting of the middle letter if the string has an odd length, or the middle two letters otherwise.

Note: For b-g, print out the variables and check the result.


Answers
What are best answers and total votes?
No Votes
Vote
SignIn to Vote
Koala Tutor
Sep 22, 2023
Koala Ai Support

Here is the Java program that includes the expressions and statements for the given requirements:

```java
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// Declare a string variable and prompt the user to input their name
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();

// Get a substring consisting of the first letter and print it
String firstLetter = name.substring(0, 1);
System.out.println("First letter: " + firstLetter);

// Get a substring consisting of the first and last letter and print it
int length = name.length();
String firstAndLastLetter = name.substring(0, 1) + name.substring(length - 1);
System.out.println("First and last letter: " + firstAndLastLetter);

// Get a substring consisting of all but the first and last letter and print it
String allButFirstAndLast = name.substring(1, length - 1);
System.out.println("All but first and last letter: " + allButFirstAndLast);

// Get the first half of the string (discarding the middle letter if the length is odd)
int halfLength = length / 2;
String firstHalf = name.substring(0, halfLength);
System.out.println("First half: " + firstHalf);

// Get the second half of the string (discarding the middle letter if the length is odd)
String secondHalf = name.substring(halfLength);
System.out.println("Second half: " + secondHalf);

// Get the substring consisting of the middle letter(s) based on the length of the string
String middleSubstring;
if (length % 2 == 0) {
// Even length, get middle two letters
int middleIndex = length / 2 - 1;
middleSubstring = name.substring(middleIndex, middleIndex + 2);
} else {
// Odd length, get middle letter
int middleIndex = length / 2;
middleSubstring = name.substring(middleIndex, middleIndex + 1);
}
System.out.println("Middle substring: " + middleSubstring);

scanner.close();
}
}
```

This program prompts the user to enter their name, and then performs the desired operations using string manipulation techniques. The results are printed out for each operation.

Koala Tutor Got a question with my answer?
Message Me