Method Out

public class MO
{
static int a = 1;
static int c = 7;
public static void main( String args[] )
{
int a=3; int b=2; int d=5;
System.out.println(a+" "+b+" "+c+" "+d);
}
}

Predict the output for these programs. Follow the procedure demonstrated in class.

public class MO
{
static int a = 1;
static int c = 7;
public static void main( String args[] )
{
int a=3; int b=2; int d=5;
System.out.println(a+" "+b+" "+c+" "+d);
c = first(a,b);
System.out.println(a+" "+b+" "+c+" "+d);
sec(b);
System.out.println(a+" "+b+" "+c+" "+d);
}

static int first(int a, int b)
{
int d = c+1;
System.out.println(a+" "+b+" "+c+" "+d);
sec(b);
System.out.println(a+" "+b+" "+c+" "+d);
c++;
b=4;
System.out.println(a+" "+b+" "+c+" "+d);
return c;
}

static void sec(int x)
{
int b = x;
int d = a;
System.out.println(a+" "+b+" "+c+" "+d);
}
}

-----------------------------------------------------------------------

public class MO
{
static int a = 1;
static int b = 5;
public static void main( String args[] )
{
int a=3; int c=2; int d=4;
System.out.println(a+" "+b+" "+c+" "+d);
a = first(a,b);
b = a + c;
System.out.println(a+" "+b+" "+c+" "+d);
sec(b);
System.out.println(a+" "+b+" "+c+" "+d);
}

static int first(int c, int d)
{
int b = c+1;
System.out.println(a+" "+b+" "+c+" "+d);
sec(b);
System.out.println(a+" "+b+" "+c+" "+d);
c++;
b=7;
System.out.println(a+" "+b+" "+c+" "+d);
return c;
}

static void sec(int c)
{
b = 9;
int d = a;
System.out.println(a+" "+b+" "+c+" "+d);
}
}

------------------------------------------------------------------

public class MO
{
static int a = 1;
static int b = 5;
public static void main( String args[] )
{
int a=3; int c=2; int d=4;
System.out.println(a+" "+b+" "+c+" "+d);
a = first(a,b);
b = a + c;
System.out.println(a+" "+b+" "+c+" "+d);
c = sec(b,c);
System.out.println(a+" "+b+" "+c+" "+d);
}

static int first(int c, int d)
{
int b = c+1;
System.out.println(a+" "+b+" "+c+" "+d);
sec(b,d);
System.out.println(a+" "+b+" "+c+" "+d);
b=7;
System.out.println(a+" "+b+" "+c+" "+d);
return c;
}

static int sec(int c, int a)
{
b = 9;
int d = a;
System.out.println(a+" "+b+" "+c+" "+d);
return a;
}
}

------------------------------------------------------------------

public class MO
{
static int a = 1;
static int b = 8;
public static void main( String args[] )
{
int a=3; int c=2; int d=4;
System.out.println(a+" "+b+" "+c+" "+d);
b =sec(a,b);
first(c);
System.out.println(a+" "+b+" "+c+" "+d);
c = sec(b,c);
System.out.println(a+" "+b+" "+c+" "+d);
}

static void first(int c)
{
int d = c+1;
System.out.println(a+" "+b+" "+c+" "+d);
b=7;
}

static int sec(int c, int a)
{
b = 9;
int d = a+1;
first(d);
System.out.println(a+" "+b+" "+c+" "+d);
return a;
}
}

-------------------------------------------------------------

Now, make your own examples by changing the values of the variables, the order of calls, or the parameters passed. Try making a third method and call it. Be able to trace code for the exam. Make sure you follow the procedure covered in class. This will help you immensely for understanding recurrsion also.

 

BuiltWithNOF

[Java at Evergreen] [Syllabus] [To Do List] [Lessons/Programs] [Evaluation/Review] [Help] [FAQ/GWIFO] [Discuss/Reports] [Resources] [Readings] [Portfolio] [Instructor]