Page 55 of 55 FirstFirst ... 545535455
Results 1,081 to 1,093 of 1093

Thread: Quiz Time

  1. #1081
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    21

    Default Re: Quiz Time

    Shall we refresh this?

    Is it able to compile the following code, and if so what is the output and why?


    Java Code:
    		int i = 10;
    		int j = 10;
    		
    		boolean b = false;
    		
    		if(b = i == j) {
    			System.out.println("True");
    		}
    		else {
    			System.out.println("False");
    		}

  2. #1082
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,708
    Rep Power
    13

    Default Re: Quiz Time

    The Java compiler sometimes disallows things we might be tempted to write in C, and thereby delivers us from the evil thereof. It's not always possible though.

  3. #1083
    vinci123 is offline Member
    Join Date
    Aug 2011
    Location
    INDIA
    Posts
    64
    Rep Power
    0

    Default Re: Quiz Time

    answer is "yes"
    I can and I will

  4. #1084
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default Re: Quiz Time

    I agree. == has higher precedence as is executed before =.
    The Java Tutorial. Read it.

  5. #1085
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    21

  6. #1086
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,474
    Rep Power
    21

    Default Re: Quiz Time

    It's no different to the classic loop for reading files:
    Java Code:
    String line;
    while ((line = reader.readLine()) != null) {
    }

  7. #1087
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default Re: Quiz Time

    It will compile and print "True" as the "==" will be performed first and then "=" giving a true for the condition .... as said its same as any of the checks we do in a for,while or if ..the operation done and the check executed.
    _______________________________________________
    give me beans .........

  8. #1088
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default Re: Quiz Time

    I dusted off this thread; as you all probably know the Math.random() method returns a number in the range [0, 1) (the range includes 0), so for two numbers x and y in that range, the result x^y (^ denotes the power operator here) is in the range [0, 1] (including both 0 and 1); the number 1 is only reached iff y == 0. An observation: x^y > x for all values of x and y in that particular first range. Have a look at the following code snippet:

    Java Code:
    public class T {
    
    	public static void main(String[] args) {
    
    		int count= 1;
    		for (double x= Math.random(); x < 1; count++, x= Math.pow(x, Math.random()))
    			System.out.println(count+": "+x);
    		System.out.println(count);
    		
    	}
    }
    This little method calculates x^y0^y1^y2 ... until the result equals 1; theoreticaly that number is never reached if all y values are unequal to zero. Run the snippet and see for yourself that this program always terminates within 50 iterations or so for al values of y not equal to zero. Why?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  9. #1089
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,937
    Rep Power
    20

    Default Re: Quiz Time

    You already said why.
    Quote Originally Posted by JosAH View Post
    An observation: x^y > x for all values of x and y
    The rest can be blamed on ncg_goldberg

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #1090
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default Re: Quiz Time

    Quote Originally Posted by DarrylBurke View Post
    You already said why.

    The rest can be blamed on ncg_goldberg
    True, but why isn't the limiting value 1-ulp(1)? The result should be less than 1 ... (unless y equals zero).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  11. #1091
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,937
    Rep Power
    20

    Default Re: Quiz Time

    Quote Originally Posted by JosAH View Post
    why isn't the limiting value 1-ulp(1)?
    That's more CS than I know about, but the documentation for the Math class does mention that
    a larger error bound of 1 or 2 ulps is allowed for certain methods.
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #1092
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,937
    Rep Power
    20

    Default Re: Quiz Time

    Not important as far as the result is concerned, but your code doesn't show the value of y, only of x^y

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #1093
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default Re: Quiz Time

    Quote Originally Posted by DarrylBurke View Post
    Not important as far as the result is concerned, but your code doesn't show the value of y, only of x^y
    But it is important for the final result; you're right about not dispaying the value of y; here's the modified code:

    Java Code:
    public class T {
    
    	public static void main(String[] args) {
    
    		int count= 1;
    		for (double x= Math.random(), y; x < 1; count++, x= Math.pow(x, y)) {
    			y= Math.random();
    			System.out.println(count+": "+x+" "+y);
    		}
    		System.out.println(count);
    		
    	}
    }
    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Page 55 of 55 FirstFirst ... 545535455

Similar Threads

  1. Hello, first time here.
    By ludragon in forum Introductions
    Replies: 2
    Last Post: 01-03-2008, 06:03 AM
  2. Help pls with a quiz
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 07:09 AM
  3. Time method
    By carderne in forum New To Java
    Replies: 5
    Last Post: 11-05-2007, 10:34 AM
  4. DataObject with the time given by me
    By garinapavan in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 06:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •