/////////////////////////////////////////////Task 8 /////////////////////////////////////////
//The purpose of the questions class object is to store a question and a correct answer then
//checks the answer provided by the user against the stored correct answer.
class Questions{
private $QuestionNumber, $Question, $StudentAnswer, $CorrectAnswer;
private static $TotalCorrect_StudentAnswers = 0;
Public static $TotalQuestions = 0;
//The constructor will setup the object with the initial properties.
public function __construct($QuestionNumber,$Question,$CorrectAnswer){
$this->QuestionNumber = $QuestionNumber;
$this->Question = $Question;
$this->CorrectAnswer = $CorrectAnswer;
$this->StudentAnswer = "";
self::$TotalQuestions++;
}
//The set_StudentAnswer will check if the student have already ansered the
//question correctly and remove the correct mark from the sum before rechecking it
//and then only add to the sum if it is correct.
public function set_StudentAnswer($Answer){
if ($this->StudentAnswer == $this->CorrectAnswer){
self::$TotalCorrect_StudentAnswers--;
}
$this->StudentAnswer = $Answer;
if ($this->StudentAnswer == $this->CorrectAnswer){
self::$TotalCorrect_StudentAnswers++;
}
}
public function get_StudentAnswer(){
return $this->StudentAnswer;
}
public function get_Question(){
return $this->Question;
}
//The get_TotalCorrect_StudentAnswers will return the current score that the user have tallied.
public static function get_TotalCorrect_StudentAnswers(){
return self::$TotalCorrect_StudentAnswers;
}
}
/////////////////////////////////////////////Task 8 A /////////////////////////////////////////
Echo "
Task 8: A
";
Echo " Methods and properties can also be accessed without the use of an object. To do this,
";
Echo " the methods/properties must be declared static. Static methods/properties of a class can
";
Echo " then be accessed directly with the help of the Class properties and class methods by using
";
Echo " the double colon also know as the Scope resolution operator.
";
/////////////////////////////////////////////Task 8 B /////////////////////////////////////////
Echo "Task 8: B
";
Echo " A static property or method can be accessed by coding the self keyword,
";
Echo " followed by a double colon and the property or method name.
";
/////////////////////////////////////////////Task 8 C /////////////////////////////////////////
Echo "Task 8: C
";
Echo " Create Questions for Student:
";
//The following code will produce a upper and lower bound for the random math questions.
$randFrom = rand(1, 7);
while ($randFrom <= 1){
$randFrom = rand(1, 7);
}
$randTot = rand(1, 8);
while ($randFrom == $randTot || $randTot <= 1 || $randFrom >= $randTot){
$randTot = rand(1, 8);
}
//end of randomizing bounds.
//The for loop creates questions and correct answers based on the upper and lower bounds created.
for($i=$randFrom; $i <= $randTot; $i++){
$ques[$i] = new Questions($i,"What is 2*$i ?", (2*$i));
}
//The ouput produced is the total number of questions created.
Echo "Total number of questions created: ". Questions::$TotalQuestions ."
";
Echo "Student answers begin:";
Echo "
";
//The following now uses the bounds created and tries to guess the correct answers.
//Then sets the guessed anser and outputs the current score as a percentage.
for($i=$randFrom; $i <= $randTot; $i++){
$ques[$i]->set_StudentAnswer((2*rand(1, $randTot)));
Echo $ques[$i]->get_Question() ." = ". $ques[$i]->get_StudentAnswer();
Echo "
";
Echo "Current Success rate for student: ". (Questions::get_TotalCorrect_StudentAnswers() / Questions::$TotalQuestions)*100 ."%
";
}
Echo "Total amount of correct answers from student: ". Questions::get_TotalCorrect_StudentAnswers() ." out of ". Questions::$TotalQuestions ."
";
Echo "By using the Static method and properties you can view the overall status of an object with out having
";
Echo "to recalculate all of the stats to see how a student is currently doing.
";