I'm a student. Just got back a homework assignment stating I should call the constructor method rather than reusing the same code. I copied the code because I could not call the constructor without an error. What is the proper syntax to call the constructor method...from a separate method?
I did do a search, but I could not find this specific (within the class) issue. I did try using "this" as well as creating a class instance too, but I keep getting errors.
import java.util.Random;
public class Coin {
// variable for a generic coin toss
private String sideUp;
// Constructor
// ******************** Instructor notes...
// This is the same code as your toss() method
// It is OK to call that method from your constructor.
// Don't copy/paste code or repeat yourself if not required.
public Coin() {
Random rand1 = new Random();
int x = rand1.nextInt(2);
if (x > 0){
sideUp = "Heads";
}else{
sideUp = "Tails";
}
}
//Void Method
public void toss() {
// how to call the Coin constructor above??????????????????????????
Coin();
}
}