Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-09-2012, 04:35 AM   PM User | #1
itsalltech
New Coder

 
Join Date: Mar 2010
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
itsalltech is an unknown quantity at this point
Drawing figure using nested for loops

Write a Java program in a class named SlashFigure that produces the following output. Use nested for loops to capture the structure of the figure.

!!!!!!!!!!!!!!!!!!!!!!
\\!!!!!!!!!!!!!!!!!!//
\\\\!!!!!!!!!!!!!!////
\\\\\\!!!!!!!!!!//////
\\\\\\\\!!!!!!////////
\\\\\\\\\\!!//////////


Could anyone help me out with this? Thank you.
itsalltech is offline   Reply With Quote
Old 07-09-2012, 06:56 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,659
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
What's the code you currently have?
Fou-Lu is offline   Reply With Quote
Old 07-09-2012, 04:59 PM   PM User | #3
itsalltech
New Coder

 
Join Date: Mar 2010
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
itsalltech is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
What's the code you currently have?
Code:
for (int i = 0; i < 6; i++) {
 for (int j = 0; j < i*2; j++) {
  System.out.print("\\");
 }
 for (int j = 0; j < 22 - (i*2); j++) {
  System.out.print("!");
 }
 for (int j = 0; j < i*2; j++) {
  System.out.print("/");
 }
 System.out.print("");
}
itsalltech is offline   Reply With Quote
Old 07-09-2012, 05:17 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,659
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Okay, not bad. First replace that last out call with System.out.println();. All you need is a linefeed there, and then it'll place each iteration on a line of its own.

The reduction is still off. Its the counter though, given what you have you need to subtract 22 - (i * 4) since you reduce by four during each iteration (two from the left, and two from the right). That should give you the correct result.

A future modification you could consider is handling this mathematically instead. So instead I want to filter down to nothing in only 4 rows instead of 6 rows. How many would I have to subtract per line in order to get to this result? And same idea with columns, I want to show 50 now and filter it down using only 5, so how many would I subtract from there. And so forth. So keep that in mind for a potential future modification for it.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
itsalltech (07-11-2012)
Old 07-11-2012, 08:07 PM   PM User | #5
itsalltech
New Coder

 
Join Date: Mar 2010
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
itsalltech is an unknown quantity at this point
Thank you! It worked! We've been given the next assignment, which asks us to modify the code. Could you help with this too? Thank you!

Modify the SlashFigure program from the previous exercise to produce a new program SlashFigure2 that uses a global constant for the figure's height. The previous output used a constant height of 6. Here is the output for a constant height of 4:

size 4

!!!!!!!!!!!!!!
\\!!!!!!!!!!//
\\\\!!!!!!////
\\\\\\!!//////
itsalltech is offline   Reply With Quote
Old 07-11-2012, 10:31 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,659
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This is precisely what I was referring to for filtering down mathematically.

Calculate the difference between the two resulting diagrams. Take note of how many !'s are in the first and last line, and find the common ratio to that of the number of !'s in the last line. That can be reversed to calculate how many are required for the first line (and the replacement value for 22).
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:48 PM.


Advertisement
Log in to turn off these ads.