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 01-30-2008, 09:07 AM
Member
 
Join Date: Jan 2008
Posts: 16
banie is on a distinguished road
Making triangle
Hi,

I just wanna do triangle. The output is like this:

@
@@
@@@
@@@@
@@@@@
@@@@@@
@@@@@@@
@@@@@@@

Here is my code:

Code:
public class Banie { private final int height; private final int width; public Banie(final int height, final int width) { // Constructor this.height = height; this.width = width; } public void print() { for (int line = 0; line < height; line++) { final double percentage = line / (double) height; final int item = (int) (percentage * width) + 1; // Type casting for (int at = 0; at < item; at++) { System.out.print("@"); } System.out.println(); if ( height == item); } } public static void main(String[] args) { Banie t = new Banie(7, 7); // Parsing args t.print(); // Calling method } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-30-2008, 09:49 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 323
tim is on a distinguished road
Okay
Hello banie

I indented and tested your code and it looks file:
Code:
public class Banie { private final int height; private final int width; public Banie(final int height, final int width) { // Constructor this.height = height; this.width = width; } public void print() { for (int line = 0; line < height; line++) { final double percentage = line / (double) height; final int item = (int) (percentage * width) + 1; // Type casting for (int at = 0; at < item; at++) { System.out.print("@"); } System.out.println(); if ( height == item); } } }
It gives output
Code:
@ @@ @@@ @@@@ @@@@@ @@@@@@ @@@@@@@
Is this what you wanted? Or, do you need:
Code:
@ @@@ @@@@@ @@@@@@@
__________________
If your ship has not come in yet then build a lighthouse.

Last edited by tim : 01-30-2008 at 09:57 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-02-2008, 09:27 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 769
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
banie, this topic was discussed recently... you may wish to use the search feature next time. When you use the search, compare your code to the code you find from others. This helps to strengthen your coding "eyes" and reasoning ability/logic skills.
__________________

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 27, 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
  #4 (permalink)  
Old 02-02-2008, 12:07 PM
Member
 
Join Date: Jan 2008
Posts: 16
banie is on a distinguished road
The output that i want is like this:

@
@@
@@@
@@@@
@@@@@
@@@@@@
@@@@@@@
@@@@@@@
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-02-2008, 12:23 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 769
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by banie View Post
The output that i want is like this:

@
@@
@@@
@@@@
@@@@@
@@@@@@
@@@@@@@
@@@@@@@
So what exactly is the problem? tim provided you with an easy solution, now just add that extra line in and you've got that output.
__________________

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 27, 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
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
change the square to triangle java anotsu New To Java 2 02-06-2008 08:21 PM
Help with making this algorithm better RLRExtra New To Java 6 01-17-2008 05:11 PM
Triangle jkswebsite New To Java 6 01-14-2008 04:33 AM
Making a session in swing sandor AWT / Swing 3 04-22-2007 11:58 PM


All times are GMT +3. The time now is 05:37 AM.


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