tags:

views:

40

answers:

2

The only other language I've any experience with is Perl & this is my first crack at OO programming. I feel like I'm approaching this all wrong. One of the problems is probably me trying to code OO Java like I coded non-OO Perl. Can anyone suggest a way to gracefully accomplish what I'm trying to accomplish in the code snippet below?

Note: The ??? in my code is where I would want to use the default object.

public class Var {
    private double var1;

    public Var (double PassedVar1) {  //method to create new object
        var1 = ???.SetVar1(PassedVar1);
    }

    public void SetVar1 (double PassedVar) {
        if (PassedVar > 0) {  //make sure we're assigning a positive value
            var1 = PassedVar;
        }
        else {  //force user to input a new value
            System.out.print( "\nFailed to set var.  " +
            "Please enter a number greater than zero: ");
            Scanner scan = new Scanner (System.in);
            PassedVar = scan.nextDouble();
            var1 = ???.SetVar1(PassedVar);//recurse call to assure positive
        }
    }
}
+2  A: 

The word you're looking for is this (i.e. this.SetVar1(...)), however in Java that's not necessary. You can just use SetVar1(...) in any non-static member of Var of any subclasses of Var.

No doubt others will offer advice on a better way to accomplish your task in Java, so I won't answer that part of the question.

Gabe
A: 

Your code would become this, after a little refactoring and fixing,

public class Var {
    private double var1;

    public Var (double passedVar1) {  //method to create new object
        setVar1(passedVar1); // You just need to invoke setVar1() by passing the argument
    }

    public void setVar1 (double passedVar1) {
        if (passedVar1 > 0) {  //make sure we're assigning a positive value
            var1 = passedVar1;
        } else {  //force user to input a new value
            System.out.print( "\nFailed to set var.  " +
                                   "Please enter a number greater than zero: ");
            Scanner scan = new Scanner (System.in);
            passedVar1 = scan.nextDouble();
            setVar1(passedVar1); //recurse call to assure positive
        }
    }
}

Okay because of critics, I dare to change it drastically.

public class Var {

    private double var1;

    private Var(double var1) { 
        this.var1 = var1;
    } 

    public static final Var getVar(double var1) { 
        return (var1 > 0) ? new Var(var1) : null;
    }    
}

In this way the calling code should check for null and do whatever it likes to on encountering null. Anyways, below is the code snippet that is making sure that the input double is valid and then instantiating Var.

         ...
         double passedVar1 = 0D;
         do {
            System.out.print( "\nFailed to set var.  " +
                                   "Please enter a number greater than zero: ");
            Scanner scan = new Scanner (System.in);
            passedVar1 = scan.nextDouble();
         } while (passedVar1 < 0)

         Var var = Var.getVar(passedVar1);
         ...

Cheers.

Adeel Ansari
I would use a `while` loop instead of a recursive call to `setVar1` so that a huge series of bad inputs doesn't cause a StackOverflow.
Gabe
I would provide a no-args constructor which calls this(-1) as well. This will mean that you can create a Var easily and the user will be prompted for the input.
Syntax
@Gabe: Of course, I am assuming that the main reason to write the recursive code is for the sake of writing recursion. It looks like an assignment kind of thing to me. Thats the reason, I didn't change it much.
Adeel Ansari
@Syntax: Then it would be completely different thing, when you are able to create the `Var` instance. And BTW, how the user will be prompted for the input?
Adeel Ansari