Results 1 to 3 of 3
  1. #1
    tiggz1980 is offline Member
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Default assignment problem help needed

    Hi all i am currently doing an assignment and struggling like mad all i need to do is create a right angled triangle out of asterisks such as

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

    it has the size set to 5 and the orientation set to up all i need to know is how to get the public instance method called generate row to work correctly any help would be appreciated i am drowning i cannot get to grips with this at all.

    the code for the method is below it should use a loop and concatenation to produce the triangle any help on this would be grateful.

    import ou.*;

    /**
    * Class Triangle - instances model triangles as rows of asterisks
    *
    * M255 Course Team
    * Version 1.0
    */
    public class Triangle
    {
    /* instance variables */
    int size; // the size of the triangle; an integer between 2 and 10 inclusive
    String orientation; // either "up" or "down"
    String asterisk;

    /**
    * Constructor for objects of class Triangle.
    */
    public Triangle()
    {
    super();
    this.size = 5;
    this.orientation = "up";


    }



    /* instance methods */

    /**
    * Return the orientation of the triangle.
    */
    public String getOrientation()
    {
    return this.orientation;
    }

    /**
    * Return the size of the triangle.
    */
    public int getSize()
    {
    return this.size;
    }

    /**
    * Generates a row of Asterisks
    */
    public String generateRow()
    {
    asterisk = "";
    aSize = Integer.parseInt(return this.size);
    for (int num = 1; num <= asize; num++)

    asterisk = asterisk + "*";

    System.out.prinln(asterisk);
    }

    }

  2. #2
    felixtfelix is offline Member
    Join Date
    Dec 2007
    Location
    Mumbai, India
    Posts
    37
    Rep Power
    0

    Default

    Try the following piece of code. It will draw downwards. Make a try for the upward one also.
    import java.io.*;

    public class DrawTriangle
    {
    private int rows;


    public int getInteger()throws IOException
    {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    return Integer.parseInt(br.readLine());
    }

    public void generateTriangle()
    {
    for(int i=1;i<=rows;i++)
    {
    String currentRow="";
    for(int j=1;j<=i;j++)
    {
    currentRow = currentRow +"*";
    }
    System.out.println(currentRow);
    }
    }

    public void getNoOfRows()throws IOException
    {
    System.out.print("Enter no of rows:");
    rows = getInteger();
    }


    public static void main(String args[])throws IOException
    {

    DrawTriangle draw = new DrawTriangle();
    draw.getNoOfRows();
    draw.generateTriangle();
    }
    }

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    First welcome to the Java Forums!

    Second, it seems you're going to too much trouble to get the program functioning as it should. There's a recent Triangle question in the forums, see if that helps.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. Array problem.. help needed please!
    By SCS17 in forum New To Java
    Replies: 3
    Last Post: 03-06-2008, 10:30 PM
  2. Java assignment
    By xtianah77 in forum New To Java
    Replies: 1
    Last Post: 02-17-2008, 11:54 PM
  3. for Assignment plz help
    By assamhammad in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 08:35 PM
  4. java assignment, need help bad.
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 04:53 PM
  5. Cannot solve the coding problem of my assignment
    By elimmom in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 11:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •