Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Stare

I want to  Write a complete Java program that will display the following three lines when it is run:

      *
   * * *
* * * * *


(I wrote this  )

public class Stars
{
      public static void main (String[] args)
   {
      final int MAX_ROWS = 10;

      for (int row = 1; row <= MAX_ROWS; row++)
      {
         for (int star = 1; star <= row; star++)
            System.out.print ("*");

         System.out.println();
      }
   }
}
 
but give me
*
**
***

(Is any body know how to fix it)
0
Ansary100
Asked:
Ansary100
  • 5
  • 5
  • 4
  • +1
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
Obviously this is a homework, so I will give you some tips.
I HOPE NOBODY GIVES YOU THE FULL ANSWER
Just have to analyze a bit:

public class Stars
{
      public static void main (String[] args)
   {
      final int MAX_ROWS = 10;

      for (int row = 1; row <= MAX_ROWS; row++)   // This is OK
      {
         // You need some spaces here: 4, 2, 0, that is (MAX_ROWS-1)*2, and decreasing by 2, put another for() here
         for (int star = 1; star <= row; star++)   // This will give you 1,2,3... But you need 1,3,5 so star must increase two times
            System.out.print ("*"); //You need to print a space after every star

         System.out.println();
      }
   }
}
0
 
venkateshwarrCommented:
try this...

public class Stars
{
    public static void main (String[] args)
   {
      final int MAX_ROWS = 10;
      int centerspace = (MAX_ROWS+1)/2;

      for (int row = 1; row <= MAX_ROWS; row++)
      {
         for (int space = 1; space <= centerspace ; space++)
            System.out.println(" ");
         
         centerspace--;

         for (int star = 1; star <= row; star+=2)
            System.out.print ("*");
     }
   }
}
0
 
Jaime OlivaresSoftware ArchitectCommented:
venkateshwarr,
Appear like you don't want to respect EE rules about homeworks.
Although you had the intention to ask full answer (violating EE rules), it is incomplete.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
venkateshwarrCommented:
Sorry for the above posting. I was a bit hasty about giving the solution...
Anyway it does not work...

But jamies logic will not work..

public class Stars
{
    public static void main (String[] args)
   {
      final int MAX_ROWS = 20;
      int centerspace = MAX_ROWS;

      for (int row = 1; row <= MAX_ROWS; row++)
      {
          // Print all the spaces till from 1 to centerspace
          // decrease the centerspace value

         //  for the following iteation you should double the row value and decrement it by one  
         for (int star = 1; star <= row; star++)
            System.out.print ("*");
         
        System.out.println();
      }
   }
}
0
 
venkateshwarrCommented:
I request the page editor to remove my first posting.
0
 
venkateshwarrCommented:
as soon as possible
0
 
Jaime OlivaresSoftware ArchitectCommented:
>I request the page editor to remove my first posting.
>as soon as possible
Do you think that's possible? Saturday?
Anyway author has received your post.
0
 
Ansary100Author Commented:
Dear jaime_olivares

Why you are imagining that I’m a student I’m NOT A STUDENT IN ANY PLACE. I’m a self learner. Please stop saying I’m a student.. I’m maybe a student of you and the others because I’m trying to learn from you and the others. If you feel comfortable not helping me, please remove all your answers. I hop that there will be others to help me learn something. I hope you understand
0
 
Jaime OlivaresSoftware ArchitectCommented:
> If you feel comfortable not helping me, please remove all your answers. I hop that there will be others to help me learn
> something. I hope you understand
The fact is, there are specific rules for EE homeworks, if author doesn't clarify it is not a homework, then I have to assume it is if I suspect, and not giving a full answer, because I can be violating EE rules (trust me, I have faced this problem DOZENS of times).
So, anyway, if this is not a homework and you are a self student, then giving you the full answer WILL NOT HELP YOU TO LEARN AND SPECIALLY TO CLEARLY UNDERSTAND.
I hope you understand me now.


0
 
Jaime OlivaresSoftware ArchitectCommented:
BTW,

>I want to  Write a complete Java program that will display the following three lines when it is run:
>      *
>   * * *
>* * * * *

Is typical student homework question, like "draw a circle", trapezoid, diamond, etc..... That's why I assummed it is.
0
 
venkateshwarrCommented:
>Anyway author has received your post.
I dont want any points for this question....
0
 
InteractiveMindCommented:
Ansary,

the question with outputting the stars in that formation (like creating a tree), is a very common Java Homework question, but if you say that it's not, then I believe you.

Either way, here's a working solution that I've also tested to be 100% sure; I'm also going to explain how it works .. because clearly you can program simple things like this, the problem that you're having with it though, is the maths/logic; so I shall cover that below..


// Stars.java

public class Stars
{
    public static void main( String [] args )
    {
        final int MAX_ROWS = 10;    // Number of rows.
        char toDisplay = '*';            // The character to output.
       
        for ( int i = 1; i <= MAX_ROWS; i+=2 )
        {
            for ( int a = 0; a < (MAX_ROWS-i)/2; a++ )    // Output the spaces.
                System.out.print( " " );                              //
           
            for ( int b = 0; b < i; b++ )                            // Output the characters.
                System.out.print( toDisplay );                     //
           
            System.out.println();                                     // Clear the line.
        }
    }
}


** EXPLANATION **

Okay, here's the output that you want:

    *
  ***
*****

Let's write that as a sequence  (in terms of the number of stars):

 1, 3, 5, ..

We can clearly see that this sequence goes up in two's. ie:

  1  +2 = 3
  3  +2 = 5
  5  +2 = ..

Therefore, we know that we need to increment by 2 (starting from a value of 1):

  for ( int i = 0; i <= MAX_ROWS; i+=2 )
  {
      ...
  }

That's not too hard is it? :o)

Now, We need to output the number of spaces first. So, we need to find a connection between the number of spaces, and the number of stars to be outputted:

Here's the number of spaces, when MAX_ROWS = 5:

  Number of Stars (s) |   Number of Spaces (sp)
  ------------------------------------------------------
  1                            |   2
  3                            |   1
  5                            |   0

Now, I can see from this pattern, that the following applies:

    sp = (5 - s) / 2

Try it:

    sp = (5 - 1) / 2 = 4 / 2 = 2
    sp = (3 - 1) / 2 = 2 / 2 = 1
    sp = (1 - 1) / 2 = 0 / 2 = 0

All of these are correct! :-)

Okay, then, we know how many stars we need to output, it will be 'i', in the for loop..


Put it all together; does that make much sense?

Regards;
Rob.
0
 
InteractiveMindCommented:
Sorry, the for loop, in the explanation that I've given should define i as 1 initially, like so:

   for ( int i = 1; i <= MAX_ROWS; i+=2 )
0
 
InteractiveMindCommented:
Also ... lol ... although the examples that I gave are valid, they don't comply with the equation that I came up with, so perhaps ignore these examples:

    sp = (5 - 1) / 2 = 4 / 2 = 2
    sp = (3 - 1) / 2 = 2 / 2 = 1
    sp = (1 - 1) / 2 = 0 / 2 = 0

and take note of these:

   sp = (5 - 1) / 2 = 4 / 2 = 2
   sp = (5 - 3) / 2 = 2 / 2 = 1
   sp = (5 - 5) / 2 = 0 / 2 = 0

..Gives the same answers. :-)
0
 
Ansary100Author Commented:
Thank you very much Now I understand the answer very well
0
 
InteractiveMindCommented:
Glad I could help :-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now