Question

In: Computer Science

First, launch NetBeans and close any previous projects that may be open (at the top menu...

First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).

Then create a new Java application called "BackwardsStrings" (without the quotation marks) that:

  1. Prompts the user at the command line for one 3-character string.
  2. Then (after the user inputs that first 3-character string) prompts the user for another 3-character string.
  3. Then prints out the two input strings with a space between them.
  4. Finally prints on a separate line the two input strings 'in reverse' (see example below) with a space between them.

So, for example, if the first string is 'usr' and the second string is 'bin', your program would output something like the following:

The two strings you entered are: usr bin.
The two strings in reverse are: nib rsu.

Note that the reversed SECOND string comes FIRST when printing the strings in reverse.

This program must be completed without using loops and without using StringBuilder.

See Horstmann pp. 62-63 for some ideas. Make sure your program includes the command line prompts for the user and that it formats the output appropriately.

Be sure both input strings are three characters in length. If this data validation isn't passed, output "Invalid string length for one or both inputs." and do not proceed with further processing of the strings.

Solutions

Expert Solution

import java.util.Scanner;

public class BackwardsStrings {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter 3 character string");
       String str1=sc.nextLine();
       System.out.println("Enter next 3 character string");
       String str2=sc.nextLine();
       if(str1.length()== str2.length() && str1.length()==3){
           System.out.println("The two strings you entered are:"+str1+" "+str2);
           System.out.println("The two strings in reverse are:"+getReverse(str2,str2.length()-1)+" "+
           getReverse(str1, str1.length()-1));
       }
       else{
           System.out.println("Invalid string length for one or both inputs.");
       }
   }
   public static String getReverse(String s, int pos){
       String rev="";
       if(pos==0){
       return String.valueOf(s.charAt(pos));
       }
   rev=s.charAt(pos)+getReverse(s, pos-1);
   return rev;
   }
     
}

Expected output:


Related Solutions

First, launch NetBeans and close any previous projects that may be open (at the top menu...
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects). Then create a new Java application called "AverageWithMethods" (without the quotation marks) according to the following guidelines. The program prompts the user for five to ten numbers, all on one line, and separated by spaces. Then the user calculates the average of those numbers, and displays the numbers and their average to the user. The program uses...
First, launch NetBeans and close any previous projects that may be open (at the top menu...
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects). Then create a new Java application called "AtmSimulator" (without the quotation marks) (not ATMSimluator!) that simulates a simple one-transaction ATM according to the following guidelines. The program should start with an initial account balance, which you can set to any legitimate double value. All output of currency values should include a leading dollar sign and use two...
First, launch NetBeans and close any previous projects that may be open (at the top menu...
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects). Then create a new Java application called "PasswordChecker" (without the quotation marks) that gets a String of a single word from the user at the command line and checks whether the String, called inputPassword, conforms to the following password policy. The password must: Be exactly three characters in length Include at least one uppercase character Include at...
In java, follow the methods in bold First, launch NetBeans and close any previous projects that...
In java, follow the methods in bold First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects). Then create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below. The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know...
What are the advantages of asking open-ended questions? Are there any advantages to asking close-ended questions?...
What are the advantages of asking open-ended questions? Are there any advantages to asking close-ended questions? What about disadvantages for each question type? Why would we use surveys to collect self-report or victimization data? What makes them the best choice for this type of data collection? Why should we be aware of bias in questionnaire items? What can we do to reduce bias in our questions?
i need a javafx code 1. first create menu bar with option open and save 2....
i need a javafx code 1. first create menu bar with option open and save 2. when user click on open it opens the file only image file 3. when user click on save it saves as new file.
Ms. Jenny Joy is planning to open her first own business project: a little café close...
Ms. Jenny Joy is planning to open her first own business project: a little café close to the university district of the imaginary town of Brightside. She has rented a small, but nice venue for the café. She has worked hard to keep the target opening date of 1 June. There is a very important report missing from her paperwork though; she does not know how much profit she can expect during the first 3 months of operation. She remembers...
Ms. Jenny Joy is planning to open her first own business project: a little café close...
Ms. Jenny Joy is planning to open her first own business project: a little café close to the university district of the imaginary town of Brightside. She has rented a small, but nice venue for the café. She has worked hard to keep the target opening date of 1 June. There is a very important report missing from her paperwork though; she does not know how much profit she can expect during the first 3 months of operation. She remembers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT