Write a program that draws a right arrow. This is yet another part of our shapes
ID: 645585 • Letter: W
Question
Write a program that draws a right arrow. This is yet another part of our shapes collection which has includes ShapeInterface and ShapeBasics. Also download this driver to test the program.Create an interface called ArrowInterface with the following method definitions
getTailLength: this returns an int and has no parameters
setTailLength: this returns nothing and has an int parameter
getArrowBase: this return an int and has no parameters
setArrowBase: this return nothing and has an int parameter
Create class called RightArrow as follows
It needs to implement the ArrowInterface and inherit from ShapeBasics
Instance variables
tailLength: corresponds to the length of the tail of the arrow and cannot be a negative value
arrowBase: the size of the arrow Write a program that draws a right arrow. This is yet another part of our shapes collection which has includes ShapeInterface and ShapeBasics. Also download this driver to test the program.
Create an interface called ArrowInterface with the following method definitions
getTailLength: this returns an int and has no parameters
setTailLength: this returns nothing and has an int parameter
getArrowBase: this return an int and has no parameters
setArrowBase: this return nothing and has an int parameter
Create class called RightArrow as follows
It needs to implement the ArrowInterface and inherit from ShapeBasics
Instance variables
tailLength: corresponds to the length of the tail of the arrow and cannot be a negative value
arrowBase: the size of the arrow Write a program that draws a right arrow. This is yet another part of our shapes collection which has includes ShapeInterface and ShapeBasics. Also download this driver to test the program.
Create an interface called ArrowInterface with the following method definitions
getTailLength: this returns an int and has no parameters
setTailLength: this returns nothing and has an int parameter
getArrowBase: this return an int and has no parameters
setArrowBase: this return nothing and has an int parameter
Create class called RightArrow as follows
It needs to implement the ArrowInterface and inherit from ShapeBasics
Instance variables
tailLength: corresponds to the length of the tail of the arrow and cannot be a negative value
arrowBase: the size of the arrow
Explanation / Answer
Program code:
/** Interface for simple shapes drawn on the screen using keyboard characters. */
public interface ShapeInterface
{
/** Sets the offset for the drawing. */
public void setOffset(int newOffset);
/** Returns the offset for the drawing. */
public int getOffset();
/** Draws the shape at lineNumber lines down from the current line. */
public void drawAt(int lineNumber);
/** Draws the shape at the current line. */
public void drawHere();
}
/**
* Class for drawing simple shapes on the screen using keyboard characters. This
* class will draw an asterisk on the screen as a test. It is not intended to
* create a "real" shape, but rather to be used as a base class for other
* classes of shapes.
*/
public class ShapeBasics implements ShapeInterface
{
private int offset;
public ShapeBasics()
{
offset = 0;
}
public ShapeBasics(int theOffset)
{
offset = theOffset;
}
public void setOffset(int newOffset)
{
offset = newOffset;
}
public int getOffset()
{
return offset;
}
/** Draws the shape at lineNumber lines down from the current line. */
public void drawAt(int lineNumber)
{
for (int count = 0; count < lineNumber; count++)
System.out.println();
drawHere();
}
/** Draws the shape at the current line. */
public void drawHere()
{
for (int count = 0; count < offset; count++)
System.out.print(' ');
System.out.println('*');
}
}
public interface ArrowInterface
{
/** returns the size of the tail */
public int getTailLength();
/** sets the size of the tail depending on the parameter */
public void setTailLength(int length);
/** returns the base of the arrow */
public int getArrowBase();
/** sets the base of the arrow depending on the parameter */
public void setArrowBase(int base);
}
public class RightArrow extends ShapeBasics implements ArrowInterface
{
int tailLength;
int arrowBase;
/** default constructor */
RightArrow()
{
super();
tailLength = 0;
arrowBase = 0;
}
/** parameterized */
RightArrow(int offset, int tail, int base)
{
super(offset);
tailLength = tail;
arrowBase = base;
}
/** accessor method to get length of the tail */
public int getTailLength()
{
return tailLength;
}
/** Mutator method to set the length of the tail */
public void setTailLength(int tailLength)
{
this.tailLength = tailLength;
}
/** accessor method to get base of the arrow */
public int getArrowBase()
{
return arrowBase;
}
/** Mutator method to set the base of the arrow */
public void setArrowBase(int arrowBase)
{
if (arrowBase >= 1)
{
if (arrowBase % 2 == 0)
{
System.out.println("Arrows Base should be an odd number.");
}
this.arrowBase = arrowBase;
} else
System.out.println("The arrow's base needs to be an odd number");
}
/** overriding the drawHere method */
public void drawHere()
{
/**
* get the actual off set of the shape base and store it in a
* variables
*/
int offset = super.getOffset();
/** logic to draw the upper triangle */
for (int i = 0; i < arrowBase; i++)
{
for (int j = 0; j < offset; j++)
{
System.out.print(" ");
}
System.out.print("*");
if (i > 0)
{
super.setOffset(i);
super.drawHere();
}
System.out.println();
}
/** logic to draw the tail of the arrow */
for (int i = 0; i < tailLength; i++)
{
System.out.print("*");
}
super.setOffset(super.getOffset());
super.drawHere();
System.out.println();
/** logic to draw the lower triangle */
for (int i = arrowBase - 1; i >= 0; i--)
{
for (int j = 0; j < offset; j++)
{
System.out.print(" ");
}
System.out.print("*");
if (i > 0)
{
super.setOffset(i);
super.drawHere();
}
System.out.println();
}
}
}
public class ArrowTester
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
ArrowInterface[] arrows = new RightArrow[5];
System.out.println("Arrows are neat!");
System.out.println("Populating arrow array!");
arrows[0] = new RightArrow(10, 16, 7);
arrows[1] = new RightArrow(10, 15, 13);
arrows[2] = new RightArrow(0, 4, 4);
arrows[3] = new RightArrow(0, 1, 5);
arrows[4] = new RightArrow(5, 5, 5);
for (RightArrow arrow : (RightArrow[]) arrows)
{
arrow.drawHere();
}
}
}
Sample Output:
Arrows are neat!
Populating arrow array!
*
* *
* *
* *
* *
* *
* *
**************** *
* *
* *
* *
* *
* *
* *
*
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*************** *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
*
* *
* *
* *
**** *
* *
* *
* *
*
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
*
* *
* *
* *
* *
***** *
* *
* *
* *
* *
*
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.