2016-03-05 42 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