Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-06-2008, 12:06 AM
Member
 
Join Date: Feb 2008
Posts: 10
anotsu is on a distinguished road
change the square to triangle java
hello i have to change the code of a program that draw a square with stars to a triangle square , can someone help me out please ?

here is the code into 2 files :

first files --->

import java.util.Scanner;

public class Hollow
{
// draw a hollow box surrounded by stars
public void drawHollowBox()
{
Scanner input = new Scanner( System.in );

int stars; // number of stars on a side
int column; // the current column of the square being printed
int row = 1; // the current row of the square being printed

// prompt and read the length of the side from the user
System.out.print( "Enter length of side:" );
stars = input.nextInt();

if ( stars < 1 )
{
stars = 1;
System.out.println( "Invalid Input\nUsing default value 1" );
} // end if
else if ( stars > 20 )
{
stars = 20;
System.out.println( "Invalid Input\nUsing default value 20" );
} // end else if

// repeat for as many rows as the user entered
while ( row <= stars )
{
column = 1;

// and for as many columns as rows
while ( column <= stars )
{
if ( row == 1 )
System.out.print( "*" );
else if ( row == stars )
System.out.print( "*" );
else if ( column == 1 )
System.out.print( "*" );
else if ( column == stars )
System.out.print( "*" );
else
System.out.print( " " );

column++;
} // end inner while loop

System.out.println();
row++;
} // end outer while loop
} // end method main
} // end class Hollow



second file ----->

public class HollowTest
{
public static void main( String args[] )
{
Hollow application = new Hollow();
application.drawHollowBox();
} // end main
} // end class HollowTest



thank you very much
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-06-2008, 07:25 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 750
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
You didn't really explain the specifics of the triangle, so this first example here is a left aligned triangle. I formatted it my way since you didn't properly format the post - please use code tags when posting code. There's many ways to do it... and I fancy for loops in case you didn't notice.


Replace your two while loops with either of the below snippets of code:
Code:
... for (i = 0; i < stars; i++) { for (j = 0; j <= i; j++) { System.out.print("*"); } System.out.println(); } ...
Since your square was hollow, here's a hollow triangle - also left aligned:
Code:
... for (i = 0; i <= stars; i++) { for (j = 0; j <= i; j++) { if (i == stars) { for (int x = 0; x<= stars; x++) System.out.print("*"); break; } else if (i > 1) { System.out.print("*"); for (int k = 0; k < i - 1; k++) System.out.print(" "); System.out.print("*"); break; } else System.out.print("*"); } System.out.println(); } ...
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on July 13, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-06-2008, 08:21 PM
Member
 
Join Date: Feb 2008
Posts: 10
anotsu is on a distinguished road
thank you pal !!!! u rock
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Computing the area of a triangle using Heron's Formula Java Tip java.lang 0 04-12-2008 09:39 PM
assignement change the java screensaver anotsu New To Java 2 03-07-2008 01:28 AM
Making triangle banie New To Java 4 02-02-2008 12:23 PM
How to change the default Java integrator New To Java 4 02-01-2008 04:19 PM
Triangle jkswebsite New To Java 6 01-14-2008 04:33 AM


All times are GMT +3. The time now is 04:24 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org