Вопрос

You will create two classes, one for a box and one for a triangle. Each of the figures, the box and the triangle, will have an offset telling how far they are indented from the edge of the screen. Each figure will also have a size, although the size will be determined differently for a box and a triangle. The base class Figure will have instance variables for any properties that all figures have in common and will have methods for the actions that all figures have. For example, every figure has a given offset, and every figure should be able to draw itself. However, because we don’t know how to draw a figure of unknown shape, declare drawHere() to be an abstract method and Figure to be an abstract class. The classes Box and Triangle will then be derived classes of Figure and they will provide the implementation of drawHere(). Here is the UML diagram of Figure, Box, and Triangle.

The method drawHere will simply indent a number of spaces on the screen equal to the offset and then write an asterisk on the screen. This is just so you can have something to test. You do not intend to use this version of drawHere in any application. You will override the definition of drawHere when you define classes for boxes and triangles.

The method drawAt has one parameter of type int. The method drawAt inserts a number of blank lines equal to this parameter and then draws the figure by calling drawHere. When you override drawHere, then drawAt will also produce correct figures.

I need to have a Christmas tree that prints out like this:

           *
          * *
         *   *
        *     *
       *       *
      *         *
     *           *
    *             *
   *               *
  *                 *
 *********************
         -----
         |   |
         |   |
         -----

Figure class:

public abstract class Figure 
{
private int offset;

public Figure()
{
   offset = 0;
}

public Figure(int theOffset)
{
   offset = theOffset;
}

public void setOffset(int newOffset)
{
   offset = newOffset;
}

public int getOffset()
{
   return offset;
}

public void drawAt(int lineNumber)
{
   for (int count = 0; count < lineNumber; count++)
      System.out.println();
   drawHere();
}

public abstract void drawHere();
}

Triangle class:

public class Triangle extends Figure
{
   private int base;

   public Triangle()
   {
      base = 0;
   }

   public Triangle(int offset, int base)
   {
      offset = 0;
      this.base = base;
   }

   public void reset(int newOffset, int newBase)
   {
      newOffset = 0;
      newBase = 0;
   }

   public void drawHere()
   {
      for (int count = 0; count < base; count++)
          System.out.print("");
     System.out.println("*");
   }
}

Box class:

public class Box extends Figure
{
private int height;
private int width;

public Box()
{
  height = 0;
  width = 0;
}

public Box(int offset, int height, int width)
{
  super(offset);
  this.height = height;
  this.width = width;
}

public void reset(int newOffset, int newHeight, int newWidth)
{
  newOffset = 0;
  newHeight = 0;
  newWidth = 0;
}

public void drawHere()
{
  for (int count = 0; count < width; count++)
     System.out.print("");
  System.out.println('-');
}
}

GraphicsTest class:

public class GraphicsTest 
{
   public static void main(String[] args)
   {
       Triangle top = new Triangle (5, 21);
       Box base = new Box(13, 4, 5);
       top.drawAt(1);
       base.drawAt(0);
   }
}

The code that it returns is:

*
-

The question I have is that what do I need to fix in the code so that it prints out the Christmas Tree. I've tried changing around the variable that is the for loop in the drawHere method and nothing will fix it. Thanks for the help!

Это было полезно?

Решение

Your triangle method is:

   public void drawHere()
   {
      for (int count = 0; count < base; count++)
          System.out.print("");
     System.out.println("*");
   }

What this does is print out only 1 star, because you are printing an empty string inside the for loop.

Here is the code I drew up for a simple triangle, through experimentation.

    int size = 11; 
    for(int i = 0; i < size-1; i++) {
        for(int j = 0; j < size - (i+1); j++)
            System.out.print(" ");
        System.out.print("*");
        for(int k = 0; k < 2*i - 1; k++)
            System.out.print(" ");
        if(i > 0)
            System.out.print("*\n");
        else
            System.out.println();
    }
    for(int i = 0; i < size; i++)
        System.out.print("* ");

Here is its subsequent output:

          *
         * *
        *   *
       *     *
      *       *
     *         *
    *           *
   *             *
  *               *
 *                 *
* * * * * * * * * * *

Другие советы

Here is the reason why you get the result with your code.

Look at method drawHere

public void drawHere()
{
  for (int count = 0; count < base; count++)
      System.out.print("");
 System.out.println("*");
}

In the for-loop you did nothing. In this method character * is printed only once. That's why you only see on * in console.

With the same reason, you got only one '-' when drawing Box.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top