-1

I am taking an intro to Java class and one of the assignments we have is to create a OOP file to an already existing driver program (provided by the instructor).

I'm stumped on one of the methods that requires us to combine 2 objects(a & b) that have been created in the driver program and to return it as a new object (c).

The assignment ask us to create a method to do the following:

reproduce – This method takes another ComputerMicrobe as a parameter and creates a new ComputerMicrobe. This new ComputerMicrobe will have a name that is the concatenation of the name of “this” ComputerMicrobe with the name of the other ComputerMicrobe. The dNACode of the new ComputerMicrobe will take half of the dNACode of “this” ComputerMicrobe concatenated with half of the dNACode of the other ComputerMicrobe. The method will return the newly created ComputerMicrobe."

invadedBy – This method takes another ComputerMicrobe as a parameter. The dNACode of the current object (this) will be complete replaced by the dNACode of the ComputerMicrobe from the parameter. The method will return the current object (return this).

Those two methods seem to be somewhat similar, to where if I can get one to work I think that I can modify the code for the other method, however im lost on how to even call the methods from the driver for those 2 objects.

I have tried various methods that our text book shows with public, void and booleans, however I always get various errors.

Here is my code so far

public class ComputerMicrobe
{
private String name; // ComputerMicrobe's name
private String dNACode; // DNA for ComputerMicrobe
private String mutate; // string for random mathfunction to replace ltr with "X"
private String reverse; // string for function to reverse dNACode
private String reproduce; // string for function to combine a.dNACode + A.name + a.dNACode + A.name
private String invadedBy; //

// Constructor Methods
public ComputerMicrobe (String newName, String newDNACode){
    this.setName(newName);
    this.setDNACode(newDNACode);
}

// Default Constructor Methods
public ComputerMicrobe (){this("NN", "NN");}


// Accessor Methods

public String getName() {
    return this.name;
} // retuns the name of object

public String getDNACode() {
    return this.dNACode;
} // retuns the dNACode of the object

public String getMutate() {
    return this.mutate;
} // returns the mutation of the object

public String getReverse() {
    return this.reverse;
} // returns the object in reverse

public String getReproduce() {
    return this.reproduce;
} // returns the object after its been combined with a + b

public String getInvadedBy() {
    return this.invadedBy;
} // returns 
// ****************end Accessor Method************

// Mutator Methods
public void setName(String newName) {
    this.name = newName;

} // method to change the name
public void setDNACode(String newDNACode) {
    this.dNACode = newDNACode;  
} // method to change the dNACode

// ****************end Mutator Method************


// Method to mutate the dna 

public ComputerMicrobe mutate() 
{
    int length; // determines the length of the dna
    int r; // random position to be defined by math random
    length = dNACode.length();  // sets the max number to generate to based on length of dNACode
    r = (int)(Math.random() * length); // random number

    char[] dNACodeChars = dNACode.toCharArray(); // creates array to change the char 
    dNACodeChars[r] = 'X'; // replacing the char of the random number 
    dNACode = String.valueOf(dNACodeChars);

    return this;    
}
// ****************end mutate method************


// Method to reverse the dna.

public ComputerMicrobe reverse()
{
    int i;
    int length; // determines the length of the dna
    String r = "";

    length = dNACode.length();

    for (i = length - 1; i >= 0; i--)
    {
        r = r + dNACode.charAt(i);

    }
    this.dNACode = r;
    // dNACode = r;
    return this;
}
// ****************end reverse method************

// Method to reproduce  the name and dna.

public String reproduce (ComputerMicrobe otherComputerMicrobe)
{
    String aNew;        
    aNew = otherComputerMicrobe.dNACode + otherComputerMicrobe.name;
    //otherComputerMicrobe.name = this.name;


    this.name = aNew;

    //return this.getDNACode() + this.getName() + this.getDNACode(OtherComputerMicrobe()) + this.getName(OtherComputerMicrobe()); 

    return this;    
}



// ****************end reproduce method************

// Method to invadedBy the name and dna.

public void invadedBy(ComputerMicrobe otherComputerMicrobe)
    {
    String temp;
    temp = otherComputerMicrobe.name;
    otherComputerMicrobe.name = this.name;
    this.name = temp;

    //return this;  
}
// ****************end method************


// Method to obtain a String with the object's status .

public String toString()  
{
return "[" + this.getDNACode() + "] " + this.getName();
} // end toString

} // end class ComputerMicrobe

Here is the provided driver program by the instructor (we have been told that we cannot change or add code to this). Some lines are commented out as I have been going down the assignment step by step.

import java.util.Scanner;

public class Week7Prog
{
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");
    a.reverse();
    b.reverse();

    System.out.println(a);
    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

Any help on how to do this would be greatly appreciated, ive been scouring the google & the Oracle java library but no luck so far.

oreoswin
  • 3
  • 1
  • The provided post you have linked me to, seems to be more geared to the classcastException. Is that the same process to retrieve information in a class/driver? – oreoswin Dec 06 '15 at 22:23

1 Answers1

-1

First off, your reproduce method returns a String which can't be assigned to a ComputerMicrobe, you should change it so that it returns a ComputerMicrobe:

public ComputerMicrobe reproduce (ComputerMicrobe otherComputerMicrobe)
{
    //Concatenated the the names of the two ComputerMicrobes
    String name = this.name + otherComputerMicrobe.getName();

    //Creates a DNACode consisting of 2 halves of the DNACode of the
    //two ComputerMicrobes
    String dnaCode = this.dNACode.substring(0, this.dNACode.length() / 2);
    dnaCode += otherComputerMicrobe.getDNACode().substring(dnaCode.length() - 1, otherComputerMicrobe.getDNACode().length();

    //Creates and returns the new ComputerMicrobe
    return new ComputerMicrobe(name, dnaCode);
}

Then the following line should work:

c = a.reproduce(b);

You might need to change the code to create the dnaCode. I assumed it wants the first half of the first ComputerMicrobe and the second half of the second ComputerMicrobe, however that might not be the case and is up to you to change it so that it is correct.

Shadow
  • 3,926
  • 5
  • 20
  • 41