Results 1 to 7 of 7
  1. #1
    mav3r1ck83 is offline Member
    Join Date
    Feb 2015
    Posts
    3
    Rep Power
    0

    Default centered allignment for console.

    I have this assignment, it really got me stomped. and it's an online class with a professor that is NO HELP at all.

    "Write a java application that displays the following patterns separately one below the other.
    Use for loops to generate the patterns. There are 4 Triangle patterns.
    All asterisks (*) should be printed by a single statement of the form System.out.print('*');
    This is 4-different patterns, not 1-pattern."


    the first 2 triangles are right angles and is simple to make with a loop with in a loop
    the 3rd triangle is like an inverted christmas tree and the 4th one is a christmas tree. ( It's not showing properly on the board so i deleted it)
    I can't seem to make the 3rd and 4th triangle line up properly it looks like this (the _ represents blank spaces on the console)
    _____*_____
    _____**_____
    ____***_____
    ____****_____
    ___*****____
    ___******_____
    __*******
    __********____

    notice how the sides of the triangle isn't straight and the triangle looks like its dancing, the assignment requires it to be a straight line. is there anyway to do this?
    this is suppose to be the output. all the lines of the triangle is straight and not jagged
    centered allignment for console.-triangle.jpg

    public class myclass {
    public static void main(String[] args) {
    int counter = 1;
    int counter2 = 1;
    while (counter != 11){
    EmptyLine();
    counter2 = 0;
    while (counter != counter2){
    PrintMe();
    counter2++;
    }
    counter++;

    }
    EmptyLine();
    counter = 10;
    while (counter != 0 ){
    EmptyLine();
    counter2 = 0;
    while (counter != counter2){
    PrintMe();
    counter2++;
    }
    counter = counter - 1;
    }
    EmptyLine();
    counter = 1;
    int counter3 = 0;
    int counter4 = 10;
    while (counter != 0){
    EmptyLine();
    counter2 = 0;
    }
    }
    public static void PrintMe(){
    System.out.print('*');
    }
    public static void EmptyLine(){
    System.out.println("");
    }

    }


    my code for those particular triangle is in the work because I cant figure out how to make it look like that.
    Last edited by mav3r1ck83; 02-08-2015 at 06:53 AM. Reason: posting output

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: centered allignment for console.

    It would be helpful if you first showed what the output is supposed to look like and to also include your code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: centered allignment for console.

    It can't be done with a console font that is "fixed" width. If you compare the first three blue lines you can see that the width of a space (or some whole number of spaces) must be exactly half the width of an asterisk.

    I was going to suggest that you write code that would output the xmas tree under those conditions (ie assuming the variable width font). But then I realised you already have: it's one of the right triangles.
    Last edited by pbrockway2; 02-08-2015 at 07:42 AM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: centered allignment for console.

    Quote Originally Posted by mav3r1ck83 View Post
    I have this assignment, it really got me stomped. and it's an online class with a professor that is NO HELP at all.
    Of course, put the blame on the teacher again ...

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    mav3r1ck83 is offline Member
    Join Date
    Feb 2015
    Posts
    3
    Rep Power
    0

    Default Re: centered allignment for console.

    Quote Originally Posted by JosAH View Post
    Of course, put the blame on the teacher again ...

    Jos
    well here's the thing. when i first brought up my question to him, all he said was "because the lines aren't straight which they should be" no guidance or anything just that sentence. And after my 2nd email with me posting it here, its not possible etc etc. his reply that i got this morning was "look at it now, the right side is vertical" .... geeze after 3 days of banging my head trying to figure out an impossible assignment that's all he has to say? correct me if I'm wrong, but his first reply to me did not offer any help what so ever. and to assign an impossible assignment...... i'm not quite sure what hes trying to do, any email I sent him has been answered by short 1 sentence answers with no form of guidance, no hint or anything. pretty much it's just different ways of saying "figure it out yourself"

    another example of him being no help at all. I asked his help with conversion to hexadecimal assignment we had. I explained how there's something wrong with how i'm converting char into a string[], I sent him my source code. his reply was "figure out how to manually convert first before you start to code".... I had a complete code sent to him it converted decimals to binary octal and hexadecimal, my mistake was in the part of placing the char into the string, which he would have seen if he actually tried to look at the source code i sent him complete with comments.
    Last edited by mav3r1ck83; 02-08-2015 at 06:19 PM.

  6. #6
    mav3r1ck83 is offline Member
    Join Date
    Feb 2015
    Posts
    3
    Rep Power
    0

    Default Re: centered allignment for console.

    thanks for the help. now that the professor changed it to something possible. i was able to get the desired output promptly
    centered allignment for console.-untitled-4.jpg

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: centered allignment for console.

    Great! Just so you know, based on what pbrockway2 said earlier, you can only center triangles properly using fixed width fonts if the lines all contain either an even or all contain an odd number of characters. See below.


    The underlines represent white space.
    Even # characters per line


    ___**
    __****
    _******


    Odd # characters per line

    ___*
    __***
    _*****

    Regards,
    Jim


    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 0
    Last Post: 03-15-2014, 10:54 AM
  2. Replies: 15
    Last Post: 12-14-2012, 01:42 PM
  3. GUI labels allignment
    By dragon_baby in forum New To Java
    Replies: 2
    Last Post: 06-17-2012, 06:32 PM
  4. drawing a centered grid with JFrame
    By adonaldson in forum AWT / Swing
    Replies: 3
    Last Post: 02-19-2012, 10:01 PM
  5. Replies: 2
    Last Post: 10-30-2008, 12:48 AM

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
  •