Create a program with class name C1Phone: 1. Promt the user for a 10 digit phone
ID: 3621642 • Letter: C
Question
Create a program with class name C1Phone:1. Promt the user for a 10 digit phone number with no puctuation, end your prompt with a ?
for example for the user to enter the phone number (562) 938-4900 they would enter 5629384900
2. Input this number into a long variable named phone. ( you will need to use nextLong() to do the input instead of nextInt() )
create three variables to hold the three parts of the phone number of the appropriate type:
part1
part2
part3
You can create other variables of type int or long as needed to solve the problem.
Use the math operators + - / and % only to then set part1 to the first three digits of phone (area).
set part2 to the next three digits of phone (the first three digits of the local number)
and set part 3 to the last four digits of phone.
Do not use String variables (this is an exercise of extracting numbers with the arithmetic operators only.
Finally print the phone number out with the following print statement:
System.out.println( "("+part1+") "+part2+"-"+part3);
When you are done, your program should work for any 10 digit phone number.
you can extract a number from the middle of a bigger number by using / to shift a part to the right and % to extract the ending digits.
Explanation / Answer
this is probably not the best way to go about this, but it works just fine and is fairly simple to understand import java.util.Scanner; public class C1Phone { public static void main(String[] args) { Scanner in = new Scanner(System.in); long phone = 0; System.out.print("What is your phone number? "); phone = in.nextLong(); long part1 = phone/10000000; phone = phone - part1*10000000; long part2 = phone/10000; phone = phone - part2*10000; long part3 = phone; System.out.print("("+part1+")"+part2+"-"+part3); } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.