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
}
}
}