0
votes

I'm creating a php calculator that needs to use the following classes, then print off the users name and the average score they achieved. This is the code I have so far, but it's not displaying correctly, it's saying there are missing arguments and undefined variables but i'm not sure where i've gone wrong!

<?php 

class person {

public $name;

}
class student extends person {

function student ($name, $grade1, $grade2) {
    if(is_numeric($grade1) && is_numeric($grade2)){
        $grades = array($grade1, $grade2);
    }
    elseif (is_numeric($grade1)) {
        $grade2 = 0;
    }
    else {
        $grade1 = 0;
    }
}

function average ($grades) {
     $length = count($grades);
     $total = 0;

  for ($i = 0; $i < $length; $i++) {
    $total = $total + $grades[i];
  }

  $average = $total / $length;
  return $average;

}
 }

$person1 = new student ($_POST['firstName'], $_POST['firstGrade1'],  $_POST['firstGrade2']);
$person2 = new student ($_POST['secondName'], $_POST['secondGrade1'], $_POST['secondGrade2']);
$person3 = new student ($_POST['thirdName'], $_POST['thirdGrade1'], $_POST['thirdGrade2']);

echo "<br/> $person1->name" . "achieved an average of" . "$person1->average();";
echo "<br/> $person2->name" . "achieved an average of" . "$person2->average();";
echo "<br/> $person3->name" . "achieved an average of" . "$person3->average();";

?>

ERROR MESSAGES: Warning: Missing argument 1 for student::average(), called in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\my portable files\Exercise 4\average.php on line 40 and defined in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\my portable files\Exercise 4\average.php on line 22

Notice: Undefined variable: grades in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\my portable files\Exercise 4\average.php on line 23

Warning: Division by zero in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\my portable files\Exercise 4\average.php on line 30

2
also if one of their grades isn't numeric then it needs to be set to 0user2953989
What specific errors are you getting? Why are you using ancient PHP4 style constructors instead of __construct()? Finally, you should strongly consider using array access notation for your POST values. Doing something like firstGrade1 is not scalable. Name your fields like name[0] grade[0][0] grade[0][1] and have that data populated into a nice array for you automatically.Mike Brant
If you want to know where you've gone wrong look at the error messages - they contain the line number of the specific piece of code that's failed.user1864610
I'm just working from my lecture notes, guess my teacher is still using ancient PHP!user2953989
Those error messages tell you exactly what the problems are. The first one means you are calling average() without passing a parameter. This is because you average method is a bad design. If you are passing grades in in the constructor, you should store them in the object, then you don't need to pass any parameters to average(). This is cause of all the other errors.Mike Brant

2 Answers

1
votes

You don't appear to be returning that $grades variable. It's probably not defined because you aren't returning anything.

Your method:

function student ($name, $grade1, $grade2) {
    if(is_numeric($grade1) && is_numeric($grade2)){
        $grades = array($grade1, $grade2);
    }
    elseif (is_numeric($grade1)) {
        $grade2 = 0;
        $grades = array($grade1, $grade2);
    }
    else {
        $grade1 = 0;
        $grades = array($grade1, $grade2);
    }
    return $grades
}

Will need to look more like that. You'll also want to actually add grade1 and grad2 to your returned array in your alternate conditions.

0
votes

The following is the classes enhanced. You will notice that they have constructors and functions to request variables. Private variables are usually the preferred method as they are protected and therefore cannot be modified outside the class.

class person {
    private $name = "";

    public function __construct ($nameV) {
        $this->name = $nameV;
    }

    public function getName() {
        return $this->name;
    }
}

class student extends person {

    private $grades;

public function __construct ($name, $grade1, $grade2) {
    parent::__construct($name);

    if ( ! is_numeric($grade1)) { $grade1 = 0; }
    if ( ! is_numeric($grade2)) { $grade2 = 0; }
    $this->grades = array($grade1, $grade2);
}

    public function average () {
        $length = count($this->grades);
        $total = 0;

      for ($i = 0; $i < $length; $i++) {
        $total = $total + $this->grades[$i];
      }

      $average = $total / $length;
      return $average;
    }
 }

The export code is then simply:

echo "<br/>" . $person1->getName() . " achieved an average of " . $person1->average();
echo "<br/>" . $person2->getName() . " achieved an average of " . $person2->average();
echo "<br/>" . $person3->getName() . " achieved an average of " . $person3->average();

Due to not having the form, I tested with the following data:

$person1 = new student ("XYZ", 1, 2);
$person2 = new student ("XYZ2", 100, 20);
$person3 = new student ("XYZ3", 95, 94);

Which exported:

XYZ achieved an average of 1.5
XYZ2 achieved an average of 60
XYZ3 achieved an average of 94.5