Hello everyone i'm beginner and now i'm at polymorphism section, and i found something hard to understand, and this is the first time i saw Math.random(), The question is why the instructor use *5, why not *6 or *4 or other numbers?, the second question is why he used +1 especially? Sorry for my bad english This is the source code that i'm talking about:
package CompositionChallenge;
class Movie {
private String name;
public Movie(String name) {
this.name = name;
}
public String plot() {
return "There is no plot";
}
public String getName() {
return name;
} }
class hacksawRidge extends Movie {
public hacksawRidge() {
super("Hacksaw Ridge");
}
@Override
public String plot() {
return "Talk about world war 2 and how a medical person helps people to live their life";
}}
class Love extends Movie {
public Love() {
super("Love");
}
@Override
public String plot() {
return "talk about a person has sex every single day with his wife and girlfriend";
}}
class TripleThreat extends Movie {
public TripleThreat() {
super("TripleThreat");
}
@Override
public String plot() {
return "The best fighters in the world in one fucking movie";
}}
public class Main {
public static void main(String[] args) {
for (int i = 0; i <= 4; i++) {
Movie movie = randomMovie();
System.out.println(
"Number " + i + " and the random movie is " + movie.getName() + " and its plot is " + movie.plot());
}}
public static Movie randomMovie() {
int randomNumber = (int) (Math.random() * 5) + 1;
System.out.println("Random number generated was " + randomNumber);
switch (randomNumber) {
case 1:
return new Love();
case 2:
return new hacksawRidge();
case 3:
return new Movie("No movie now");
case 4:
return new TripleThreat();
default:
return null;
}
}}
Aucun commentaire:
Enregistrer un commentaire