• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

plz Explain me line by line this code

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Beverage {

boolean [] b = new boolean[3];
int count = 0;

void set(boolean [] x , int i ){
x[i]= true;
++count;
}

public static void main(String []args){
Beverage ba = new Beverage();
ba.set(ba.b,0);
ba.set(ba.b,2);
ba.test();
}

void test() {
if(b[0]&& b[1] | b[2])
count++;
if(b[1] && b[(++count -2)])
count +=7;
System.out.println ("Count =" +count);


}
}
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Akhilesh
le me do a bit 2 exp
whenn u call the set method first time in main
coun becomes 1
second time it becomes 2

on calling test method
if(b[0]&& b[1] | b[2])
count++;
cond is true
hence

count becomes 3

so in SOP ans should be 3

regards

jerry
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b[] = new boolean[3] gives the default values "false" to teh three elements

set() modifies the b[0] = b[2] = true;
and the value of count = 2;

so the first condition is ( true && false || true) is "true"
and count is incremented to 3;

Then the next condition is (false && b[++count-2]) evaluates only upto the first false, since "&&" is short circuit and.
So the count remains "3".

If the expression has been (true && b[++count-2]) then the result is "11"
[ April 04, 2006: Message edited by: Shaliey G ]
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic