0

A portion of my assignment dictates that I must create a method in a class that reverses the input string. The instructor has written the driver, and I have to complete the class. I have just started and have been stuck on how to reverse a string in the class. He as also dictated to use "return this" which I also had trouble with, so I used "return c"

Assignment:

reverse – This method will take no parameters. It will reverse the String for the dNACode/ For example if the dNACode is “ABC”, after this method the dNACode is “CBA”. The method will return the current object (return this).

Here is my Class:

public class ComputerMicrobe
{

    private String name;  // Instance variable for name of the Computer Microbe
    private String dNACode; // Instance variable to hold the number of Computer Microbe
    private String reverse;

    // Constructor Methods

    public ComputerMicrobe (String newName, String newDNACode){

        this.setName(newName);
        this.setDNACode(newDNACode);

    } // End Constructor Methods

    //Accessor Methods

    public String getName(){return this.name;}

    public String getDNACode(){return this.dNACode;}

    public String getReverse(){return this.reverse;}

    // Mutator Methods

    public void setName(String newName) {this.name = newName;}

    public void setDNACode(String newDNACode) {this.dNACode = newDNACode;}

    public String reverse(){

        int i;
        int n;
        String c = null;

        n = dNACode.length();

        for (i = n - 1; i >= 0; i--){

            c = c + dNACode.charAt(i);

        } // End for loop

        return c;

    } // End reverse

    } // End reverse


} // End Class

Here is the Driver (written by instructor):

import java.util.Scanner;

public class CluffAaronWeek7Prog

{

    public static void main (String[] args)

        {
              Scanner stdIn = new Scanner(System.in);
              String name;    //Auxiliar ComputerMicrobe name
              String dNACode;   //Auxiliar ComputerMicrobe DNA Code
              ComputerMicrobe a, b, c; // ComputerMicrobe objects

              System.out.println("Enter name of first ComputerMicrobe");
              name = stdIn.next();
              System.out.println("Enter DNA Code for first ComputerMicrobe");
              dNACode = stdIn.next();
              a = new ComputerMicrobe(name, dNACode);

              System.out.println("Enter name of second ComputerMicrobe");
              name = stdIn.next();
              System.out.println("Enter DNA Code for second ComputerMicrobe");
              dNACode = stdIn.next();
              b = new ComputerMicrobe(name, dNACode);

              System.out.println("Initial set of ComputerMicrobes");
              System.out.println(a);
              System.out.println(b);

              //System.out.println("ComputerMicrobe a after mutation");
              //a.mutate();
              //System.out.println(a);

              System.out.println("ComputerMicrobe b after reverse");
              b.reverse();
              System.out.println(b);

              //System.out.println("ComputerMicrobe c after reproduction of a and b");
              //c = a.reproduce(b);
              //System.out.println(c);

              //System.out.println("ComputerMicrobe c after mutation and reverse");
              //c.mutate().reverse();
              //System.out.println(c);

              //System.out.println("ComputerMicrobe b after invasion of reverse a");
              //b.invadedBy(a.reverse());
              //System.out.println(b);
    } // end main
} // end class

I am testing one section at a time, so that is why most of the lines are marked as comments.

I get this as the output when I run the driver:

enter image description here

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
SgtC
  • 3
  • 3
  • 1
    What exactly is your question? – Chris Stillwell Dec 03 '15 at 21:52
  • Not sure if you can do this for your assignment but a pretty easy way to reverse a string is to use `StringBuilder`. `String reverseString = new StringBuilder(forwardString).reverse().toString();` If not, you can always iterate through the `String` backwards. – gonzo Dec 03 '15 at 21:55
  • Sorry... what are the lines that I am getting that show the @ symbol? Those are not the desired outcome. – SgtC Dec 03 '15 at 22:19
  • I am still getting this error is what I get when I change from null to "": java.lang.stringindexoutofboundsexception string index out of range 1. Anyone? – SgtC Dec 04 '15 at 00:40

1 Answers1

0

What you you are seeing with the @ is the String representation of the ComputerMicrobe instance. When an object is passed into System.out.println() the default toString() method is called. So if you override the toString() method in your ComputerMicrobe class so that it returns the dnaCode variable this will be output when the object is printed.

Here's a link about the toString() method which should help you understand what it does and how to use it.

Regarding return this, that's all you need to do instead of return c. What this does is return the current instance of the class. So if you call b.reverse() that will return b.

So after these changes your reverse() method should look something like this:

public ComputerMicrobe reverse() {
  int i;
  int n;
  String c = "";

  n = dNACode.length();

  for (i = n - 1; i >= 0; i--){
    c = c + dNACode.charAt(i);
  }

  dNACode = c;
  return this;
}

The reason for these changes is that when you return this you are returning a ComputerMicrobe object and not a String, so the return type of the method has to change. Also since c is not being returned it has to be put somewhere that it can be accessed by the rest of the program, so that goes into the dNACode member variable which will be output in the toString() method. And finally c was initialised as null, which is not a String, so concatenating a char onto that will not work as expected. It should be initialised as an empty String. Hope this all makes sense, happy to explain further or provide some links for anything you are not sure about.

Here are a couple to get you started:

This one is another SO question about concatenating null strings. And this one is about the this keyword.

Community
  • 1
  • 1
ewanc
  • 1,284
  • 1
  • 12
  • 23
  • Thanks for the link! The data is displaying properly now, but the numbers are not reversing. I tried to change "return c" to "return this" and I got this error: C:\Users\The Cluffs\Desktop\Java Homework\ComputerMicrobe.java:59: error: incompatible types return this; ^ required: String found: ComputerMicrobe 1 error Tool completed with exit code 1 – SgtC Dec 03 '15 at 22:59
  • ok, so the incompatible types error, you need to change the return type of reverse() from String to ComputerMicrobe. The reason the string is not reversing is because you are storing the reversed version in the variable c which is then not used so it is discarded. At the end of the reverse() method you need to set dnaCode equal to c. Let me know if anything isn't clear and I can explain it in more detail. – ewanc Dec 03 '15 at 23:05
  • Changing the return type fixed the return error. But making dNACode = c; at the end of my method returned this: ComputerMicrobe b after reverse: [null8lun] Soft. I used the numbers 5678 for the dNACode string. I'm not sure where the null8ln is coming from – SgtC Dec 03 '15 at 23:30
  • Great, did dnaCode = c work for the reversing problem? – ewanc Dec 03 '15 at 23:32
  • No it didn't, see my last comment for a description – SgtC Dec 03 '15 at 23:33
  • Sorry, missed that one. Try initialising c to an empty string instead of null, ie "" – ewanc Dec 03 '15 at 23:35
  • this error is what I get when I change from null to "": java.lang.stringindexoutofboundsexception string index out of range 1 – SgtC Dec 03 '15 at 23:54
  • ah ok, I think I know what might be the problem. I will update my answer with a code example for the reverse method. – ewanc Dec 04 '15 at 09:11
  • Perfect! Your code worked perfectly. My problem was I had dNACode = c; inside the loop. You're a life saver! – SgtC Dec 04 '15 at 16:25
  • Excellent, I thought that might have been the problem. Good luck with the rest of the assignment! – ewanc Dec 04 '15 at 17:06