The third wrong answer in Pass4Sure 1Z0-851

Hi every one …warning-sign


There is another question that have been solved wrongly in Pass4Sure in SCJP 1Z0-851 [1/6/2011].

Here is the question:

Given:  public class Doubler{
                public static int doubleMe(Holder h){
                         return h.getAmount()*2;
                }
           }

and:     public class Holder{
                 int amount = 10;
                 public void doubleAmount(){
                           amount = Doubler.doubleMe(this); }
                 public int getAmount(){ return amount; }
                 //more code here
            }

Place the code fragments in position to reduce the coupling between Doubler and Holder.

           public class Doubler{
                public static int doubleMe( ------------ h){
                         return ------------  * 2;
                }
           }

           public class Holder{
                 int amount = 10;
                 public void doubleAmount(){
                           amount = Doubler.doubleMe( ------------ ); }
                 public int getAmount(){ return amount; }
                 //more code here
            }

Code Fragments

Doubler int Holder void
amount this h h.getAmount()
 
 
 
The answer Pass4Sure was:

           public class Doubler{
                public static int doubleMe( void h){
                         return h* 2;
                }
           }

           public class Holder{
                 int amount = 10;
                 public void doubleAmount(){
                           amount = Doubler.doubleMe( h.getAmount() ); }
                 public int getAmount(){ return amount; }
                 //more code here
            }

But the answer must be:

           public class Doubler{
                public static int doubleMe( int h){
                         return h* 2;
                }
           }

           public class Holder{
                 int amount = 10;
                 public void doubleAmount(){
                           amount = Doubler.doubleMe( amount ); }
                 public int getAmount(){ return amount; }
                 //more code here
            }

Firstly:
you the argument h must have a type [There is no void type].
Secondly:
the parameter that you pass when you call the static method doubleMe can’t be h.getAmount because there is no h object, so it must be the variable amount.

I have tried it on NetBeans, the classes:

The Main class to test

/**
*
* @author Ahmad Attallah –www.hope-sun.net-
*/
public class Main {

    public static void main(String[] args) {
        Holder h = new Holder();
        h.doubleAmount();
        System.out.println(h.getAmount());
    }
}

The Holder class

public class Holder {

    int amount = 10;

    public void doubleAmount() {
        amount = Doubler.doubleMe(amount);
    }

    public int getAmount() {
        return amount;
    }
    //more code here
}

The Doubler class

public class Doubler {

    public static int doubleMe(int h) {
        return h * 2;
    }
}

ليست هناك تعليقات:

إرسال تعليق

---- أتشرف بتعليقاتكم ----