written 7.8 years ago by
teamques10
★ 64k
|
•
modified 4.2 years ago
|
PROGRAM
import java.util.Random;
import java.io.*;
class Student {
String name;
int id;
}
class StudentLottery {
Student s[] = new Student[10]; //Assuming school has 100 students
private int counter = 0;
void addStudents(String name, int id) {
s[counter] = new Student();
s[counter].name = name;
s[counter].id = id;
counter++;
}
Student pickWinner() {
Random rand = new Random();
int min = 0;
int max = 9;
int randNum = rand.nextInt((max - min) + 1 )+min; //generates the number between 0 - 9
return s[randNum];
}
public static void main(String args[]) {
StudentLottery lot = new StudentLottery();
lot.addStudents("Raju", 1);
lot.addStudents("Pinki", 2);
lot.addStudents("Veer", 3);
lot.addStudents("Yash", 4);
lot.addStudents("Janet", 5);
lot.addStudents("Sanket", 6);
lot.addStudents("Pankaj", 7);
lot.addStudents("Chinmay", 8);
lot.addStudents("Sumit", 9);
lot.addStudents("Mayuri", 10);
Student s1 = lot.pickWinner();
System.out.println("The winner is -> " + s1.id +" : "+s1.name);
}
}
.
OUTPUT
F:\krrish\java>javac StudentLottery.java
F:\krrish\java>java StudentLottery
The winner is -> 6 : Sanket
The answer will be different name every time.
can you show the output please?