Creating a Pattern Assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NoviceJava
    New Member
    • Mar 2008
    • 14

    Creating a Pattern Assignment

    Hey,

    I'm having trouble with an assignment and I need some help.

    basically, the assignment is the same as the from the following link:
    http://www.thescripts. com/forum/thread607498.ht ml

    However, there is one major exception: The pattern is reversed. Here is what it looks like:
    If the user types "3", your program should produce the following output:
    ***
    **
    *
    If the user types "9", your program should produce:
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *

    I found out how to do the other pattern of my own but I am having trouble figuring out how to flip it.

    Here is my code so far:
    ------------------------------------------------------------------
    import java.io.*;

    public class MagicPrinter
    {
    public static void main(String[] args)
    throws java.io.IOExcep tion
    {
    String s1, s2, s3, total;
    int num, minnum;
    char a = 'a';
    String star = "*";

    total = "";

    InputStreamRead er isr = new InputStreamRead er(System.in);

    BufferedReader br = new BufferedReader( isr);

    System.out.prin t("How many lines? ");
    s1 = br.readLine();
    num = Integer.parseIn t(s1);

    minnum = 1;

    do
    {
    total = total + star;
    minnum++;

    System.out.prin tln(total);
    }
    while(minnum <= num);
    }
    }
    ----------------------------------------------------------------

    Any help would be apprecitated. Thanks.
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by NoviceJava
    Hey,

    I'm having trouble with an assignment and I need some help.

    basically, the assignment is the same as the from the following link:


    However, there is one major exception: The pattern is reversed. Here is what it looks like:
    If the user types "3", your program should produce the following output:
    ***
    **
    *
    If the user types "9", your program should produce:
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *

    I found out how to do the other pattern of my own but I am having trouble figuring out how to flip it.

    Here is my code so far:
    ------------------------------------------------------------------
    import java.io.*;

    public class MagicPrinter
    {
    public static void main(String[] args)
    throws java.io.IOExcep tion
    {
    String s1, s2, s3, total;
    int num, minnum;
    char a = 'a';
    String star = "*";

    total = "";

    InputStreamRead er isr = new InputStreamRead er(System.in);

    BufferedReader br = new BufferedReader( isr);

    System.out.prin t("How many lines? ");
    s1 = br.readLine();
    num = Integer.parseIn t(s1);

    minnum = 1;

    do
    {
    total = total + star;
    minnum++;

    System.out.prin tln(total);
    }
    while(minnum <= num);
    }
    }
    ----------------------------------------------------------------

    Any help would be apprecitated. Thanks.
    Algorithm:

    Assuming my input is 7

    Code:
    while( input not equal to 0 )
           
           Print the ' * ' input times...
           Create new line...
           Decrement the input...

    Sukatoa (Shadow Shaman)

    Comment

    • NoviceJava
      New Member
      • Mar 2008
      • 14

      #3
      Originally posted by sukatoa
      Algorithm:

      Assuming my input is 7

      Code:
      while( input not equal to 0 )
             
             Print the ' * ' input times...
             Create new line...
             Decrement the input...

      Sukatoa (Shadow Shaman)
      I tried this and it is giving me the output

      *
      *
      *
      *
      *
      *
      *
      *
      *
      This is what I typed:
      while (num != 0)
      {
      System.out.prin tln(star);
      num--;
      }

      when I also try:
      while (num !=0)
      {
      total = total + star;
      System.out.prin tln(total);
      num--;
      }

      I get the same pattern I had to begin with. Maybe I'm just unsure of what you meant by print '*' input times...

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        Originally posted by NoviceJava
        I tried this and it is giving me the output

        *
        *
        *
        *
        *
        *
        *
        *
        *
        This is what I typed:
        while (num != 0)
        {
        System.out.prin tln(star);
        num--;
        }

        when I also try:
        while (num !=0)
        {
        total = total + star;
        System.out.prin tln(total);
        num--;
        }

        I get the same pattern I had to begin with. Maybe I'm just unsure of what you meant by print '*' input times...
        print '*' input times is = Print the asterisk many times depend on the input, if my input is currently 7, then print the asterisk 7 times in the same line...

        Maybe you forgot this,

        What is the difference between

        Code:
        System.out.print("Something");
        and

        Code:
        System.out.println("Something");
        If you can differentiate that, then proceed... If you are not sure, make some experiments about the two...


        Update us.
        Sukatoa

        Comment

        • NoviceJava
          New Member
          • Mar 2008
          • 14

          #5
          Originally posted by sukatoa
          print '*' input times is = Print the asterisk many times depend on the input, if my input is currently 7, then print the asterisk 7 times in the same line...

          Maybe you forgot this,

          What is the difference between

          Code:
          System.out.print("Something");
          and

          Code:
          System.out.println("Something");
          If you can differentiate that, then proceed... If you are not sure, make some experiments about the two...


          Update us.
          Sukatoa
          I know the difference between the methods; println returns a new line after completing while print prints subsequent information on the same line. I played around with many different things and this is what I ended with

          while (num != 0)
          {
          total = total + star;
          System.out.prin t(total);
          System.out.prin t("");
          num--;
          }

          When imputing 9 I get the required number of stars but on the same line. When i use println for the second method I just get the same result I had to begin with.

          Comment

          • sukatoa
            Contributor
            • Nov 2007
            • 539

            #6
            You must understand the code below before you attempt to try it, if you don't follow my instructions, later you will be dependent from the others....

            I hope you will understand this....

            Code:
            public class demo{
            	public static void main(String sukatoa[]){
            		int input = new java.util.Scanner(System.in).nextInt();
            		while(input > 0){
            			System.out.println();
            			for(int x=0;x<input;x++){
            				System.out.print("*");
            			}input--;
            		}System.out.println();
            	}
            }
            The code i've posted above will be deleted after a few minutes by the moderator... Please, read the book carefully next time...

            Try to make have some experiments... Later you will come-up an idea...

            Worrying
            Sukatoa.
            Last edited by sukatoa; Mar 9 '08, 01:51 PM. Reason: Changes

            Comment

            • NoviceJava
              New Member
              • Mar 2008
              • 14

              #7
              Originally posted by sukatoa
              You must understand the code below before you attempt to try it, if you don't follow my instructions, later you will be dependent from the others....

              I hope you will understand this....

              Code:
              public class demo{
              	public static void main(String sukatoa[]){
              		int input = new java.util.Scanner(System.in).nextInt();
              		while(input > 0){
              			System.out.println();
              			for(int x=0;x<input;x++){
              				System.out.print("*");
              			}input--;
              		}System.out.println();
              	}
              }
              The code i've posted above will be deleted after a few minutes by the moderator... Please, read the book carefully next time...

              Try to make have some experiments... Later you will come-up an idea...

              Worrying
              Sukatoa.
              Thank You. I now see. So first comes the space and then the input, along with the corresponding amount of stars. The input is then decreased by one and a new line is then printed whereby the decreased amount of stars follows the same process until it reaches 1. At least I think I've now got it.

              Comment

              • sukatoa
                Contributor
                • Nov 2007
                • 539

                #8
                Originally posted by NoviceJava
                Thank You. I now see. So first comes the space and then the input, along with the corresponding amount of stars. The input is then decreased by one and a new line is then printed whereby the decreased amount of stars follows the same process until it reaches 1. At least I think I've now got it.
                Nice flow...

                No probz.. ;-)

                Sukatoa...

                Comment

                Working...