• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assignment Java-1b (Resolved)

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well this one had beaten me been trying for days to get it to work right, removed the args from the command line to simplfy it until it works.

Where have I gone wrong please?

You will probbly have to compile it to see what I mean the test is below

Javaranch-Java-1b


[ April 02, 2008: Message edited by: Stuart Smith ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at the picture here, it looks like you are trying to make it more difficult than it should be. Why are you counting the number of names that can fit in one screen ? Appending names one after another should be enough.
 
Stuart Smith
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Christophe Verre:
Looking at the picture here, it looks like you are trying to make it more difficult than it should be. Why are you counting the number of names that can fit in one screen ? Appending names one after another should be enough.



I am counting the names length becuase I am doing the second part where you are required to modify Java-1a so words are never split up. Assume that the console is 80 characters wide.

Scroll down its the 1b I am doing not the 1a

Also its not printing out 100 times it is much much more.
[ March 31, 2008: Message edited by: Stuart Smith ]
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ask yourself:

How many times will the outer loop execute?
How many times will the inner loop execute for each outer loop iteration?
nameCount = consoleWidth/nameLength forgets what part of the output?
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mate just check the length of the "some text" you are going to pass to the program via command prompt.

int len = args[0].length();


int m =80/len, test=1;

for(i=0;i<100;i++)
{
System.out.print(+args[0]+" ");

if(test++==m)
{ System.out.println();
m*=2;
}
}


Hope this may help...
[ March 31, 2008: Message edited by: Shivit Agarwal ]
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think carefully about m*=2 for a moment.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scroll down its the 1b I am doing not the 1a


Doooh. Sorry
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tim LeMaster:
Think carefully about m*=2 for a moment.



You are right.

m*=2; is like a deadly virus. Thank You.

I have also overlooked the spaces case.There will a one space between two terms everytime. So little modification.


int len = args[0].length();

m =80/len -1; // -1 will take care of spaces
int i=0;

for(i=0;i<100;i++)
{
System.out.print(+args[0]+" ");
if(i++=m)
{ System.out.println();
i=0;
}


Originally posted by Christophe Verre:

Doooh. Sorry



Mate,thats the Q. we are talking about.
 
Stuart Smith
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tim LeMaster:
Ask yourself:

How many times will the outer loop execute?
How many times will the inner loop execute for each outer loop iteration?
nameCount = consoleWidth/nameLength forgets what part of the output?



How many times will the outer loop execute? 100
How many times will the inner loop execute for each outer loop iteration?
Depends on the length on name I guess.
nameCount = consoleWidth/nameLength forgets what part of the output? no idea I am affraid.
 
Stuart Smith
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tim LeMaster:
Think carefully about m*=2 for a moment.



I don't really understand what m*=2 does exactly, this is the problem my maths is weak, in fact I am having trouble understand how the code works that uses it.
 
Stuart Smith
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shivit Agarwal:


if(test++==m)
{ System.out.println();
m*=2;
}
}



Being one with a need to understand the code rather than just getting the answer correct could you explain what the if(test++==m) and the m*=2 actually means.
 
Stuart Smith
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lastly thanks all for your replies any one suggest a good place to learn the math I guess I will need to update my skills there before I continue programming =(
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
most of this isn't really complicated math, it's complicated syntax of the language.

a common requirement is to add one to a variable - i.e. you're trying to count something. you could say every time - "take the value of i, add one to it, and assign that value back to i".

since this is done SO OFTEN, the people who created Java designed a shortcut into the language... the ++ operator. so whenever you see something like , you can think of that as "add 1 to i".

another common thing done in programs is to take a variable, and add/subtract/multiply/something else it by something, and store that back in the original variable. there is a shortcut for this as well. so, instead of writing, you can write see if you can now explain what those two pieces in your question mean...
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stuart Smith:


I don't really understand what m*=2 does exactly, this is the problem my maths is weak, in fact I am having trouble understand how the code works that uses it.



Ok mate here I go...

m*=2 is equivalent to m=m*2;
Now let say m=1, then what will happen for the above expression..

for Loop
1: m=1*2=2
2: m=2*2=4 = 2^2 (^ stands for power)
3: m=4*2=8 = 2^3
4: m=8*2=16 = 2^4
...
Likewise

Now what will happen whn it will reach 20 round of loop, m will become
m=2^20 which is a very vast value. Now just think what will happen when loop terminate at 100. It may crash your program(error).

Hope you got something atleast.
 
Stuart Smith
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right so I do not need the inner loop that makes sense to me now.

nameCount = consoleWidth / nameLength;

Should be

nameCount = consoleWidth / nameLength -1; ?

To account for the space I think.

Going to play with this some more to fully copmrened the output in a visual form, thank you all for the help like I said been stuck on this a while now although I have managed to get the next few lessons done with no problem including the Leap year test.

Thanks again everyone for helping understand what to me was complicated but in fact is very basic =/
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wasn't going to say anything - but as you changed the title to resolved I have to say the calculation "nameCount = consoleWidth / nameLength -1;" is a poor estimation of the space required at best. Here are some examples.

nameCount = 80/4 - 1; ==19 So a four character name will be printed on the line 19 times - that will actually take up 94 characters. You should only print 16 per line.

nameCount = 80/14 - 1; ==4 So a fourteen character name will be printed on the line 4 times - that will only take up 59 characters. It should be printed 5 times.

Personally I think the best solution for this does not try to determine how many to print per line, but just prints 100 hundred of them keeping track of how many characters have been printed and adding a new line when printing the next set would go beyond the number of columns.
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tim LeMaster

You are correct. I just put the concept to give the OP an idea that it could be done differently.

Anyway your logic was better.
reply
    Bookmark Topic Watch Topic
  • New Topic