Question
Java Programming Assignment: An Introduction to Java Classes Implement a simple command line application that prompts the user for a name, and prints it in
Java Programming Assignment: An Introduction to Java Classes
Implement a simple command line application that prompts the user for a name, and prints it in Bond, James Bond form. Your application class should be called Bond.java.
Requirements -
As you probably know, in most James Bond movies he introduces himself at some point as Bond, James Bond. Your program should turn any name entered by the user into a string in this form. For example:
Enter a name: Jane Doe
Doe, Jane Doe
Note that the last space in the string entered should be the one used to break the name up. For example:
Enter a name: John D. Smith
Smith, John D. Smith
Note that this should work for names with any number of strings. For example, if someone enters Anakin Darth Vader Skywalker, your program should print Skywalker, Anakin Darth Vader Skywalker.
Finally, if the name entered contains no spaces, the output should be in the format name, just name. For example:
Enter a name: Yoda
Yoda, just Yoda
Creating an Application -
The first step is to create an application in NetBeans. To do this, do the following:
Create a new project called Bond. This should automatically create a package called bond, and a class called Bond.java within that package. If that does not happen, you will need to create them yourself.
The Bond.java file should automatically be created as an application, with a main method (which is where you should put your code). If not, you will need to add the following code: public static void main (String[ ] args) { your code will go here } You will need to add the following code after the package statement to include the Scanner class:
import java.util.*;
Getting Command Line Input -
The first thing your program must do is to prompt the user for a name. This can be done at the command line by:
(1) Printing the prompt Enter a name.
You are to use System.out.print for this to keep the prompt on the same line as the user input. This is always an important part of good user interface design, so dont forget to do this!
(2) Constructing a Scanner object.
Note that you will need to import java.util.* to have access to the Scanner class, as mentioned above.
(3) Use the nextLine method of that Scanner object to get the string entered by the user. This will return a String.
Implementation Approaches -
There are actually many different ways to implement the rest of this program in Java. Whichever way you choose, you will probably need to look at the Java API to find some of the String class methods you will need for your approach. The Java API can be found at https://docs.oracle.com/javase/7/docs/api/
(1) Array-Based Approach -
You might consider solving this problem by converting the String that nextLine returned into an array of characters.
You could then search the array (probably from the end of the array) to find the index of the last space character in the array, and then print the characters before and after that index to create the required output.
If you take this approach, you will need to first convert the String entered by the user into an equivalent array of characters. Hint: There is a method in the String class that returns an array of characters equivalent to the string.
(2) String-Based Approach -
You can also solve this problem using nothing but methods in the String class.
If you take this approach, you will need to use some of the built-in methods in the String class to search for the index of a given character, and then use other methods to find substrings of that string based on that index.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started