In: Computer Science
1. Write a program that keeps asking the user for a password until they correctly name it. Once they correctly enter the password, the program congratulates the user and tells them how many guesses it took. Be sure to be grammatically correct with the guess/guesses output. Call the program LastNamePassword. Example (user input in italics) What is the password? monkeys Incorrect. Guess again. dishwasher Incorrect. Guess again. aardvark Correct! You got the password, and it took you 3 guesses to get it correct. 2. Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K 3. Write a program where the user enters a string, and the program outputs a string where for every char in the original, there are two chars except if the character is the number 2. If that happens, do not duplicate the 2. Call your program LastNameDoubles. Input Output The → TThhee AAbb → AAAAbbbb Hi-There → HHii—Tthheerree 1234 → 1123344
1.
import java.util.*;
class LastNamePassword
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s1="aardvark";
int count=0;
System.out.println("What is the password?");
while (true)
{
String s=new String();
s=sc.nextLine();
if(s.equals(s1))
{
count+=1;
System.out.println("Correct ! you got the password,and it took you
"+count+" guesses to get it correct.");
break;
}
else
{
System.out.println("Incorrect . Guess again.");
count+=1;
}
}
}
}
2.
import java.util.*;
class LastNameUpDown
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=new String();
s=sc.nextLine();
for(int i=0;i<s.length();i++)
{
for(int j=0;j<=i;j++)
{
System.out.print(s.charAt(j));
}
System.out.println();
}
for(int i=0;i<s.length()-1;i++)
{
for(int j=1;j<s.length()-i;j++)
{
System.out.print(s.charAt(j-1));
}
System.out.println();
}
}
}
OUTPUT :
K
Ke
Kea
Kean
Kea
Ke
K