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: