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 11 of 11

Thread: HourGlass [Due tonight need help!]

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HourGlass [Due tonight need help!]

    public class DrawHourGlass {
     
       public static void main(String[] args) {
     
          System.out.println("An hourglass of size 4:");
          drawHourGlass(4);      // draw an hourglass of size 4
          printChars('\n', 2);   // skip two lines by printing two newline chars
     
          System.out.println("An hourglass of size 20:");
          drawHourGlass(20);       // draw an hourglass of size 20
       }
     
     
       //Draws the hourglass related to the size.
       public static void drawHourGlass(int size) {
     
          drawBorder(size);       // draw top line 
          drawTopHalf(size);      // draw top half
          drawBottomHalf(size);   // draw bottom half 
          drawBorder(size);       // draw bottom line
       }
     
     
       //Outside of the hourglass.
       public static void drawBorder(int size) {
     
          System.out.print('|');
          printChars('\"', 2*size);     // print 2*size dashes
          System.out.print('|');
          System.out.println();
       }
     
     
       //Top half of hourglass.
       public static void drawTopHalf(int size) {
     
          for (int k=1; k <= size; k = k+1) {
             printChars(' ', k);          // print k-1 spaces
             System.out.print('\\');        // print a backslash
             printChars(':', 2*(size-k));   // print 2*size-2k dots
             System.out.print('/');         // print a slash
             System.out.println();          // skip to next line
          }
       }
     
     
       //Bottom half of hourglass.
       public static void drawBottomHalf(int size) {
     
          for (int k=1; k <= size; k = k+1) {
             printChars(' ', size-k);       // print size-k spaces
             System.out.print('/');         // print a slash
             printChars(':', 2*(k));      // print 2*k-2
             System.out.print('\\');        // print a backslash
             printChars(' ', size-k);       // print size-k spaces
             System.out.println();          // skip to next line
          }
       }
       //Prints the amount of charcter and numbers input.
       public static void printChars(char m, int n)
       {
          for (int i=1; i <= n; i = i+1) { System.out.print(m); } //Prints in invidual line.
       }
     
    }

    I have too many methods. I don't have a constant.
    ... I'm not sure if I'm allowed to use printChars either.

    2hnt1s0.jpg


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: HourGlass [Due tonight need help!]

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Next time, start earlier and come here when you need help. Don't wait to the last minute.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HourGlass [Due tonight need help!]

    Thanks...

    If anyone can help me that'd be cool.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HourGlass [Due tonight need help!]

    I have too many methods. I don't have a constant.
    That's not strictly a problem.

    Does the code produce the desired output?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: HourGlass [Due tonight need help!]

    By the way, you should read the requirements again. You are going to lose points on technicalities.

  6. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HourGlass [Due tonight need help!]

    I could not get it to print | | in the middle instead of \/.
    But everything else prints how it asks for.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HourGlass [Due tonight need help!]

    I could not get it to print | |
    Where is the code that is supposed to print the | |?
    Is it being executed?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: HourGlass [Due tonight need help!]

    I threw something together but it doesn't line up perfectly and its just a patch. This might put you on the right track...

     //Top half of hourglass.
    	   public static void drawTopHalf(int size) {
    		   int count=0;
    	      for (int k=1; k <= size; k = k+1,count++) 
    	      {
    	    	  if(count<19)
    	    	  {
    	         printChars(' ', k);          // print k-1 spaces
    	         System.out.print('\\');        // print a backslash
    	         printChars(':', 2*(size-k));   // print 2*size-2k dots
    	         System.out.print('/');         // print a slash
    	         System.out.println();          // skip to next line
    	    	  }
    	      }
    	      String space = String.format("%"+ 23 +"s"," | | ");
    	      System.out.println(space);
     
    	   }

  9. #9
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hourglass.

    //Makes bottom half of hourglass.
        public static void bottom(int rows) {
            for (int j = 1; j < rows; j++) {
                printChars(' ', rows - j);       
                System.out.print('/');         
                printChars(':', 2 * (j));      
                System.out.print('\\');       
                printChars(' ', rows - j);      
                System.out.println();

    If I wanted to make my printChars call method to a for a loop, how would I do it?

  10. #10
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Hourglass.

    This is the same question you posted a few hours ago. Please continue using that same thread...

  11. The Following User Says Thank You to jocdrew21 For This Useful Post:

    GregBrannon (October 1st, 2014)

  12. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: HourGlass [Due tonight need help!]

    Threads merged.

    Please do not post multiple threads on the same topic. It divides attention rather than attracts it. If you do not feel the original thread is getting attention, then add new details, results, or any info to the original to raise it to the top of the "New Post" stack.

Similar Threads

  1. Replies: 3
    Last Post: November 8th, 2013, 06:09 PM
  2. Project Nursing Home Due Tonight Help
    By morrism35 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 16th, 2013, 07:17 PM
  3. Using FOR and WHILE loops to find average (DUE TONIGHT)
    By cbh793 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 1st, 2013, 08:56 PM
  4. Replies: 1
    Last Post: December 5th, 2012, 10:58 PM
  5. Help On Java Homework (DUE TONIGHT!!!)
    By agmolina90 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 14th, 2011, 08:28 AM