Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Asterisk Graph

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Asterisk Graph

    Hi There,

    I have a question that I can't seem to solve on my own.

    I'm supposed to create a graph using asterisks' that follows the month of October for a store. The point is that using random numbers between 10000-40000 it is supposed to make a graph using 1 asterisk for every 1000 in the random number. Here is were it gets harder. On Sunday the store is closed. On Monday the store always earns over 30000. On Tue the store always earns over 20000. And Saturdays the store always earns less than 15000. Here is what I have so far.
    import java.util.*;
    import java.math.*;
    import java.util.Random;
    class ChartingSales
    {
       public static void main(String args[])
       {
           System.out.println("Day\tDaily\tSales Graph");
           RegDay();
        }  
     
     
         static void RegDay()
         {
           Random rand = new Random();
           int Reg = rand.nextInt(40000-10000) + 10000;  
           int Mon = rand.nextInt(40000-30000) + 30000;
           int Tue = rand.nextInt(40000-20000) + 20000;  
           int Sat = rand.nextInt(15000-10000) + 10000;  
           int i = 0;  
           while(i < 31)
           {
               i++;
               if(i == 1 || i == 8 || i == 15 || i == 22 || i == 29)
               { 
                 System.out.println("");  
                }
     
                else if(i == 2 || i == 9 || i == 16 || i == 23 || i == 30)
               {   
                 System.out.println(i+"\t"+Mon+"\t");  
                for(int j = 0; j<Mon;j=j+1000);
                {
                    System.out.print("*");
                }
                }
                 else if(i == 3 || i == 10 || i == 17 || i == 24 || i == 31)
               { 
                 System.out.println(i+"\t"+Tue+"\t"); 
                for(int j = 0; j<Tue;j=j+1000);
                {
                    System.out.print("*");
                }             
                }
                 else if(i == 6 || i == 13 || i == 20 || i == 27)
               { 
                 System.out.println(i+"\t"+Sat+"\t"); 
                for(int j = 0; j<Sat;j=j+1000);
                {
                    System.out.print("*");
                }             
                }            
                 else
                {
                  System.out.println(i+"\t"+Reg+"\t");
                for(int j = 0; j<Reg;j=j+1000);
                {
                    System.out.print("*");
                }              
                }
            }         
         }
    }

    The output code should look like this(Since random numbers are used the numbers will vary.):
    Day Daily Sales Graph
    2 37081 *************************************
    3 28355 ****************************
    4 39158 ***************************************
    5 24904 ************************
    6 28879 ****************************
    7 13348 *************

    9 38791 **************************************
    10 32564 ********************************
    11 23867 ***********************
    12 18154 ******************
    13 25830 ***********************
    14 14092 **************

    16 36861 ************************************
    17 26207 ************************
    18 10921 **********
    19 16573 ****************
    20 33423 *********************************
    21 12766 ************

    23 33770 *********************************
    24 28823 **************************
    25 38883 **************************************
    26 20959 ******************
    27 16262 ****************
    28 13269 *************

    30 33557 *********************************
    31 22579 **********************

    Help would be amazing!!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: Asterisk Graph

    What does the program's output look like now? Please post the program's current output and add some comments saying what is wrong with it.

    Since random numbers are used the numbers will vary.)
    The random number series can be made the same for each new execution by using a seed arg to the Random(12345) constructor.


    The code needs to be properly formatted using indentations of 3-4 spaces to show nested logic. Too many statements start in almost the same column making the code hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Asterisk Graph

    The current output looks like this:

    Day Daily Sales Graph

    2 37944
    *3 36443
    *4 23057
    *5 23057
    *6 12651
    *7 23057
    *
    9 37944
    *10 36443
    *11 23057
    *12 23057
    *13 12651
    *14 23057
    *
    16 37944
    *17 36443
    *18 23057
    *19 23057
    *20 12651
    *21 23057
    *
    23 37944
    *24 36443
    *25 23057
    *26 23057
    *27 12651
    *28 23057
    *
    30 37944
    *31 36443
    *

    As you can see the Asterisks don't align where they are supposed to be and for some reason they aren't being displayed enough times. Another problem I have is to get the numbers random for every day. I created a variable to store the random numbers but I need new numbers for each day.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: Asterisk Graph

    30 37944
    *31 36443
    *
    It looks like there is a newline output after the DailySales number and before the *. Check the calls to the printing methods to see where the newline is coming from.
    Also why is the day number printed on the same line following the *? Shouldn't it be at the start of a line?

    they aren't being displayed enough times.
    They are all displayed ONLY ONE TIME. No lines have more than one *.
    Try debugging the code by printing out the value of the loop control variable (j) inside one of the loops to see how its value is changing and why the code in the loop doesn't execute more than once.

    Suggestion: make a method that prints *s on a line and pass it the number of *s to print instead of having so many separate loops to print the *s
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Checkerboard (asterisk pattern) problem
    By jjb1989 in forum Loops & Control Statements
    Replies: 18
    Last Post: September 8th, 2013, 08:52 PM
  2. help with loop to print an equliateral asterisk triangle
    By everyone0 in forum Loops & Control Statements
    Replies: 13
    Last Post: October 11th, 2012, 12:37 PM
  3. Please help! Nested while loops and asterisk triangles!
    By rockout341 in forum Loops & Control Statements
    Replies: 12
    Last Post: September 15th, 2011, 11:50 AM
  4. Java Bar graph takes user input to create graph?
    By kenjiro310 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:37 AM
  5. asterisk forming diamond
    By cutee_eyeh in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 13th, 2010, 08:53 PM