Kindly find the fault in the code, as I'm not getting the requiredoutput - thanks

V

Vasu

Hi anybody there!

Following is the output what I'm getting vs what I want, and below
that is the code snippet I've created. Can anybody help me find the
root of problem / solution for it. Thanks in advance

Test.java : Outcome

What I'm getting:

**
****
******
********
**********
************
**************
****************
**
********************
******************
****************
**************
************
**********
********
******
****


What I need it to be:

**
****
******
********
**********
************
**************
****************
********************
******************
****************
**************
************
**********
********
******
****
**

Here is the code I've used to create this figure :

for(k=0; k<10; k++){
while(i<10)
{
System.out.print(" ");
i++;
}

i=k;

while(j>0){
System.out.print("*");
j-=1;
}

while(l>0){
System.out.print("*");
l-=1;
}
System.out.println();
j=k;
l=k;
}

for(k=0; k<10; k++){
while(i>0)
{
System.out.print(" ");
i--;
}
i=k;

while(l<10){
System.out.print("*");
l+=1;
}

while(j<10){
System.out.print("*");
j+=1;
}

System.out.println();
j=k;
l=k;
}
 
M

markspace

Vasu said:
Here is the code I've used to create this figure :

for(k=0; k<10; k++){
while(i<10)

well i is not initialized here, for starters.
{
System.out.print(" ");
i++;
}

i=k;

while(j>0){

Nor is j initialized.
System.out.print("*");
j-=1;
}

while(l>0){

Etc.


I didn't look more than that. Too many errors now to guess what is going
on. Also, you don't declare i,j,k or l anywhere. Next please post the
entire code, not just a snippet.
 
V

Vasu

Hi Markspace!

Thanks for the trouble you've taken. Here is the entire code :

class Test
{
public static void main(String[] test)
{
int i=0, j=0, k=0, l=0;

for(k=0; k<10; k++)
{
while(i<10)
{
System.out.print(" ");
i++;
}

i=k;

while(j>0)
{
System.out.print("*");

j-=1;
}

while(l>0)
{
System.out.print("*");
l-=1;
}
System.out.println();
j=k;
l=k;
}

for(k=0; k<10; k++)
{
while(i>0)
{
System.out.print(" ");
i--;
}
i=k;

while(l<10)
{
System.out.print("*");
l+=1;
}

while(j<10)
{
System.out.print("*");
j+=1;
}

System.out.println();
j=k;
l=k;
}
}
}
 
R

Roedy Green

Here is the code I've used to create this figure :

First indent your code.

Second don't reuse any variables for different purposes.

Use final wherever possible.

That will help clarify your code and make errors much easier to spot.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Out of 135 criminals, including robbers and rapists, 118 admitted that when they were children they burned, hanged and stabbed domestic animals."
~ Ogonyok Magazine 1979.
 
A

alexandre_paterson

Hi anybody there!

Following is the output what I'm getting vs what I want, and below
that is the code snippet I've created. Can anybody help me find the
root of problem / solution for it.

I've found the problem...

You need to remove all the lines and add this one:

for (int i = 0; i < 20; i++) System.out.println( " ".substring
(0,Math.abs(10-i)) + "********************".substring(0, (i >= 10 ? 20
- i : i )*2) );

:)

In case Usenet breaks that line:

http://pastebin.com/m151328f4

:)
 
L

Lord Zoltar

I've found the problem...

You need to remove all the lines and add this one:

for (int i = 0; i < 20; i++) System.out.println( "          "..substring
(0,Math.abs(10-i)) + "********************".substring(0, (i >= 10 ? 20
- i : i )*2) );

:)

In case Usenet breaks that line:

http://pastebin.com/m151328f4

:)

oooOOOOoooo! I LIKE this one! :)
 
M

markspace

Lord said:
oooOOOOoooo! I LIKE this one! :)


I like this one


System.out.println(
" **\n"
+" ****\n"
+" ******\n"
+" ********\n"
+" **********\n"
+" ************\n"
+" **************\n"
+" ****************\n"
+"********************\n"
+" ******************\n"
+" ****************\n"
+" **************\n"
+" ************\n"
+" **********\n"
+" ********\n"
+" ******\n"
+" ****\n"
+" **" );


One thing I just noticed as I did that: It starts with two asterisks,
not one, and the lines adjacent to the middle line have 18 asterisks,
where the middle line has 20. I.e., 19 asterisks is skipped. I don't
know if that a typo by the OP or not.
 
A

Arne Vajhøj

Roedy said:
Use final wherever possible.

That is not really the true Java way.

There are other languages that are better than Java if
ones intent is to code functionally.

Arne
 
L

Lew

That is not really the true Java way.

Actually, it is a best practice in Java to make methods and variables 'final'
whenever possible.
There are other languages that are better than Java if
ones intent is to code functionally.

What do you mean by this? How does it relate to the practice of using 'final'
when feasible?
 
A

Arne Vajhøj

Lew said:
Actually, it is a best practice in Java to make methods and variables
'final' whenever possible.

No it is not best practice.

The performance gains are just tales from the early days of JIT.

It can be useful to create good code. It can make sense to
use it for fields if you are trying to create an immutable object.
The guy with the same last name as you mention that usage in his
well known book. But for parameters and local variables it is just
fill.
What do you mean by this?

I mean that if you want to code functionally instead of imperative,
then you should pick a functional language instead of an imperative
language like Java.
How does it relate to the practice of using
'final' when feasible?

Using final everywhere is an attempt to use a functional style in Java.

Arne
 
L

Lew

No it is not best practice.

The performance gains are just tales from the early days of JIT.

It can be useful to create good code. It can make sense to
use it for fields if you are trying to create an immutable object.

That's what I was talking about.
The guy with the same last name as you mention that usage in his
well known book. But for parameters and local variables it is just
fill.

Sorry, I misspoke. I meant for members, not local variables or parameters. I
agree with you.
I mean that if you want to code functionally instead of imperative,
then you should pick a functional language instead of an imperative
language like Java.

Yeah, that much I did get.

Lew:
Using final everywhere is an attempt to use a functional style in Java.

I don't follow that reasoning at all. I think you need to connect the dots.
 
J

John B. Matthews

No it is not best practice.

The performance gains are just tales from the early days of JIT.

It can be useful to create good code. It can make sense to
use it for fields if you are trying to create an immutable object.

That's what I was talking about.
The guy with the same last name as you mention that usage in his
well known book. But for parameters and local variables it is just
fill.

Sorry, I misspoke. I meant for members, not local variables or parameters.
I
agree with you.
I mean that if you want to code functionally instead of imperative,
then you should pick a functional language instead of an imperative
language like Java.

Yeah, that much I did get.

Lew:
Using final everywhere is an attempt to use a functional style in Java.

I don't follow that reasoning at all. I think you need to connect the dots.[/QUOTE]

This example demonstrates the continuation-passing style seen in
functional programming. IIUC, the local variable named "parameter" must
be final in order to be accessible to the inner class:

<http://en.wikipedia.org/wiki/Continuation-passing_style#
Continuations_as_objects>
 
L

Lew

John said:
This example demonstrates the continuation-passing style seen in
functional programming. IIUC, the local variable named "parameter" must
be final in order to be accessible to the inner class:

<http://en.wikipedia.org/wiki/Continuation-passing_style#Continuations_as_objects>

Just because inner classes need variables to be 'final' to use them doesn't
mean that use of 'final' is an attempt to do functional programming. That's
only one use case. All juice is a beverages, but not all beverages are juice.

--
Lew
'You might just as well say,' added the Dormouse, who seemed to be
talking in his sleep, 'that "I breathe when I sleep" is the same thing
as "I sleep when I breathe"!'
- /Alice in Wonderland/, Lewis Carroll
 
J

John B. Matthews

[QUOTE="Lew said:
This example demonstrates the continuation-passing style seen in
functional programming. IIUC, the local variable named "parameter"
must be final in order to be accessible to the inner class:

<http://en.wikipedia.org/wiki/Continuation-passing_style#Continuations_as_objects>

Just because inner classes need variables to be 'final' to use them
doesn't mean that use of 'final' is an attempt to do functional
programming. That's only one use case. All juice is a beverages,
but not all beverages are juice.[/QUOTE]

I agree. It's just an example of final in connection with a functional
style. I find final helps me curb an unfortunate tendency to recycle
local variables, which I might otherwise forget to clean up.

I don't see using final as a source of confusion.
 
A

Arne Vajhøj

Lew said:
I don't follow that reasoning at all. I think you need to connect the
dots.

Functional style is characterized with that everything is immutable.

Attempting to make everything final is attempting to make everything
immutable.

(ignoring the fact that final only makes a ref not the object final)

Arne
 
L

Lew

Arne said:
Functional style is characterized with that everything is immutable.

Attempting to make everything final is attempting to make everything
immutable.

(ignoring the fact that final only makes a ref not the object final)

That means functional implies immutable, not immutable implies functional.
 
A

Arne Vajhøj

Lew said:
That means functional implies immutable, not immutable implies functional.

You think you can have everything immutable and not be coding
functionally?

Arne
 
L

Lew

You think you can have everything immutable and not be coding
functionally?

You haven't convinced me otherwise. You've only shown how functional
programming implies immutability. I admit ignorance - I just don't see how
the converse holds. So, yes, I do. For example, I've thrown 'final'
liberally in my programs, though of course not "everything", which is
something I've already agreed is excessive and unnecessary at least. At no
point when I did that was I attempting to program functionally, or in any way
thinking in a functional paradigm. I was thinking in an object-oriented,
imperative fashion.

So, again, yes, I do think you can have everything *appropriate* be final and
not be attempting to code functionally. My original point in this subthread
was that using 'final' liberally is a best practice in Java, and my question
to you has been how that implies an attempt to code functionally. That is a
brand-new point to me, and I believe that I will learn something valuable from
its explanation.
 
A

Arne Vajhøj

Lew said:
You haven't convinced me otherwise. You've only shown how functional
programming implies immutability. I admit ignorance - I just don't see
how the converse holds. So, yes, I do.

If we really oversimplify things then:

immutable => functional
mutable => imperative
For example, I've thrown
'final' liberally in my programs, though of course not "everything",
which is something I've already agreed is excessive and unnecessary at
least.

I think we more or less agree on the usage of finally. I started
this subthread because I objected to "Use final wherever possible.".
I do not have a problem with "Use final wherever appropriate."
So, again, yes, I do think you can have everything *appropriate* be
final and not be attempting to code functionally. My original point in
this subthread was that using 'final' liberally is a best practice in
Java, and my question to you has been how that implies an attempt to
code functionally. That is a brand-new point to me, and I believe that
I will learn something valuable from its explanation.

I guess it is possible to code functionally without knowing it.

Some claim that functional languages is the future of programming
especially on multi-multi-core systems where parallel programming
is essential.

That may be true. Time will tell.

I am just skeptical about trying to use Java in a way the
language was not originally designed for.

I don't believe in object oriented Cobol either.

Not because object oriented is not good.

But because Cobol was not designed for that.

I would use Java for object oriented programming.

And I would use something else (like Scala) for functional
programming if I ever wanted to dive into that.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top