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

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

1

can you show the output please?


1 Answer
1
30views

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.

Please log in to add an answer.