Haani Naz

Greenhorn
+ Follow
since May 30, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Haani Naz

Jeff Verdegan wrote:

Haani Naz wrote:So i believe the 2nd one (below) will finish. Seeing how n only increments by 1 and i will multiplication of its own value - at some point it will be greater than n. I am right correct?




Yes, it will finish, but your reasoning applies more to actual integers than to Java ints or longs. (And, like I said, unless you come back and tell me otherwise, I'm going to assume we're dealing with Java primtives here.)

One very simple way to answer this question is to keep in mind that multiplying an int by 2 is the same as doing a bitwise left-shift of 1.



Think about what happens to the bit pattern when you do that. Better yet, follow Campbell's suggestion and work it out on pencil and paper. Do it with a single 8-bit byte to make your life simpler.





i actually stole this question off a python quiz. Was trying to understand the general implications.
12 years ago

Campbell Ritchie wrote:What about No 2: What if you start with n = 2000000000;? What will happen then?
Go through the execution with pencil and paper for 32 iterations and tell us the values of i and n at that stage. Then work out whether the loop will terminate.



I did one better, i ran the program. At the end:

i was 2147483648
n was 2000000031


So it would finish?
12 years ago
So i believe the 2nd one (below) will finish. Seeing how n only increments by 1 and i will multiplication of its own value - at some point it will be greater than n. I am right correct?


12 years ago

Jeff Verdegan wrote:

Haani Naz wrote:

Jeff Verdegan wrote:I will say, though, that the last one looks like it might be the Collatz conjecture.




Thanks did a look up on that and i found my answer. Will focus on the 2nd one now!



Be careful though. If you start Collatz with, say 987654321 on paper you will NOT get the same next value as you will in Java.

Also, did you figure out the case where the first one never finishes?




Nah Jeff, i can't seem to figure it out. feel kinda stupid now lol.
12 years ago

Jeff Verdegan wrote:I will say, though, that the last one looks like it might be the Collatz conjecture.




Thanks did a look up on that and i found my answer. Will focus on the 2nd one now!
12 years ago
Hi guys,

Reading up on some the loops to get a firm understanding. Can somebody please clarify the following for me:


In the following examples which do you think best decribes the result:
result: will finish
result: sometimes runs forever
result: unknown



(1).



The above looks like it will finish.


(2).






(3).





I'm struggling with the last two. Can somebody shed some light on this? thanks.

12 years ago
ok just read Anniruddh Rana's post again.

think its slowly gelling together now, i realize that i need to override the equals method.

will give that a go!
12 years ago
Hi, so i took the advise and am trying to break the problem into smaller chunks.

All i'm trying to do now is check if a person exists or not.

1. if person exists, do nothing
2. if person doesn't exist, add person


here's what i've got so far:





helper.checkIfPersonExists(p1, ml) makes a call to:




i've also tried changing the if staments above to:



i can't seem to figure out how to NOT add the person if they exist. it doesn't seem to work :s

ml is the MyLibrary object and the getPeople() returns the arrayList of people.
12 years ago
Thanks guys! will test it out and report back when i get a chance.
12 years ago
Hi guys,

i'm working on a library lending system..

the logic i'm using is:

1. get name of person you would like to lend the book to
2. if arraylist is empty, add person/set name
3. loop through peroson arraylist
3.1 compare person name
3.1.1 if person name matches then lend book
3.1.2 if person name doesn't match add person to arraylist
[go back to step 3]




Here's my incomplete code:



The problem with my code despite being able to add the first person and second person successfully is when i add the third person what happens is, it goes through each item in the arrayList and subsequently adds the new person 2 more times.

so for instance:
check if person name in the arrayList is the same
if not add person to the arrayList

[when the arrayList has 2 people, it will add the third person 3 times]





here's the output so that what i'm talking about makes sense:





i've made the assumption that names are unique.

can somebody pleas advise/help me in getting this to work? i'm new to java and i can't seem to think of a solution to implement. how can i do search of the entire arrayList by the person name and add the new person if it doesn't exist..?

this is me trying to learn java as a hobby and its doing my head in lol.


THANKS!


12 years ago
Hi there,

I have the following piece of code which fails at the point it needs to accept input and i can't seem to figure out why.






The issue is at line 115.


Here is the output:



As you can see instead accepting input it skips it.

can somebody please tell me why?

thanks in advance!
13 years ago
Hi Guys,

i'm currently learning java off the book head first java. its a great book but i'd like to be able to apply my knowledge by doing some pracs.

anybody know of any websites where they offer tutorials, labs etc. so i can practice my skills?

thanks.
13 years ago
Hi,

been reading up head first java. there's a bit about creating a battleship game and i just jumped ahead and tried doing it myself.

so far i managed to create a grid which populates ships denoted by 'x' in 3 cells. i'm having trouble getting the checks right if it tries to populate it in the same cells.


here is my code. please assist in giving me advice is getting it to work, i'd really appreciate it.







here's a working output for me:


row position: 2
board length marker 2: 6
board length marker 2: 5
board length marker 2: 4
row position: 0
board length marker 2: 6
board length marker 2: 5
board length marker 2: 4
row position: 4
board length marker 2: 6
board length marker 2: 5
board length marker 2: 4
Printing starts here..
x x x - -
- - - - -
x x x - -
- - - - -
x x x - -


non-working output:

row position: 2
board length marker 2: 6
board length marker 2: 5
board length marker 2: 4
row position: 1
board length marker 2: 6
board length marker 2: 5
board length marker 2: 4
row position: 1
col increment: 1
col increment: 2
col increment: 3
board length marker 2: 3



As you can see as soon as its the same row it fails to work. it also fails to print the grid : /

Thanks in advance.


13 years ago

Ernest Friedman-Hill wrote:Look at these three lines very carefully:



Notice anything wrong?




gosh now i just feel stupid lol

thanks man.
13 years ago

Jesper de Jong wrote:

Haani Naz wrote:This fails to work - i get run time errors.


Please tell us exactly what those errors are, that would make it a lot easier to help you find the cause.



Sure. this is what i get:


13 years ago