Results 1 to 16 of 16
  1. #1
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default jGrasp - Creating an Isosceles triangle with a scanner in java language.

    This is an entirely simple problem and I feel like I'm missing something totally obvious, but I have to create an iso triangle using a scanner and input spaces using a separate method. I got a right triangle to be displayed, but i need to correct my method in order to actually add and take away spaces to make something like this:

    /**

    ---*
    --**
    -***
    ****

    */
    (Ignore (-) in above example)

    Here is my code so far:

    Java Code:
    [FONT=Times New Roman][SIZE=5][SIZE=3]public class IsoTriangle
    {
       public static void spaces(int spaces, int decrease)
       {
       for (int j = 1; j <= spaces; j++)
       {
       
          System.out.print(" ");
       
       }
          decrease = spaces - 1;
       }
       
       public static void main(String[] args) 
       {
       int size;
       
       Scanner input = new Scanner(System.in);
       
       System.out.println("Enter size of triangle: ");
          size =  input.nextInt();
          
       int odd = 1;
       int noOfSpaces = size - 1;
       int decrement = noOfSpaces;
       for(int i = 1; i <= size; i++)
       {
          spaces(noOfSpaces, decrement);
          
          for (int j = 1; j <= odd; j++)
          {
             System.out.print("*");
             
          }
          
          System.out.println("");
          odd = odd + 2;
       }
       
       
          
    
    }
    }[/SIZE][/SIZE][/FONT]
    Last edited by Norm; 11-05-2013 at 12:00 AM. Reason: removed spaces in first code tag

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    Have you worked out a design for what is to be printed on each line?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    Quote Originally Posted by Norm View Post
    Have you worked out a design for what is to be printed on each line?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    Is that what you meant to do? x.x and yeah.. I would like to display the triangle using "*".

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    Can you explain what goes on each line as it is printed?
    First line: ???
    second line: ???
    etc for the rest of the lines
    Last edited by Norm; 11-05-2013 at 12:01 AM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    Quote Originally Posted by Norm View Post
    Can you explain what goes on each line as it is printed?
    First line: ???
    second line: ???
    etc for the rest of the lines
    Alright, well in the main method, the first for loop is taking the size they enter into the scanner and it will run each line or row printing out the asterisks. The next for loop will actually take each row and simply add columns such as the first row has 1 column or *, the next will have 3 columns or ***, and so on and so forth. That's why I have the odd increment by two each run through outside of the second loop though and the println obviously to get it onto the next row. Now, what i actually need help with is fixing the spaces method so instead of displaying a right triangle like this:
    /**
    ---*
    ---**
    ---***
    */
    to:
    /**
    ---*
    --**
    -***
    */
    which is why I have a space variable and parameter, in order to essentially get the total amount of spaces needed to start with and then decrease it by one each time actually forming an Isosceles triangle if all that makes sense.

    **Apologies for late responses, handling a bunch at once.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    Can you explain in English what goes on each line without talking too much about the code.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    Yeah sure, sorry.. I'm just wanting someone to enter a preferred size of a triangle. Then, it's going to essentially create a triangle with two equal sides, which you know will increase by two on each row. The spaces I'm talking about are whats added in order to make it centered. Don't over think this at all, it is a really simple problem, just can't figure out how to do the spaces. This is the actual problem that was given:
    Create an IsoTriangle application that prompts the user for the size of an isosceles triangle and then displays the triangle with that many lines.
    Should include:
    -addSpaces() method which prints spaces.
    Application should look similar to:
    Enter the size of the triangle: 4
    ---*
    --**
    -***
    ****
    Hope that helps, don't know how to make it more understandable :/

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    Can you explain in English what goes on the first line and how it is computed.
    If the user gives a size of 6,
    what goes on the first line?
    What goes on the second line?
    etc

    How does the number of spaces change for each line?
    How does the number of "*"s change for each line?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    alright, if the user puts in 6, you know your going to have to use a for loop and run it 6 times. Simply, your going to display one "*", then your going to have to get a counter in order to display that same symbol two more times each run. So, if you put in 6, your gonna start with *, then move to the next line, then **, then move to the next line until you reach 6 rows. The number of spaces are going to start with how big the size you entered is and then it's going to subtract that by one each time in order to take away a space to give it a more triangle like appearance.
    ---*
    --**
    -***
    ****
    notice how each dash decreases by one, that is exactly what i mean.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    number of spaces are going to start with how big the size you entered is and then it's going to subtract that by one each time
    Ok that sounds like a good start. Basically nbr of spaces on a line is: size - line number (with line number starting at 0)
    Now, How many "*"s go on each line?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    Well, it's a triangle, so it will increment by one "*" only for the first row to move to the second, then it will keep going +2 creating a string accumulator basically. Also, yeah, size - line number sounds much simpler. xD

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    What is the relationship between the line number and the number of "*"s on that line?
    Can you say it simply?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    The only simple way I can think to state it is what I've said before. Adding Two "*" 's each time to what is already there.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    Ok I guess that will work. With some thought and a paper and pencil its possible to get an expression that computes the number of * that go on a line.

    Now that you have the rules for the number of spaces and the number of "*"s that go on a line, try writing some code.
    One approach uses nested loops: the outer through the number of lines, the inner ones: one for spaces and one for the "*"s that go on each line.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    Yeah, I got all that, I'm using nested for loops already, but in the code above, everything works except the method that I'm actually trying to implement the spaces with or as you put it, the expression, spaces = size - line number.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: jGrasp - Creating an Isosceles triangle with a scanner in java language.

    in the code above
    If the design for that code is wrong, get rid of it and start over with the new design.


    Java Code:
       decrease = spaces - 1;
    The value of decrease is never used in the method. What is that statement for?
    Why is decrease even passed to the method?
    Last edited by Norm; 11-05-2013 at 04:00 AM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Problem with creating a triangle out of integers...
    By ericj5555 in forum New To Java
    Replies: 1
    Last Post: 11-08-2012, 08:29 AM
  2. JGrasp cant find java?
    By maskanio in forum New To Java
    Replies: 4
    Last Post: 11-03-2012, 04:41 AM
  3. java jgrasp pingpong
    By torlejv123 in forum Java Applets
    Replies: 1
    Last Post: 11-27-2010, 12:57 AM
  4. creating new jvm language
    By dinosoep in forum Advanced Java
    Replies: 22
    Last Post: 08-08-2010, 02:46 PM
  5. Creating a new programming language?
    By hawaiifiver in forum Forum Lobby
    Replies: 0
    Last Post: 03-21-2009, 09:46 PM

Tags for this Thread

Posting Permissions

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