Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

help drawing triangle

Thread view: 
jinto12@gmail.com - 08 Nov 2005 04:35 GMT
Hi,

i need to draw a empty triangle using only characters such as (*), the
thing is i need help figuring out the right side of the triangle.

public class Subclass {

    public static void main (String args[]){

        int counter=1;
        int base=15;

        while(counter<=(base/2)){
            System.out.print(" ");

            System.out.print("\n ");

                for(int i= counter; i<=((base/2)-1);i++){//prints n-1 spaces
                    System.out.print(" ");

                }

            System.out.print("*");

                for(int m=1; m<=((counter*2)-3); m++){//problem with this loop,
need help
                    System.out.print(" ");
                }

            System.out.print("*");
            counter++;
        }

            System.out.print("\n");

            for(int a=1; a<=base ;a++){System.out.print("*");}//base of the
triangle
           
           
           
       
       
    }   
       
           
}
Benji - 08 Nov 2005 04:42 GMT
> i need to draw a empty triangle using only characters such as (*), the
> thing is i need help figuring out the right side of the triangle.

Hi,

Is this your homework?  If it is, posting your code to a newsgroup and
asking someone to fix it is inappropriate.

Signature

Of making better designs there is no end,
 and much refactoring wearies the body.

Benji - 08 Nov 2005 05:15 GMT
>> i need to draw a empty triangle using only characters such as (*), the
>> thing is i need help figuring out the right side of the triangle.

> Is this your homework?  If it is, posting your code to a newsgroup and
> asking someone to fix it is inappropriate.

Well, I'm going to assume your answer is yes, since you're posting from
your dorm room at Cal Poly Pomona.

Go ask your TA, or your professor.

Signature

Of making better designs there is no end,
 and much refactoring wearies the body.

jinto12@gmail.com - 08 Nov 2005 05:19 GMT
> > i need to draw a empty triangle using only characters such as (*), the
> > thing is i need help figuring out the right side of the triangle.
[quoted text clipped - 7 lines]
> Of making better designs there is no end,
>   and much refactoring wearies the body.

sorry about that i just posted the code to see if anybody can give any
suggestions.
Benji - 08 Nov 2005 05:28 GMT
> sorry about that i just posted the code to see if anybody can give any
> suggestions.

the problem is that unless you tell us what CS program you're in, and
what the ethic codes are, we don't know how we can help you without
violating the rules of your school.

Signature

Of making better designs there is no end,
 and much refactoring wearies the body.

Andrew Thompson - 08 Nov 2005 05:33 GMT
>>i need to draw a empty triangle using only characters such as (*), the
>>thing is i need help figuring out the right side of the triangle.
..
> Is this your homework?  If it is, posting your code to a newsgroup and
> asking someone to fix it is inappropriate.

Some people (myself included) feel it is perfectly fine
to post homework questions, verbatim, so long as the OP
is merely doing it as background information to lead into
a specific technical question.

I feel there is a huge difference between posts that
might both start with a 'copy/paste' of the question,
but end with ..'can you do my homework?' as opposed to
'I cannot understand why it throws an NPE at line 34.
Where am I going wrong?'/'The compiler is reporting an..
How/where do I investigate this further?'

[ FWIF - I do not know which of the above possibilities
is the most accurate representation of this thread.  ..and
perhaps I read too much into your comment - I suppose it
depends on the exact level of 'fix', but I feel the OP
might have simply been asking for general direction
and advice, rather than a 'paint by numbers' description. ]
Benji - 08 Nov 2005 05:40 GMT
> Some people (myself included) feel it is perfectly fine
> to post homework questions, verbatim, so long as the OP
> is merely doing it as background information to lead into
> a specific technical question.

I agree with this.

> [ FWIF - I do not know which of the above possibilities
> is the most accurate representation of this thread.  ..and
> perhaps I read too much into your comment - I suppose it
> depends on the exact level of 'fix', but I feel the OP
> might have simply been asking for general direction
> and advice, rather than a 'paint by numbers' description. ]

Well, I think that at very least, he should say that it's his
homework.  But not only that, he just said what it was supposed
to do, and pasted the code.  So, it was essentially what you
said - "please write my code for me"

At least at my school (up until a few years ago when we
decided that copying code was "collaboration"), this method
of asking for homework help was considered academic misconduct.

Signature

Of making better designs there is no end,
 and much refactoring wearies the body.

Andrew Thompson - 08 Nov 2005 06:01 GMT
> At least at my school (up until a few years ago when we
> decided that copying code was "collaboration"), this method
> of asking for homework help was considered academic misconduct.

Good point.  It pays to be mindful of the rules of your college.
<http://groups.google.com/group/comp.lang.java.programmer/msg/433532584ad191a8>
[ 'Robocop' cared not one bit, what the rules were.. ]
Thomas Hawtin - 08 Nov 2005 07:32 GMT
> i need to draw a empty triangle using only characters such as (*), the
> thing is i need help figuring out the right side of the triangle.
[quoted text clipped - 4 lines]
>
>         int counter=1;

Minor point, but it tends to be easier to start from 0 and work in a
half-closed interval, from 0 to an exclusive upper limit.

>         int base=15;

You might as well make base final, to show your intent not to change it.

>         while(counter<=(base/2)){

This could be a for loop.

>             System.out.print(" ");
>
>             System.out.print("\n ");
>
>                 for(int i= counter; i<=((base/2)-1);i++){//prints n-1 spaces

You will probably find it easier to work out how many spaces you want
first. Then do a bog standard for (int i=0; i<num; ++i).

>                     System.out.print(" ");
>
[quoted text clipped - 4 lines]
>                 for(int m=1; m<=((counter*2)-3); m++){//problem with this loop,
> need help

i is out of scope here, so you don't have to play with the loop index names.

If you had worked out how many spaces you needed outside the loop, this
should be clearer. You can then print the number of spaces as an integer
to ease debugging.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

zero - 08 Nov 2005 12:59 GMT
>> i need to draw a empty triangle using only characters such as (*),
>> the thing is i need help figuring out the right side of the triangle.
[quoted text clipped - 7 lines]
> Minor point, but it tends to be easier to start from 0 and work in a
> half-closed interval, from 0 to an exclusive upper limit.

Some beginner books suggest using a 1-based counter because that is more
"natural" and can avoid off-by-one errors.  Sometimes these texts even give
arrays an extra element, and keep index 0 unused.

Personally I agree with you, mostly for performance reasons (which are not
that important for beginners) but also because the beginner will be more
accustomed to the 0-based counts, which are so prevalent in CS, from the
very start.
Roedy Green - 08 Nov 2005 13:33 GMT
>Some beginner books suggest using a 1-based counter because that is more
>"natural" and can avoid off-by-one errors.  Sometimes these texts even give
>arrays an extra element, and keep index 0 unused.

I think more because FORTRAN, PL/1 and Pascal were usually 1 based.

I don't think you should teach bad habits.

The sooner you start thinking of a loop as a gestalt the sooner you
will be able to write code quickly.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 09 Nov 2005 22:42 GMT
> Some beginner books suggest using a 1-based counter because that is more
> "natural" and can avoid off-by-one errors.  Sometimes these texts even
> give
> arrays an extra element, and keep index 0 unused.

   This is, IMHO, bad. If you write code like this, the next person who
comes along will see your code, and wonder why you're not using index 0.
They won't know if it's okay to refactor the code to use 0 or not. Is 0 some
sort of special value that musn't be touched? Is this a bug and 0 should be
used? And if we start using 0, does that mean we should decrement the upper
bound, or leave it as it is?

   Some languages use 0 based sequences (e.g. Java), and other languages
use 1 based sequences (e.g. XQuery). For my job, I have to switch back and
forth between these languages several times a day. Learn to adapt to
whatever the conventions are for the language you're working with.

   - Oliver
zero - 10 Nov 2005 14:50 GMT
>> Some beginner books suggest using a 1-based counter because that is
>> more "natural" and can avoid off-by-one errors.  Sometimes these
[quoted text clipped - 16 lines]
>
>     - Oliver

I don't know if I was clear enough about where I fit in.  I too think using
1-based structures is Java is a bad idea.  I was merely pointing out that
some beginner books suggest it.  They have reasons for doing so, but IMO
those don't weigh up to the reasons for not doing it.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.