In: Computer Science
A.Write a program that prompts for and reads the user’s first and last name (separately).Then print the initials with the capital letters. (Note that we do not expect that inputs from users are case-sensitive. For example, if the user’s input of the first name is robert and of the last name is SMITH, you should print R.S.)
B.Write a program that creates and prints a random phone number of the form XXX-XXX-XXXX. Include the dashed in the output. Do not let the first three digit contain an 8 or 9 (but don’t be more restrictive than that), and make sure that the second set of three digits is not greater than 655. Hint: Think through the easiest way to construct the phone number. Each digit does not have to be determined separately.
-use java
Answer 1:
import java.util.Scanner;
public class Example1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//reading first name and last name
System.out.println("Enter first name");
String f=sc.next();
System.out.println("Enter last name");
String l=sc.next();
//appending first char from each string and converting
into uppercase
String in =
f.toUpperCase().charAt(0)+"."+l.toUpperCase().charAt(0);
System.out.println(in);
}
}
Answer 2:
import java.util.Random;
public class RandomPhone {
public static void main(String[] args) {
String phone = "";
Random r = new Random();
String firstThree = "";
// generating first three numbers
until it has other 8 and 9
while (true) {
firstThree = ""
+ (r.nextInt(899) + 100);
if
(!firstThree.contains("8") &&
!firstThree.contains("9"))
break;
}
phone = firstThree;
// generating the next 3 numbers
less than 655
phone = phone + "-" +
(r.nextInt(555) + 100);
// generating last 4 digits
phone = phone +"-"+
(r.nextInt(9999) + 1000);
System.out.println(phone);
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me