I have tried adding a main method and running the file but cannot get an * as my result.
Where am i going wrong?

more information:
(i) It should instantiate a Figure object using the empty constructor and then run the object’s drawAt() method with 2 different lineNumber values, say 2 and 6.

(ii) It should instantiate two Figure objects using 2 different theOffset values, say 5 and 10, and for each object run the drawAt() method with lineNumber equal to 4, say.

package sdexam;

public 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)
{
    int count;
    for (count = 0; count < lineNumber;count++)
        System.out.println();
    drawHere();
}

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

}

}

add the correct type, name, signature, ... and it should work just fine, that is, of course, if you call the correct methods

I think when you are trying to create a figure with the empty constructor(parameterless default constructor), but you are not setting its offset using the setOffset() method, below is the code of how you can create object using the default constructor or using the parameterless default constructor:

public class FigureApp
{
    public static void main(String[] Args)
    {
        Figure figure0=new Figure();
        figure0.setOffset(2);
        figure0.drawAt(4);
        figure0.setOffset(6);
        figure0.drawAt(4);

        Figure figure1=new Figure(5);
        figure1.drawAt(2);

        Figure figure2=new Figure(10);
        figure2.drawAt(4);
    }
}

Hope this helps.. :)

Hope this helps.. :)

it only helps if you think this is a "we do your homework for you" site. All you have taught the OP is a quick way to cheat.

Edited 4 Years Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.