In: Computer Science
Read this entire document before beginning your lab.
For this lab you are required to fulfill all requirements exactly as described in this provided document, no less, no more.
Problem: Read in a word and display the requested characters from that word in the requested format. Write a Java program that
● prompts the user for a word and reads it,
● converts all characters of the input word to uppercase and display the word with a double quotation mark ( " ) at the start and end of the word,
● displays the word with all characters whose index is odd in lower case and for the rest of the characters displaying a * instead of the character,
● finally displays the original word as it was entered by the user.
Based on the previous specifications your program should behave and look exactly as shown in the cases below. Your program should work for any word entered by the user, not just the ones in the samples.
Below illustrates how your program should behave and appear.
Note that ◦ symbol indicates a space and ↵ is a new line character. All words except for user input (in blue) must be exactly as indicated in the sample. Any extra “spaces” and/or “new lines” will be graded a wrong answer.
Program Sample outputs |
|
ex:1 |
Enter◦a◦word:◦ELEPhant↵ ↵ ELEPhant |
ex:2 |
Enter◦a◦word:◦Nancy_12A↵ ↵ Nancy_12A |
Note 1: The use of libraries other than java.util.Scanner is
prohibited.
Note 2: You may find the use of the following methods useful –
charAt(), toLowerCase(), toUpperCase(), length().
in java
Note 3: Final thought, remember that your solution is case-sensitive and space-sensitive, fulfill the above instructions carefully and precisely.
Code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word:
");
String a=sc.nextLine();
int n=a.length();
String u=a.toUpperCase();
u="\""+u+"\"";
String l=a.toLowerCase();
String s="";
for(int i=0;i<n;i++)
{
char x=l.charAt(i);
if(i%2==0)
s=s+"*";
else
s=s+x;
}
System.out.print("\n"+u+"\n"+s+"\n"+a);
}
}
Please refer to the screenshot of the code to understand the indentation of the code:
Outputs:
1.
2.
For any doubts or questions comment below.