Select a random student: Difference between revisions

From Computer Science Wiki
Line 71: Line 71:
</body>
</body>
</html>
</html>
</syntaxhighlight>
<!-- and below is the random.php file -->
<syntaxhighlight lang="php" >
<?php
session_start();
$chosenClass = $_POST['block'];
if($chosenClass==1){
    if(ISSET($_SESSION['block1Students'])){
        $block1Students = $_SESSION['block1Students'];
        $randomStudent = array_rand($block1Students);
        echo "We are choosing <strong>$block1Students[$randomStudent]</strong>";
        unset($block1Students[$randomStudent]);
        $_SESSION['block1Students'] = $block1Students;
        $countOfStudents = count($block1Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";
        if ($countOfStudents == 0) {
            echo"<br>You have chosen all your students<br>";
            $block1Students=  ["Cesur","Maciej","Tarik","Emily","Cameron","Michael","Vera","Avi","Jaegeun","Lucia","Andrea","Aleksandra","John","Changseok","Alex","Sua","Harry"];
            $_SESSION['block1Students'] = $block1Students;
        }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";
    } else {
        $block1Students=  ["Cesur","Maciej","Tarik","Emily","Cameron","Michael","Vera","Avi","Jaegeun","Lucia","Andrea","Aleksandra","John","Changseok","Alex","Sua","Harry"];
        echo "This is the first time, we are going to create the array<br>";
        $_SESSION['block1Students'] = $block1Students;
        $block1Students = $_SESSION['block1Students'];
        $randomStudent = array_rand($block1Students);
        echo "We are choosing <strong>$block1Students[$randomStudent]</strong>";
        unset($block1Students[$randomStudent]);
        $_SESSION['block1Students'] = $block1Students;
        $countOfStudents = count($block1Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";
        if ($countOfStudents == 0) {
            echo"<br>You have chosen all your students<br>";
            $block1Students=  ["Cesur","Maciej","Tarik","Emily","Cameron","Michael","Vera","Avi","Jaegeun","Lucia","Andrea","Aleksandra","John","Changseok","Alex","Sua","Harry"];
            $_SESSION['block1Students'] = $block1Students;
        }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";
    }
} elseif($chosenClass==2) {
    if(ISSET($_SESSION['block2Students'])){
        $block2Students = $_SESSION['block2Students'];
        $randomStudent = array_rand($block2Students);
        echo "We are choosing <strong>$block2Students[$randomStudent]</strong>";
        unset($block2Students[$randomStudent]);
        $_SESSION['block2Students'] = $block2Students;
        $countOfStudents = count($block2Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";
        if ($countOfStudents == 0) {
            echo "<br>You have chosen all your students<br>";
            $block2Students = ["Dominik", "QingYu", "David", "Przemyslaw", "Stanislaw", "Sven", "Marcin", "Jan", "Siddharth", "Wojciech", "Mikolaj", "Yoon", "Chan Woo", "Maksymilian"];
            $_SESSION['block2Students'] = $block2Students;
        }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";
        } else {
        $block2Students = ["Dominik","QingYu","David","Przemyslaw","Stanislaw","Sven","Marcin","Jan","Siddharth","Wojciech","Mikolaj","Yoon","Chan Woo","Maksymilian"];
        echo "This is the first time, we are going to create the array<br>";
        $_SESSION['block2Students'] = $block2Students;
        $block2Students = $_SESSION['block2Students'];
        $randomStudent = array_rand($block2Students);
        echo "We are choosing <strong>$block2Students[$randomStudent]</strong>";
        unset($block2Students[$randomStudent]);
        $_SESSION['block2Students'] = $block2Students;
        $countOfStudents = count($block2Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";
            if ($countOfStudents == 0) {
                echo "<br>You have chosen all your students<br>";
                $block2Students = ["Dominik", "QingYu", "David", "Przemyslaw", "Stanislaw", "Sven", "Marcin", "Jan", "Siddharth", "Wojciech", "Mikolaj", "Yoon", "Chan Woo", "Maksymilian"];
                $_SESSION['block2Students'] = $block2Students;
            }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";
    }
} elseif($chosenClass==4) {
    if(ISSET($_SESSION['block4Students'])){
        $block4Students = $_SESSION['block4Students'];
        $randomStudent = array_rand($block4Students);
        echo "We are choosing <strong>$block4Students[$randomStudent]</strong>";
        unset($block4Students[$randomStudent]);
        $_SESSION['block4Students'] = $block4Students;
        $countOfStudents = count($block4Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";
            if ($countOfStudents == 0) {
                echo "<br>You have chosen all your students<br>";
                $block4Students = ["Emre", "Zofia", "Sheevankit", "Zofia", "Nguyen", "Mahmood", "Felix", "Siddharth", "Maximo", "Jasper", "Alexander", "Franciszek", "Seung Won", "Gabriele", "Annie"];
                $_SESSION['block4Students'] = $block4Students;
            }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";
            } else {
        $block4Students = ["Emre", "Zofia", "Sheevankit", "Zofia", "Nguyen", "Mahmood", "Felix", "Siddharth", "Maximo", "Jasper", "Alexander", "Franciszek", "Seung Won", "Gabriele", "Annie"];
        echo "This is the first time, we are going to create the array<br>";
        $_SESSION['block4Students'] = $block4Students;
        $block4Students = $_SESSION['block4Students'];
        $randomStudent = array_rand($block4Students);
        echo "We are choosing <strong>$block4Students[$randomStudent]</strong>";
        unset($block4Students[$randomStudent]);
        $_SESSION['block4Students'] = $block4Students;
        $countOfStudents = count($block4Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";
            if ($countOfStudents == 0) {
                echo "<br>You have chosen all your students<br>";
                $block4Students = ["Emre", "Zofia", "Sheevankit", "Zofia", "Nguyen", "Mahmood", "Felix", "Siddharth", "Maximo", "Jasper", "Alexander", "Franciszek", "Seung Won", "Gabriele", "Annie"];
                $_SESSION['block4Students'] = $block4Students;
            }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";
    }
} elseif($chosenClass==5) {
    if(ISSET($_SESSION['block5Students'])){
        $block5Students = $_SESSION['block5Students'];
        $randomStudent = array_rand($block5Students);
        echo "We are choosing <strong>$block5Students[$randomStudent]</strong>";
        unset($block5Students[$randomStudent]);
        $_SESSION['block5Students'] = $block5Students;
        $countOfStudents = count($block5Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";
        if ($countOfStudents == 0) {
            echo "<br>You have chosen all your students<br>";
            $block5Students = ["Taylor", "Andrew", "Marta", "Aron", "Tomasz", "Cameron", "Tomasz", "Taekyung", "David", "Filip", "Juwon", "Maximilian", "Haruka", "Norval", "Aleksander", "Maxwell", "Ian", "Yilin"];
            $_SESSION['block5Students'] = $block5Students;
        }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";
        } else {
        $block5Students = ["Taylor", "Andrew", "Marta", "Aron", "Tomasz", "Cameron", "Tomasz", "Taekyung", "David", "Filip", "Juwon", "Maximilian", "Haruka", "Norval", "Aleksander", "Maxwell", "Ian", "Yilin"];
        echo "This is the first time, we are going to create the array<br>";
        $_SESSION['block5Students'] = $block5Students;
        $block5Students = $_SESSION['block5Students'];
        $randomStudent = array_rand($block5Students);
        echo "We are choosing <strong>$block5Students[$randomStudent]</strong>";
        unset($block5Students[$randomStudent]);
        $_SESSION['block5Students'] = $block5Students;
        $countOfStudents = count($block5Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";
        if ($countOfStudents == 0) {
            echo "<br>You have chosen all your students<br>";
            $block5Students = ["Taylor", "Andrew", "Marta", "Aron", "Tomasz", "Cameron", "Tomasz", "Taekyung", "David", "Filip", "Juwon", "Maximilian", "Haruka", "Norval", "Aleksander", "Maxwell", "Ian", "Yilin"];
            $_SESSION['block5Students'] = $block5Students;
        }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";
    }
} else {
    echo "ERROR: invalid class!!!!! How are you here?!?! ";
}


</syntaxhighlight>
</syntaxhighlight>

Revision as of 17:26, 29 October 2017

This a problem set for you to work through [1]

This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are:

  1. to build your skill applying computational thinking to a problem
  2. to assess your knowledge and skills of different programming practices


What is this problem set trying to do[edit]

I am testing your current ability to think about a problem and solve it. There isn't a right or wrong here, just demonstrating what you know. In broad strokes, this problem set requires you to:

  1. REALLY understand the problem
  2. Think about how you might solve the problem
  3. Create a solution to the problem
  4. Test if your solution works (solves the problem)


The Problem[edit]

I teach 5 classes. I am interested in creating a system to randomly call on students in my classes. I would prefer not to call on the same student twice during one class. At the end of a class, I would like to know who I called and who I did not call on. Please reflect on your computational thinking skills & ask me questions which will help you design this system.

The input[edit]

The input will be five different buttons which represent each of my five classes. For example:

  • Class 1
  • Class 2
  • Class 3
  • Class 4
  • Class 5

The output[edit]

The output of this program will be:

  1. the first name of a student from a class, randomly selected. I will provide you with a list of first names for my students.
  2. you should store the students you have called in a list (even though for now, you will only select only one).

A possible solution[edit]

Click the expand link to see one possible solution, but NOT before you have tried and failed!

<!-- the file below should be named random.html there is another file, below.-->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Choose a random student</title>
</head>
<body>
<form action="random.php" method="post">

    <fieldset>
        <legend>Select a class</legend>

        <input type="radio" id="classes" name="block" value="1">Block 1<br>
        <input type="radio" id="classes" name="block" value="2">Block 2<br>
        <input type="radio" id="classes" name="block" value="4">Block 4<br>
        <input type="radio" id="classes" name="block" value="5">Block 5<br>

        <button type="submit">Click here to select a student</button>
    </fieldset>

</form>
</body>
</html>
<?php
session_start();

$chosenClass = $_POST['block'];

if($chosenClass==1){

    if(ISSET($_SESSION['block1Students'])){

        $block1Students = $_SESSION['block1Students'];
        $randomStudent = array_rand($block1Students);
        echo "We are choosing <strong>$block1Students[$randomStudent]</strong>";
        unset($block1Students[$randomStudent]);
        $_SESSION['block1Students'] = $block1Students;
        $countOfStudents = count($block1Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";

        if ($countOfStudents == 0) {

            echo"<br>You have chosen all your students<br>";
            $block1Students=  ["Cesur","Maciej","Tarik","Emily","Cameron","Michael","Vera","Avi","Jaegeun","Lucia","Andrea","Aleksandra","John","Changseok","Alex","Sua","Harry"];
            $_SESSION['block1Students'] = $block1Students;
        }

        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";

    } else {

        $block1Students=  ["Cesur","Maciej","Tarik","Emily","Cameron","Michael","Vera","Avi","Jaegeun","Lucia","Andrea","Aleksandra","John","Changseok","Alex","Sua","Harry"];
        echo "This is the first time, we are going to create the array<br>";
        $_SESSION['block1Students'] = $block1Students;
        $block1Students = $_SESSION['block1Students'];
        $randomStudent = array_rand($block1Students);
        echo "We are choosing <strong>$block1Students[$randomStudent]</strong>";
        unset($block1Students[$randomStudent]);
        $_SESSION['block1Students'] = $block1Students;
        $countOfStudents = count($block1Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";

        if ($countOfStudents == 0) {

            echo"<br>You have chosen all your students<br>";
            $block1Students=  ["Cesur","Maciej","Tarik","Emily","Cameron","Michael","Vera","Avi","Jaegeun","Lucia","Andrea","Aleksandra","John","Changseok","Alex","Sua","Harry"];
            $_SESSION['block1Students'] = $block1Students;
        }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";


    }
} elseif($chosenClass==2) {

    if(ISSET($_SESSION['block2Students'])){

        $block2Students = $_SESSION['block2Students'];
        $randomStudent = array_rand($block2Students);
        echo "We are choosing <strong>$block2Students[$randomStudent]</strong>";
        unset($block2Students[$randomStudent]);
        $_SESSION['block2Students'] = $block2Students;
        $countOfStudents = count($block2Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";

        if ($countOfStudents == 0) {

            echo "<br>You have chosen all your students<br>";
            $block2Students = ["Dominik", "QingYu", "David", "Przemyslaw", "Stanislaw", "Sven", "Marcin", "Jan", "Siddharth", "Wojciech", "Mikolaj", "Yoon", "Chan Woo", "Maksymilian"];
            $_SESSION['block2Students'] = $block2Students;
        }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";

        } else {

        $block2Students = ["Dominik","QingYu","David","Przemyslaw","Stanislaw","Sven","Marcin","Jan","Siddharth","Wojciech","Mikolaj","Yoon","Chan Woo","Maksymilian"];
        echo "This is the first time, we are going to create the array<br>";
        $_SESSION['block2Students'] = $block2Students;
        $block2Students = $_SESSION['block2Students'];
        $randomStudent = array_rand($block2Students);
        echo "We are choosing <strong>$block2Students[$randomStudent]</strong>";
        unset($block2Students[$randomStudent]);
        $_SESSION['block2Students'] = $block2Students;
        $countOfStudents = count($block2Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";

            if ($countOfStudents == 0) {

                echo "<br>You have chosen all your students<br>";
                $block2Students = ["Dominik", "QingYu", "David", "Przemyslaw", "Stanislaw", "Sven", "Marcin", "Jan", "Siddharth", "Wojciech", "Mikolaj", "Yoon", "Chan Woo", "Maksymilian"];
                $_SESSION['block2Students'] = $block2Students;
            }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";

    }

} elseif($chosenClass==4) {

    if(ISSET($_SESSION['block4Students'])){

        $block4Students = $_SESSION['block4Students'];
        $randomStudent = array_rand($block4Students);
        echo "We are choosing <strong>$block4Students[$randomStudent]</strong>";
        unset($block4Students[$randomStudent]);
        $_SESSION['block4Students'] = $block4Students;
        $countOfStudents = count($block4Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";
            if ($countOfStudents == 0) {

                echo "<br>You have chosen all your students<br>";
                $block4Students = ["Emre", "Zofia", "Sheevankit", "Zofia", "Nguyen", "Mahmood", "Felix", "Siddharth", "Maximo", "Jasper", "Alexander", "Franciszek", "Seung Won", "Gabriele", "Annie"];
                $_SESSION['block4Students'] = $block4Students;
            }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";

            } else {

        $block4Students = ["Emre", "Zofia", "Sheevankit", "Zofia", "Nguyen", "Mahmood", "Felix", "Siddharth", "Maximo", "Jasper", "Alexander", "Franciszek", "Seung Won", "Gabriele", "Annie"];
        echo "This is the first time, we are going to create the array<br>";
        $_SESSION['block4Students'] = $block4Students;
        $block4Students = $_SESSION['block4Students'];
        $randomStudent = array_rand($block4Students);
        echo "We are choosing <strong>$block4Students[$randomStudent]</strong>";
        unset($block4Students[$randomStudent]);
        $_SESSION['block4Students'] = $block4Students;
        $countOfStudents = count($block4Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";
            if ($countOfStudents == 0) {

                echo "<br>You have chosen all your students<br>";
                $block4Students = ["Emre", "Zofia", "Sheevankit", "Zofia", "Nguyen", "Mahmood", "Felix", "Siddharth", "Maximo", "Jasper", "Alexander", "Franciszek", "Seung Won", "Gabriele", "Annie"];
                $_SESSION['block4Students'] = $block4Students;
            }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";

    }

} elseif($chosenClass==5) {

    if(ISSET($_SESSION['block5Students'])){

        $block5Students = $_SESSION['block5Students'];
        $randomStudent = array_rand($block5Students);
        echo "We are choosing <strong>$block5Students[$randomStudent]</strong>";
        unset($block5Students[$randomStudent]);
        $_SESSION['block5Students'] = $block5Students;
        $countOfStudents = count($block5Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";
        if ($countOfStudents == 0) {

            echo "<br>You have chosen all your students<br>";
            $block5Students = ["Taylor", "Andrew", "Marta", "Aron", "Tomasz", "Cameron", "Tomasz", "Taekyung", "David", "Filip", "Juwon", "Maximilian", "Haruka", "Norval", "Aleksander", "Maxwell", "Ian", "Yilin"];
            $_SESSION['block5Students'] = $block5Students;
        }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";

        } else {

        $block5Students = ["Taylor", "Andrew", "Marta", "Aron", "Tomasz", "Cameron", "Tomasz", "Taekyung", "David", "Filip", "Juwon", "Maximilian", "Haruka", "Norval", "Aleksander", "Maxwell", "Ian", "Yilin"];
        echo "This is the first time, we are going to create the array<br>";
        $_SESSION['block5Students'] = $block5Students;
        $block5Students = $_SESSION['block5Students'];
        $randomStudent = array_rand($block5Students);
        echo "We are choosing <strong>$block5Students[$randomStudent]</strong>";
        unset($block5Students[$randomStudent]);
        $_SESSION['block5Students'] = $block5Students;
        $countOfStudents = count($block5Students);
        echo "<br><br>";
        echo "There are $countOfStudents left to choose.";
        if ($countOfStudents == 0) {

            echo "<br>You have chosen all your students<br>";
            $block5Students = ["Taylor", "Andrew", "Marta", "Aron", "Tomasz", "Cameron", "Tomasz", "Taekyung", "David", "Filip", "Juwon", "Maximilian", "Haruka", "Norval", "Aleksander", "Maxwell", "Ian", "Yilin"];
            $_SESSION['block5Students'] = $block5Students;
        }
        echo "<br><br> <a href=\"random.html\">Click here to choose another student</a>";

    }

} else {

    echo "ERROR: invalid class!!!!! How are you here?!?! ";
}

How you will be assessed[edit]

Your solution will be graded using the following axis:


Scope

  • To what extent does your code implement the features required by our specification?
  • To what extent is there evidence of effort?

Correctness

  • To what extent did your code meet specifications?
  • To what extent did your code meet unit tests?
  • To what extent is your code free of bugs?

Design

  • To what extent is your code written well (i.e. clearly, efficiently, elegantly, and/or logically)?
  • To what extent is your code eliminating repetition?
  • To what extent is your code using functions appropriately?

Style

  • To what extent is your code readable?
  • To what extent is your code commented?
  • To what extent are your variables well named?


References[edit]