2016-03-05 39 views
0

我是一名初學Java程序員,已經在相當長的一段時間裏辛苦了。我需要將下面的程序轉換爲OOP格式,並且不能無誤地編譯它。我想我會發布工作非格式化的程序,而不是我的失敗和波濤洶涌的嘗試。如果任何人都可以將下面的程序轉換爲OOP,那將非常感激。請原諒,因爲我是新手,所以無能爲力。將Java程序轉換爲面向對象的格式(基本)

感謝您的幫助

import java.awt.*; 

public class Turtle3 { 

    public static void drawLine(Turtle myrtle, Color color, int x1, int y1, int x2, int y2) { 
    myrtle.hide(); 
    myrtle.penUp(); 
    myrtle.setPenColor(color); 
    myrtle.moveTo(x1, y1); 
    myrtle.penDown(); 
    myrtle.moveTo(x2, y2); 
    } 

    public static void setPenWidth(int w) { 
    World worldObj = new World(); 
    Turtle myrtle = new Turtle(200, 200, worldObj); 
    Turtle billy = new Turtle(100, 700, worldObj); 
    Turtle thomas = new Turtle(100, 100, worldObj); 
    myrtle.setColor(Color.RED); 
    myrtle.setPenWidth(50); 
    myrtle.forward(50); 
    myrtle.show(); 
    billy.setColor(Color.BLUE); 
    billy.setPenWidth(100); 
    billy.forward(300); 
    thomas.show(); 
    thomas.setColor(Color.BLACK); 
    thomas.setPenWidth(100); 
    thomas.forward(50); 
    thomas.show(); 
    }; 

    public static void main(String[] args) {  
    World worldObj = new World(); 
    Turtle myrtleTheTurtle = new Turtle(0, 0, worldObj); 

    drawLine(myrtleTheTurtle, Color.RED, 10, 20, 700, 20);  
    drawLine(myrtleTheTurtle, Color.BLUE, 10, 40, 700, 40); 
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 60, 700, 60);    
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 80, 700, 80); 
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 100, 700, 100); 
    drawLine(myrtleTheTurtle, Color.RED, 10, 120, 700, 120);  
    drawLine(myrtleTheTurtle, Color.BLUE, 10, 140, 700, 140); 
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 160, 700, 160);    
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 180, 700, 180); 
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 200, 700, 200); 
    drawLine(myrtleTheTurtle, Color.RED, 10, 220, 700, 220);  
    drawLine(myrtleTheTurtle, Color.BLUE, 10, 240, 700, 240); 
    drawLine(myrtleTheTurtle, Color.RED, 10, 260, 700, 260);  
    drawLine(myrtleTheTurtle, Color.BLUE, 10, 280, 700, 280); 
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 300, 700, 300);    
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 320, 700, 320); 
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 340, 700, 340); 
    drawLine(myrtleTheTurtle, Color.RED, 10, 360, 700, 360);  
    drawLine(myrtleTheTurtle, Color.BLUE, 10, 380, 700, 380); 
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 400, 700, 400);    
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 420, 700, 420); 
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 440, 700, 440); 
    drawLine(myrtleTheTurtle, Color.BLUE, 10, 460, 700, 460); 
    drawLine(myrtleTheTurtle, Color.BLACK, 10, 480, 700, 480); 
    drawLine(myrtleTheTurtle, Color.BLACK, 480, 10, 480, 700); 
    drawLine(myrtleTheTurtle, Color.BLACK, 180, 10, 180, 700); 
    drawLine(myrtleTheTurtle, Color.BLACK, 480, 10, 480, 700); 
    drawLine(myrtleTheTurtle, Color.BLACK, 330, 10, 330, 700); 

    setPenWidth(50); 
    }//end of main method 
}//end of class 

回答

0

我不完全相信你的要求。您已經在您的setPenWidth()方法中使用了OOP。如果你想在你的程序中使用更多的OOP,你可能會想你的方法drawLine()移到Turtle類並將其更改爲類似

public void drawLine(Color color, int x1, int y1, int x2, int y2) 
{ 
    this.hide(); 
    this.penUp(); 
    this.setPenColor(color); 
    this.moveTo(x1, y1); 
    this.penDown(); 
    this.moveTo(x2, y2); 
} 

其中提到Turtle當前實例,而無需將它傳遞作爲論據。注意:您必須拿走static關鍵字才能使其工作。然後,你必須改變你的代碼的主要方法來利用這個通過改變你的電話drawLine()drawLine(myrtleTheTurtle, color, x1, y1, x2, y2);myrtleTheTurtle.drawLine(color, x1, y1, x2, y2)

+0

我不知道你的意思是「要將你的方法drawLine()移動到龜類「因爲它已經和其他所有東西一樣在這個類中。 –

+0

找不到符號 - 方法隱藏這是我按照您的指示運行程序時得到的錯誤 –

+0

您所有的東西都在Turtle3類中。我的意思是把它移到「烏龜」類,它定義了海龜的類型,比如默特爾,比利和托馬斯 – goose121

相關問題