2015-12-06 17 views
-1

我正在介紹Java類,我們的任務之一是創建一個OOP文件到已有的驅動程序(由講師提供)。從驅動程序中組合類中的對象?

我很困難的方法之一,需要我們結合2個對象(一個&乙)已在驅動程序中創建並返回它作爲一個新的對象(三)。

分配要求我們創建一個方法來做到以下幾點:

重現 - 此方法需要另一個ComputerMicrobe作爲參數,並創建一個新的ComputerMicrobe。這個新的ComputerMicrobe將具有一個名稱,即「this」ComputerMicrobe的名稱與另一個ComputerMicrobe的名稱的並置。新的ComputerMicrobe的dNACode將佔用「this」ComputerMicrobe的dNACode的一半,與另一個ComputerMicrobe的dNACode的一半連接。該方法將返回新創建的ComputerMicrobe。「

invadedBy - 此方法將另一個ComputerMicrobe作爲參數,當前對象的此dNACode將完全由參數中的ComputerMicrobe的dNACode替換。將返回當前對象(返回這個)

這兩種方法似乎有點類似,如果我能得到一個工作,我認爲我可以修改另一種方法的代碼,但我失去了如何甚至可以從驅動程序調用這兩個對象的方法

我已經嘗試過各種方法,我們的教科書用public,void和booleans顯示,但是我方式得到各種錯誤。

這裏是我到目前爲止的代碼

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 

這裏是由導師提供的驅動程序(我們被告知,我們不能改變或添加代碼到這一點)。一些行被註釋掉,因爲我一直在一步一步地分配任務。

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 

如何做到這一點任何幫助,將不勝感激,我一直都淘到谷歌&甲骨文的Java庫,但到目前爲止沒有運氣。

+0

您提供的帖子與我聯繫起來,似乎更貼近classcastException。這是相同的過程來檢索類/驅動程序中的信息? – oreoswin

回答

-1

首先,你reproduce方法返回不能被分配到一個ComputerMicrobeString,所以它返回一個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); 
} 

然後將下面的行應該工作:

c = a.reproduce(b); 

您可能需要更改代碼以創建dna代碼。我認爲它需要第一個ComputerMicrobe的前半部分和第二個ComputerMicrobe的後半部分,然而這可能不是這種情況,並且由您決定是否改變它以便它是正確的。

+0

謝謝!這絕對有助於消除我所遇到的很多混亂。 – oreoswin

相關問題