195k views
17 votes
Given the following class: Create a class MyGenerator that can be used in the below main method.

class Random{
public static void main(String [] args){
perform(new MyGenerator());
}
private static abstract class Generator {
protected abstract double getRandom();
}
public static void perform(T g){
System.out.println(g.getRandom());
}
}

User Nikeee
by
5.4k points

1 Answer

10 votes

Answer:

Step-by-step explanation:

The below code shows the complete code for the project. we have used Math.random() as a means of generating a random double value in the getRandom() method since it is not specified.

class Random {


\ \ \ \ \ \ \ \ \mathbf{ public \ static \ void \ main \ (String[] \ args) \ \ \{}

perform(new MyGenerator());

}


\mathbf{private \ static \ abstract \ class \ Generator \{ \ }


\mathbf{ \ protected \ abstract \ double \ getRandom();}

}

public static <T extends Generator> void perform(T g) {


\mathbf{System.out.println(g.getRandom());}

}

// MyGenerator class inherited from Generator

public static class MyGenerator extends Generator {

// implementing abstract method getRandom() of Generator class


\mathbf{Override}


\mathbf{protected \ double \ getRandom() \{ }

//returning a single double value between 0.0 and 1.0


\mathbf{return \ Math.random();}

}

}

}

/*OUTPUT (will be random)*/

0.53296649765

User Ayende Rahien
by
6.0k points