Question: Each year, sleepy Hollow Elementary School holds a "Principal for a day" lottery.
0

A student can participate by entering his/her name and ID into a pool of candidates. The winner is selected randomly from all entries. Each student is allowed only one entry. Implement a Student Class that encapsulates the Student. Implement StudentLottery class with methods addStudents() and pickWinner() and main().

Hint: use random class to pick winner.

Marks: 10 M

Year: Dec 2014

ADD COMMENTlink
 modified 2.1 years ago by Krrish ♦ 190 written 2.7 years ago by Sayali Bagwe • 1.9k
1

can you show the output please?

ADD COMMENTlink
 written 2.1 years ago by

I've added the output below.

ADD REPLYlink
 written 2.1 years ago by Krrish ♦ 190
1

OUTPUT

F:\krrish\java>javac StudentLottery.java

F:\krrish\java>java StudentLottery
The winner is -> 6 : Sanket


The answer will be different name everytime.

ADD COMMENTlink
 written 2.1 years ago by Krrish ♦ 190
0
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);
}
}

ADD COMMENTlink
 written 2.7 years ago by Sayali Bagwe • 1.9k
Please log in to add an answer.