Paul,John,Adam,Liz, at al.,.
Thank you all for your individual help and input into trying to
helping me. Sorry, all I needed was one sentence:"Post your code that
is not working". Instead, I was subjected to all sorts of unfair
stereotypes. But, I perfectly understand where you are coming from,
and I appreciate that.
Anyway, I tested John and Paul's codes in a main method and
they worked perfectly,even thogh Paul intentionally meant to confuse
me.
However, I have two main issues...
(1)I STILL want HELP to implement the method with one of my OWN
UNFINISHED codes below and
(2) I would like you to help me understand your code with clear
commenting if possible..., especially, does a "-" sign in front of an
int variable imply ("opposite direction")? Also, what do the symbols
?,:,etc mean in Java? I have a general understanding of the logical
operators, or, and, xor etc but not the ?,: and the rest
(1). Here's one of my many attempts that I have struggled with for
days:
int n=9,allrows=n, allcols=n, i=1, j=1,totalstars=1,starlimit=1;
String s ="*";
while(i < allrows+1 )//loop for making rows
{
while(j < allcols+1)// loop for making columns.
{
while((j >=(((allrows+1)/2)-i)) && (totalstars < starlimit+1))//
create top stars
{
if(i=((n+1)/2)+1) break;// if it gets to 1 more than half total
rows, stop making top stars.
System.out.print(s); // otherwise, print stars for top triangle.
j++
}
//I need help with some code here to implement the stars that form
the
// bottom triangle if everything else is correct
}
System.out.println("\n");
i++;
starlimit+=2;
totalstars+=2;
}
Please feel free to point out approaches I need to stay away from and
those i need to work harder on..etc
Also, is it advisable, if I attempted such problems with just two
whiles and if-else clauses?
Again, if I wanted to approach such a problem with first assumming
that all rows and colums are turned on (filled with 1's),i.e a
complete 2D-matrix of 1's,.. is it possible to somehow combine logical
shifts or mask( like ANDing)or some other logical operators to turn
some of the 1's off to 0's to form the shape and then print a star in
places where there are only 0's?
I guess I can think of many approaches but not knowing Java is my
real problem.
I would appreciate as much help and pointers to as many helpful
resources as possible.
Thanks,
Jay Dean.
arne thormodsen said:
Yes, I agree. Yours is an example of a perfectly good working solution that
no sane student would dare turn in, because he might have to try to explain
it.
I sense an "obsfuscated code" contest here... ;-)
You might try replacing the "/2" operations with some bit-shifting
logic to start with.
Also, rather than use the ternary operator to decide on the character
to print, why not calculate it's UNICODE value from some formula based
on the index values?
If I wasn't already occupied this could be fun!
--arne