Home > front end >  Assignment of Code Inputs and Partial Outputs
Assignment of Code Inputs and Partial Outputs

Time:02-08

I need help with this coding assignment I have.

Assignment:

¨User will input two words

The longer word will output in reverse starting with the last letter until the middle letter

The shorter word will output in order starting at the middle until the end; then output at the beginning until (but not including) the middle

-in the scenario of same length, the shorter word will be designated with the first input

Example

"radio" and "computer" should produce

"radio" would be "diora"

"computer" would be "retu"¨

^thats the assignment.

the thing I'm trying to figure is what exactly to put in the if and else statements.

my code:

System.out.println("Input word #1:  ");
String w1 = scan.nextLine();
    
System.out.println("Input word #2:  ");
String w2 = scan.nextLine();
    
int len1 = w1.length();
int len2 = w2.length();
        
int location1 =  w1.indexOf( w1 );
int half1 = len1/ 2;
     
int location2 =  w2.indexOf( w2 );
int half2 = len2/ 2;
        
if(len1 > len2){
    System.out.println(len1);
}
else{
}

Please help me.

CodePudding user response:

I suggest taking a step back and thinking about how to solve the problem by describing the steps in plain English (or any other written language if English isn't your native language). For example, if I were trying to solve this problem, you might break it down into these three steps:

  1. Get two words from the user
  2. Print out the longest word in reverse
  3. Print out the shortest word from the middle out

Next, add more details as needed. For example, to do step 2, we first need to determine which word is longest. Similarly, for step 3, we need to find determine which word is shortest. So now our steps are:

  1. Get two words from the user
  2. Determine which word is the longest
  3. Determine which word is the shortest
  4. Print out the longest word in reverse
  5. Print out the shortest word from the middle out

Continue like this by adding more and more detail for each step until it is detailed enough that you can write code from the verbal description directly.

CodePudding user response:

First determine what you need: the longer and shorter word. This already simplifies the problem:

    System.out.println("Input word #1:");
    String w1 = scan.nextLine();
    System.out.println("Input word #2:");
    String w2 = scan.nextLine();
    
    String longerWord;
    String shorterWord;
    if (w1.length() <= w2.length()) { // < or <= ?
       shorterWord = w1;
       longerWord = w2;
    } else {
       shorterWord = w2;
       longerWord = w1;
    }

For the longer word you must pick the letters in the reverse order:

    int longerMiddle = longerWord.length() / 2; // Also the middle letter.
    ...
    for (int i = longerWord.length() - 1; i >= longerMiddle; --i) {
        ... letter ...

It would spoil the fun helping you more.

  •  Tags:  
  • Related