有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

封装如何在OOP Java中为主方法编码?

这是我的作业

你只需要在屏幕上画一个点,在屏幕上画一个圆。让我们把它们当作游戏中的角色。我们将把这两个字符都称为可移动对象。如果物体可以在屏幕上移动,则称其为可移动的。允许以下简单移动:

-public void moveLeft();//将x向左移动1个单位

-public void moveRight();//将x向右移动1个单位

-public void moveUp();//向上移动y 1单元

-public void moveDown();//向下移动y 1单元

-public void display();//打印有关移动设备的所有信息

点和圆字符都是可移动的。 该点具有x坐标和y坐标。圆有一个点(代表它的中心)和一个半径(为了我们的目的,这是一个int)。这些属性都可以设置为public

-不用说,点和圆应该实现所有移动的方法 因为它们是可移动的物体

-角色将要移动的屏幕是一个具有 坐标(0,0)作为其左上角,坐标(200200)作为其右下角

-确保当字符在屏幕上移动时,它们不会离开屏幕 屏幕,尤其是圆形字符,因为它包含半径

-对于display()函数,in Point,print Point:(x,y)和in Circle,print Circle:(x,y),r

输入格式 第一个输入是1(一个点)或2(一个圆)。如果是点,则跟随2个非负整数;如果是圆,则跟随3个非负整数,第3个为半径。然后读取一个正整数m,表示必须执行的操作数,后跟操作本身。-1-moveLeft()-2-moveRight()-3-moveUp()-4-moveDown()

输入样本

1

0 0

5

4

4

2

2 

1

输出样本

Point:·(1,2)

这缺少了main方法,但是我也不知道如何为它编码。我很难理解主方法的编码,你能帮我吗

    interface Moveable
{
   public void moveLeft();
   public void moveRight();
   public void moveUp();
   public void moveDown();
   public void display();
}

class Point implements Moveable

{

public static final int MAX_CHAR=200;
protected int x;
protected int y;

public Point(int X, int Y) { x=X; y=Y; }
public int GetX() { return(x); }
public int GetY() { return(y); }


Point(Point p) { this.x = p.x; this.y=p.y; } //copy constructor

//implements Moveable

public void moveLeft()
{
    if (x>0)
{
    x--;
}

else{

System.out.println("BUMP:Bounces off left side");

}

}

public void moveRight()
{

    if (x<Point.MAX_CHAR)

{
    x++;
}

else
{
    System.out.println("BUMP:Bounces off right side");
}

}
public void moveUp()
{
if (y>0)
{
    y--;
}

else

{
    System.out.println(" BUMP: Bounces off top ");
}
}
public void moveDown()
{
if (y<Point.MAX_CHAR)
{
    y++;
}
else
{
    System.out.println(" BUMP: Bounces off bottom");
}

}
@Override public String toString()

{

return(
new String("Point:(" +x + "," +y+")")

);

}

public void display()

{

System.out.println(toString());

}

}

/*****************************************************************************/

class Point implements Moveable

{

public static final int MAX_CHAR=200;
protected int x;
protected int y;

public Point(int X, int Y) { x=X; y=Y; }
public int GetX() { return(x); }
public int GetY() { return(y); }

Point(Point p) { this.x = p.x; this.y=p.y; } //copy constructor

//implements Moveable

public void moveLeft()

{

    if (x>0)

{

    x--;

}

else
{
    System.out.println("BUMP:Bounces off left side");

}

}

public void moveRight()

{

if (x<Point.MAX_CHAR)
{

    x++;
}
else
{
    System.out.println("BUMP:Bounces off right side");
}

}

public void moveUp()

{
if (y>0)
{
    y--;

}

else

{
    System.out.println(" BUMP: Bounces off top ");

}

}

public void moveDown()

{
if (y<Point.MAX_CHAR)
{
    y++;
}

else
{

    System.out.println(" BUMP: Bounces off bottom");

}

}



@Override public String toString()

{

return(

new String("Point:(" +x + "," +y+")")

);

}

public void display()

{

System.out.println(toString());

}
}

/******************************************************************/

class Point implements Moveable

{



public static final int MAX_CHAR=200;
protected int x;
protected int y;

public Point(int X, int Y) { x=X; y=Y; }
public int GetX() { return(x); }
public int GetY() { return(y); }


Point(Point p) { this.x = p.x; this.y=p.y; } //copy constructor



//implements Moveable

public void moveLeft()

{

if (x>0)

{

    x--;

}

else

{

System.out.println("BUMP:Bounces off left side");

}

}



public void moveRight()

{

if (x<Point.MAX_CHAR)

{

x++;

}

else

{

System.out.println("BUMP:Bounces off right side");

}

}



public void moveUp()

{

if (y>0)

{
    y--;
}

else

{
System.out.println(" BUMP: Bounces off top ");

}

}


public void moveDown()

{
if (y<Point.MAX_CHAR)
{
    y++;

}

else
{
    System.out.println(" BUMP: Bounces off bottom");
}

}

@Override public String toString()

{

return (new String("Point:(" +x + "," +y+")"));

}

public void display()

{

System.out.println(toString());

}
}

共 (0) 个答案