Write a complete, stand alone program in a single class called Names, which work
ID: 3621885 • Letter: W
Question
Write a complete, stand alone program in a single class called Names, which works this way.Include in your program this array of names:
String[] names = {"Jo","Jim","Dana","Luke","Lana","Ned","Sy","Ted","Bill","Joe"};
Your program should read in a non-negative int value - say n - from the keyboard, and then print one of the names from the array using the value of n as the index into the array. If n is 0, for example, your program should print Jo; if n is 1, print Jim, if n is 2, print Dana, and so forth. The name you print out is determined by the array index you enter. If n is larger than 9, then the name you print is the one determined by the last digit of n. Thus if n is 911 (or 12341) print Jim -- the name with index 1; if n is 1002 print Dana - the name with index 2. In general, use the mod operator "%" to figure the last digit, and then select that name from the list. (Thus 911%10 = 1, 12345%10 = 5, and so forth).
A final requirement: your solution must make explicit use of the names array (in other words, don't solve the problem by using 10 if statements).
If your code were perfect (we don't expect this for a timed exam), it would be possible to enter you solution into an IDE such as DrJava, and have the code compile. Then you would be able to run the resulting compiled code.
Explanation / Answer
Hope this helps. Let me know if you have any questions. Please rate. :) import java.util.*; public class Names { public static void main(String[] args) { String[] names = {"Jo","Jim","Dana","Luke","Lana","Ned","Sy","Ted","Bill","Joe"}; Scanner s = new Scanner(System.in); System.out.print("Enter a number: "); int n = s.nextInt(); n = n%10; System.out.println(names[n]); } }
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.