编写 Java 辅助方法
Writing Java helper method
我有 4 个 classes,火箭、三角形、正方形和圆形。
火箭是主要的 class,它允许在动画屏幕中创建火箭的表示。
火箭由 3 种形状组成,三角形是机头,正方形是 body,圆形是喷气机。火箭以垂直编队定向。其他 3 个 class 定义形状的特征。
我需要编写一个辅助方法 'getBodyXPos()' 所以它 returns 一个合适的值,相对于鼻子的位置。鼻子的位置已经定义,所以我需要在代码中使用现有值,这样如果鼻子移动,body 将随之移动它的位置,即不是硬编码值。
这是我的第一个 post,很抱歉代码转储。我很确定这有一个非常简单的解决方案,但我是 Java 的新手,而且我一辈子都找不到一种方法来获得我需要的值。任何帮助将不胜感激,谢谢!
火箭Class
import ou.*;
public class Rocket
{
private Triangle nose; // represents the rocket's nose cone
private Square body; // represents the rocket's body
private Circle jet; // represents the blast from the rocket's engine
/**
* Constructor for objects of class Rocket
*/
public Rocket(Triangle t, Square s, Circle c)
{
//references to the shape objects
this.nose = t;
this.body = s;
this.jet = c;
//sets the initial positions of the nose.
//The other parts need to be set relative to these positions.
this.nose.setXPos(50);
this.nose.setYPos(300);
//sets the body relative to the nose, using the helper methods
this.body.setXPos(getBodyXPos());
this.body.setYPos(getBodyYPos());
this.jet.setColour(OUColour.WHITE);
this.jet.setDiameter(10);
this.jet.setXPos(getJetXPos());
this.jet.setYPos(getJetYPos());
}
private int getBodyXPos()
{
//SOLUTION HERE
return 0;
}
private int getBodyYPos()
{
return 0;
}
}
三角形Class
import ou.*;
public class Triangle extends OUAnimatedObject
{
/* Instance variables */
private OUColour colour;
private int xPos;
private int yPos;
private int width;
private int height;
/**
* Constructor for objects of class Triangle with the default characteristics.
*/
public Triangle()
{
this.colour = OUColour.RED;
this.xPos = 0;
this.yPos = 0;
this.width = 20;
this.height = 20;
}
/* Instance methods */
/**
* Sets the colour of the receiver to the value of the argument aColour.
*/
public void setColour (OUColour aColour)
{
this.colour = aColour;
this.update();
}
/**
* Returns the colour of the receiver.
*/
public OUColour getColour ()
{
return this.colour;
}
/**
* Sets the horizontal position of the receiver to the value of the argument x.
*/
public void setXPos(int x)
{
this.xPos = x;
this.update();
}
/**
* Returns the horizontal position of the receiver.
*/
public int getXPos()
{
return this.xPos;
}
/**
* Sets the vertical position of the receiver to the value of the argument y.
*/
public void setYPos(int y)
{
this.yPos = y;
this.update();
}
/**
* Returns the vertical position of the receiver.
*/
public int getYPos()
{
return this.yPos;
}
/**
* Sets the width of the receiver to the value of the argument aWidth.
*/
public void setWidth(int aWidth)
{
this.width = aWidth;
this.update();
}
/**
* Returns the width of the receiver.
*/
public int getWidth()
{
return this.width;
}
/**
* Sets the height of the receiver to the value of the argument aHeight.
*/
public void setHeight(int aHeight)
{
this.height = aHeight;
this.update();
}
/**
* Returns the height of the receiver.
*/
public int getHeight()
{
return this.height;
}
/**
* Returns a string representation of the receiver.
*/
public String toString()
{
return "An instance of class "+ this.getClass().getName()
+ ": position (" + this.getXPos() + ", " + this.getYPos()
+ "), width " + this.getWidth() + ", height " + this.getHeight()
+ ", colour " + this.getColour();
}
}
广场Class
import ou.*;
public class Square extends OUAnimatedObject
{
/* Instance variables */
private int length;
private OUColour colour;
private int xPos;
private int yPos;
/**
* Constructor for objects of class Square with the default characteristics.
*/
public Square()
{
this.length = 20;
this.colour = OUColour.BLUE;
this.xPos = 0;
this.yPos = 0;
}
/* Instance methods */
/**
* Sets the length of the receiver to the value of the argument aLength.
*/
public void setLength(int aLength)
{
this.length = aLength;
this.update();
}
/**
* Returns the length of the receiver.
*/
public int getLength()
{
return this.length;
}
/**
* Sets the colour of the receiver to the value of the argument aColour.
*/
public void setColour (OUColour aColour)
{
this.colour = aColour;
this.update();
}
/**
* Returns the colour of the receiver.
*/
public OUColour getColour ()
{
return this.colour;
}
/**
* Sets the horizontal position of the receiver to the value of the argument x.
*/
public void setXPos(int x)
{
this.xPos = x;
this.update();
}
/**
* Returns the horizontal position of the receiver.
*/
public int getXPos()
{
return this.xPos;
}
/**
* Sets the vertical position of the receiver to the value of the argument y.
*/
public void setYPos(int y)
{
this.yPos = y;
this.update();
}
/**
* Returns the vertical position of the receiver.
*/
public int getYPos()
{
return this.yPos;
}
/**
* Returns a string representation of the receiver.
*/
public String toString()
{
return "An instance of class "+ this.getClass().getName()
+ ": position (" + this.getXPos() + ", " + this.getYPos()
+ "), length " + this.getLength() + ", colour " + this.getColour();
}
}
希望 Body
开始位置总是与 Nose
开始一些东西对齐
/\
/ \
----
|__|
在这种情况下,Nose
的 X
将是 Body
的 X
和 bodyY = noseY + noseHeight
private int getBodyXPos(){
return nose.getXpos();
}
private int getBodyYPos(){
return nose.getYpos() + nose.getheight();
}
我有 4 个 classes,火箭、三角形、正方形和圆形。
火箭是主要的 class,它允许在动画屏幕中创建火箭的表示。 火箭由 3 种形状组成,三角形是机头,正方形是 body,圆形是喷气机。火箭以垂直编队定向。其他 3 个 class 定义形状的特征。
我需要编写一个辅助方法 'getBodyXPos()' 所以它 returns 一个合适的值,相对于鼻子的位置。鼻子的位置已经定义,所以我需要在代码中使用现有值,这样如果鼻子移动,body 将随之移动它的位置,即不是硬编码值。
这是我的第一个 post,很抱歉代码转储。我很确定这有一个非常简单的解决方案,但我是 Java 的新手,而且我一辈子都找不到一种方法来获得我需要的值。任何帮助将不胜感激,谢谢!
火箭Class
import ou.*;
public class Rocket
{
private Triangle nose; // represents the rocket's nose cone
private Square body; // represents the rocket's body
private Circle jet; // represents the blast from the rocket's engine
/**
* Constructor for objects of class Rocket
*/
public Rocket(Triangle t, Square s, Circle c)
{
//references to the shape objects
this.nose = t;
this.body = s;
this.jet = c;
//sets the initial positions of the nose.
//The other parts need to be set relative to these positions.
this.nose.setXPos(50);
this.nose.setYPos(300);
//sets the body relative to the nose, using the helper methods
this.body.setXPos(getBodyXPos());
this.body.setYPos(getBodyYPos());
this.jet.setColour(OUColour.WHITE);
this.jet.setDiameter(10);
this.jet.setXPos(getJetXPos());
this.jet.setYPos(getJetYPos());
}
private int getBodyXPos()
{
//SOLUTION HERE
return 0;
}
private int getBodyYPos()
{
return 0;
}
}
三角形Class
import ou.*;
public class Triangle extends OUAnimatedObject
{
/* Instance variables */
private OUColour colour;
private int xPos;
private int yPos;
private int width;
private int height;
/**
* Constructor for objects of class Triangle with the default characteristics.
*/
public Triangle()
{
this.colour = OUColour.RED;
this.xPos = 0;
this.yPos = 0;
this.width = 20;
this.height = 20;
}
/* Instance methods */
/**
* Sets the colour of the receiver to the value of the argument aColour.
*/
public void setColour (OUColour aColour)
{
this.colour = aColour;
this.update();
}
/**
* Returns the colour of the receiver.
*/
public OUColour getColour ()
{
return this.colour;
}
/**
* Sets the horizontal position of the receiver to the value of the argument x.
*/
public void setXPos(int x)
{
this.xPos = x;
this.update();
}
/**
* Returns the horizontal position of the receiver.
*/
public int getXPos()
{
return this.xPos;
}
/**
* Sets the vertical position of the receiver to the value of the argument y.
*/
public void setYPos(int y)
{
this.yPos = y;
this.update();
}
/**
* Returns the vertical position of the receiver.
*/
public int getYPos()
{
return this.yPos;
}
/**
* Sets the width of the receiver to the value of the argument aWidth.
*/
public void setWidth(int aWidth)
{
this.width = aWidth;
this.update();
}
/**
* Returns the width of the receiver.
*/
public int getWidth()
{
return this.width;
}
/**
* Sets the height of the receiver to the value of the argument aHeight.
*/
public void setHeight(int aHeight)
{
this.height = aHeight;
this.update();
}
/**
* Returns the height of the receiver.
*/
public int getHeight()
{
return this.height;
}
/**
* Returns a string representation of the receiver.
*/
public String toString()
{
return "An instance of class "+ this.getClass().getName()
+ ": position (" + this.getXPos() + ", " + this.getYPos()
+ "), width " + this.getWidth() + ", height " + this.getHeight()
+ ", colour " + this.getColour();
}
}
广场Class
import ou.*;
public class Square extends OUAnimatedObject
{
/* Instance variables */
private int length;
private OUColour colour;
private int xPos;
private int yPos;
/**
* Constructor for objects of class Square with the default characteristics.
*/
public Square()
{
this.length = 20;
this.colour = OUColour.BLUE;
this.xPos = 0;
this.yPos = 0;
}
/* Instance methods */
/**
* Sets the length of the receiver to the value of the argument aLength.
*/
public void setLength(int aLength)
{
this.length = aLength;
this.update();
}
/**
* Returns the length of the receiver.
*/
public int getLength()
{
return this.length;
}
/**
* Sets the colour of the receiver to the value of the argument aColour.
*/
public void setColour (OUColour aColour)
{
this.colour = aColour;
this.update();
}
/**
* Returns the colour of the receiver.
*/
public OUColour getColour ()
{
return this.colour;
}
/**
* Sets the horizontal position of the receiver to the value of the argument x.
*/
public void setXPos(int x)
{
this.xPos = x;
this.update();
}
/**
* Returns the horizontal position of the receiver.
*/
public int getXPos()
{
return this.xPos;
}
/**
* Sets the vertical position of the receiver to the value of the argument y.
*/
public void setYPos(int y)
{
this.yPos = y;
this.update();
}
/**
* Returns the vertical position of the receiver.
*/
public int getYPos()
{
return this.yPos;
}
/**
* Returns a string representation of the receiver.
*/
public String toString()
{
return "An instance of class "+ this.getClass().getName()
+ ": position (" + this.getXPos() + ", " + this.getYPos()
+ "), length " + this.getLength() + ", colour " + this.getColour();
}
}
希望 Body
开始位置总是与 Nose
开始一些东西对齐
/\
/ \
----
|__|
在这种情况下,Nose
的 X
将是 Body
的 X
和 bodyY = noseY + noseHeight
private int getBodyXPos(){
return nose.getXpos();
}
private int getBodyYPos(){
return nose.getYpos() + nose.getheight();
}