tags:

views:

351

answers:

9

A. Describe what happens the variables ‘a’ and ‘x’ during the execution of the ‘main’ and ‘add1’ methods in the class below. Clearly specify which are the formal and actual parameters when the ‘add1’ method is called.

public class Variables {

    public static void add1(int a) {
        a = a + 1;
        System.out.println("a = " + a);
    }

    public static void main(String[] args) {
        int x = 1;
        add1(x);
        System.out.println("x = " + x);
    }
}

B. Using suitable examples outline the difference between value and action methods and also explain what is meant by method composition.

Stuck with this question and was wondering if anyone would be able to give me a quick summary?

+14  A: 

Quick summary:

You are being asked to describe what happens to the variables ‘a’ and ‘x’ during the execution of the ‘main’ and ‘add1’ methods in the given class. You're asked also to clearly specify which are the formal and actual parameters when the ‘add1’ method is called.

Then you're asked to explain the difference between value and action methods and also explain what is meant by method composition, and give examples.

hth.

CPerkins
`if(leastHelpfulAnswer.equals(onlyCorrectAnswer)) {upvote++;}` :-))
Andreas_D
+1 for attention to detail.
Milan Ramaiya
+3  A: 

You need to read some introduction articles on programming first and then try to solve the problem. Start with variable. Then try to understand what assignment means. I'm sure if you don't give up and read this article about methods you'll be able to solve task.

Boris Pavlović
I agree with you, in general. However, from the sound of this question the poster is in a schooling environment, and normally would have been given the relevant information before or along with the exercise. If he's not making use of the resources provided, then he's not learning effectively and will likely do poorly in his course.
Carl Smotricz
I agree with you, in general. Let's say somebody who's not a scholar reads this question and is just curious how to solve the task. Maybe these links could help her.
Boris Pavlović
+7  A: 

You probably know that execution of your program is starting with the first statement in method main(). Hopefully you also understand the language well enough to know what happens from that point on. With this, I think your assignment can be done like this:

  • make a chart, with your program's variables (a and x) over the columns and one row for each line of the program executed.
  • for each line executed, note down what happens to the values in the variables.
  • recap the results.
Carl Smotricz
A: 

After doing it on papper, as sugested by Carl Smotricz, you should try to run it and compare your results with the computer ones
and keep an eye what happens with the value of x before and after the call to add1

EDIT: also have a look at Jeliot

Carlos Heuberger
+1  A: 

The answer is 42!

Stefan Hendriks
+2  A: 

A formal parameter is one that is in a method signature.

Does that help any?

R. Bemrose
A: 

My 2 cents worth: It's never too early to learn how to use a debugger.

It will allow you to go step by step in the program and "see" what happens.
There are some great video tutorials on the Eclipse debugger here.

If you learn how to use the debugger effectively, it'll help you for all of your assignments and you'll be able to finish your programs much faster than other people. Not because you won't make mistakes (you will make a ton at first) but because finding them will be trivial.

JRL
But if someone is having trouble understanding what a parameter is and what an assignment statement does, asking him to master a debugger is a little unrealistic. That's a little like saying, "Oh, you're having trouble understanding the idea that flipping this switch makes the light go on? Well, the first step is to learn quantum mechanics, then learn how to use a particle accelerator, so you can monitor the flow of electrons through the wires."
Jay
@Jay: your analogy that a debugger is like quantum physics is **ridiculous**. Using a debugger is not complex at all, it's a step that's often skipped. Being able to see what the program is doing step by step is an invaluable tool that is never too early to use. I actually recommend it even MORE for beginners. And if you looked at those videos, you'd see they are for total beginners, yet can be useful for more experienced programmers as well.
JRL
@Jay, in fact, your comment is exactly what makes beginners think that a debugger is this kind of mysterious overly complex thing and thus they don't use it and continue putting print statements all over the place for 2 years.
JRL
I think his comment is relatively accurate considering that if someone doesnt know what method composition is, they have no chance of even understanding what a debugger will do in the first place. Find out how long it will take for them to utilize a break point.
John V.
@John: the link I posted are **video** tutorials made for beginners. Can't make it much easier than that. Anyhow, I believe it's a great tool - I used it on my second assignment in my first CS class, and don't see why others couldn't. In any case, if someone tries it and can't figure it out, it's just a suggestion, no one's forced to do anything. But apparently answering 42 is more helpful than my suggestion.
JRL
A: 

For homework, please ask your teacher.

Thorbjørn Ravn Andersen
+2  A: 

So lets take this from the beginning ...

    public class Variables {

        public static void add1(int a) {
            a = a + 1;
            System.out.println("a = " + a);
        }

        public static void main(String[] args) {
            int x = 1;
            add1(x);
            System.out.println("x = " + x);
        }
    }


could be rewritten ...

    public class Variables {

        public static void main(String[] args) {
            int x = 1;
            int a = x;
            a = a + 1;
            System.out.println("a = " + a);
            System.out.println("x = " + x);
        }
    }


and run it would print ...

        a = 2
        x = 1


I've done the above to demonstrate that the method add1 has no effect on the variable x.


re: Question 1


Formal Parameter:
The identifier used in a method to stand for the value that is passed into the method by a caller.


Actual Parameter:
The actual value that is passed into the method by a caller.


reference: http://chortle.ccsu.edu/CS151/Notes/chap34/ch34_3.html


As main executes x is declared, and it is then initialized to hold the value 1.

Next add1 is called with the actual parameter 1 by main, and since the method add1 has the formal parameter a declared in its parameter list, it intializes a to hold the value of 1.

The add1 method then adds the value of 1 to the formal parameter a assigning the resultant value back the to the formal parameter a,  prints "a = 2" to the standard out, and add1 returns.

The main method then prints "x = 1" to the standard out, and the program terminates.


re: Question 2


Value Method:
Will return a primitive or an object based on an input, or the program or object's state.


Action Method:
Will do something like print text to the screen, or perhaps display a window when called.
(Could return a boolean upon success or failure though)


Googling the phrase method composition isn't very enlightening is it.

"A Java Method is Composed of" ...


A Modifier or Modifiers:
Which define which and/or how Objects can call the Method.

    public static void main(String[] args) {

    // "public" and "static" are "Modifiers".


A Return Type:
Which states what the method will return.

    public static void main(String[] args) {

    // "void" is the "Return Type".


A Signature:
Which consists of "The Method's Name" (main) and "Parameter List" (args)

    public static void main(String[] args) {

    // "main(String[] args)" is "The Method Signature".


A Body:
In which it may declare local variables, and which will contain the code which will be performed by the method.

                                           {
        int x = 1;
        add1(x);
        System.out.println("x = " + x);
    }

    // Everything between the "Curly Brackets" is "The Body".
_ande_turner_
+1 for effort! I *almost* understood all that, too! :)
Carl Smotricz