Assignment 15
Due Thursday, November 15 at 9:20am on Canvas

Assignment 15.1: Comparing Characters (10 pts)

  • Write a program that tests if the first or last letter of a word is earlier in the alphabet.
  • If the characters are the same, we will note that as well.
  • Recall that we may compare String characters to each other because they are stored as numbers in the computer, according to a standard known as Unicode.
  • Name the project FirstLast with a class called FirstLast.java and include all your code in this single file.
  • Ask the user for the following inputs (and no other input) in this order and each on their own line:
    1. a word such as "easy" (without the quote marks) as a String
    2. "y" or "n" (without the quote marks) to see if the program should loop again
    • Assume the user enters only valid data.
    • See sample output below
  • Use the substring method that we learned in Lesson 14 to extract the first and last characters, and then test if the first or last letter of the word is earlier in the alphabet.
    • Do NOT use charAt() or you will not receive credit for this assignment. You must use substring() to access the first and last characters.
  • Note the use of "" around the letter in the output below. These must be included.
  • If the letters are the same then print, "Letters are the same." (without the quotes) as shown in the sample output.
  • Add a while statement that allows the user to repeat the program by inputting a "y" (without the quotes), and exiting the loop for any other character entered, as shown in the sample output.
  • When your program works identically to the example output below, submit it to Canvas:

Your output should look identical to the output below:

***Comparing First and Last Characters***

Input a word: easy
First letter "e" is earlier in the alphabet.

Run again? (y/n) y

Input a word: zebra
Last letter "a" is earlier in the alphabet.

Run again? (y/n) y

Input a word: Zebra
First letter "Z" is earlier in the alphabet.

Run again? (y/n) y

Input a word: eve
Letters are the same.

Run again? (y/n) n



Assignment 15.2: More Sentences (10 pts)
  • Write a program that allows a user to enter a sentence and then the position of two characters in the sentence.
  • The program should then report whether the two characters are identical or different.
  • When the two characters are identical, the program should display the message:
<char> and <char> are identical!
  • Note that <char> should be replaced with the characters from the String. See example output below for more information.
  • When the two characters are different, the program should display the message:
<char> and <char> are unique characters.
  • Note that <char> should be replaced with the characters from the String. See example output below for more information.
  • Name your program Sentences.java
  • Your program must use a do-while loop to allow the user to enter a series of sentences, or "X" to exit.
    • The program should accept both lower and upper case "X" as an indication the user wishes to exit the program.
    • Please see example output below
  • It should also verify that the user provides numerical input for the positions of the characters in the String.
    • Hint: Use a while loop and !input.nextInt() to check for input mismatch exception.
    • See Lesson 15 notes for a discussion on input mismatch exception
    • You will need two while loops, one for each input.nextInt() statement for the numeric location of each character in the String
  • Important: Note that you will need to use input.nextLine() to account for the fact that the loop causes an input.nextInt() to come before an input.nextLine().
    • See lesson 15 notes regarding the problem that can occur when you use input.nextInt() before an input.nextLine()
  • When your program runs identically to the example output below, submit it to Canvas.

Your output should look identical to the following, except user input will vary:

Welcome!
This program compares two letters in a sentence.

Enter a sentence or X to exit: Let's have cake and ice cream for dinner!
Enter the numeric location of the first letter: 1
Enter the numeric location of the second letter: 9

e and e are identical.

Enter a sentence or X to exit: "Flight Behavior" is a good book.
Enter the numeric location of the first letter: six

Error! Enter a number, not text!
Enter the numeric location of the first character: six

Error! Enter a number, not text!
Enter the numeric location of the first character: six

Error! Enter a number, not text!
Enter the numeric location of the first character: 6
Enter the numeric location of the second letter: 10

t and h are unique characters.

Enter a sentence or X to exit: Summer vacation is almost here!
Enter the numeric location of the first letter: 2
Enter the numeric location of the second letter: three

Error! Enter a number, not text!
Enter the numeric location of the second character: three

Error! Enter a number, not text!
Enter the numeric location of the second character: three

Error! Enter a number, not text!
Enter the numeric location of the second character: 3

m and m are identical.

Enter a sentence or X to exit: x
Goodbye!