Hi all,

I cannot seem to grasp the use of wrapper classes, or rather how to use them. I am in the middle of practicing for an exam tommorrow but I am a little stumped with the following task:

Create an overloaded setValue method that recieves a String parameter. This method is set the field value. You will need to use the wrapper class to convert the String parameter to the value field's type of double.

I will show you what I have done so far:

package order;

public class Order {
    private int orderNo;
    private String supplier;
    private double value;

    public Order(int orderNo, String supplier, double value){
        this.orderNo = orderNo;
        this.supplier = supplier;
        this.value = value;
    [B]}
    public void setValue(String value){
        double s = Double.parseDouble(value);
    }[/B]
    public void setOrderNo(int o){
        orderNo = o;
    }
    public void setSupplier(String s){
        supplier = s;
    }
    public void setValue(double v){
        value = v;
    }
    public int getOrderNo(){
        return orderNo;
    }
    public String getSupplier(){
        return supplier;
    }
    public double getValue(){
        return value;
    }

}

I have made bold the part where I am going wrong.
Now I have created a tester as follows:

package order;

    public static void main(String[] args) {
        Order o1 = new Order(1002,"Tremble supplies",320.60);
        o1.setValue("90.68");
        System.out.println("test value "+o1.getValue());
    }

}

but my output is :

run:
test value 320.6
BUILD SUCCESSFUL (total time: 0 seconds)

where it should be "test value 90.68"
I hope I have been clear here, if not let me know.
So bassically what I am not to sure on is the bolded code (not sure about the parameters and variables ive used either), I have not had much practice with overloaded methods with wrapper classes. I could not find many examples online as at this stage we do not use java.util for example.

I look forward to any suggestions.

Recommended Answers

All 2 Replies

You are missing the assignment to "value" field of your object i.e. this.value = s .

... in the setValue(String) method, that is.

public void setValue(String value){
        double s = Double.parseDouble(value);
    }

This sets a local variable, which is then lost when the method ends.

Add Sanjay's line (above) and you're set.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.