In this program if-statement is presented which must be called from an another class with the class name Studentfactory.
public class Control {
public static void main(String[] args) {
Student[] studs = new Student[3];
for (int i = 0; i < 3; i++) {
studs[i] = createStudent();
}
for (int i = 0; i < 3; i++) {
System.out.println(studs[i]);
}
}
static Student createStudent() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name:");
String name = sc.nextLine();
System.out.print("Enter your age:");
int age = sc.nextInt();
if(age <20) {
return new JuniorStudent(name, age);
} else if( age < 30) {
return new IntermediateStudent(name,age);
}
return new SeniorStudent(name, age);
}
}
Aucun commentaire:
Enregistrer un commentaire