Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MODIFY THE FOLLOWING PHP SCRIPT AS FOLLOWS: 1. Add an additional property called

ID: 3906402 • Letter: M

Question

MODIFY THE FOLLOWING PHP SCRIPT AS FOLLOWS:

1. Add an additional property called "type" to the Pet class

2. Set all data fields to "private"

3. Provide setter and getter methods for these private data fields

4. Create a new method called "talk" in the Pet class to display the message like this:

Bucky is talking. //Bucky is the actual name of pet

5. override the "talk" method in both the Cat and Dog classes to say:

Bucky meows.   //for a cat
Snoopy barks.   //for a dog

6. Create and modify all constructors accordingly to initialize the data fields

7. Add the additional codes to display the cat and dog types.

8. Add the additional code to have the pets talk.

1    <!doctype html>

2    <html lang="en">

3    <head>

4       <meta charset="utf-8">

5       <title>Pets</title>

6       <link rel="stylesheet" href="style.css">

7    </head>

8    <body>

9    <?php # Script 5.3 - pets2.php

10   // This page defines and uses the Pet, Cat, and Dog classes.

11

12   # ***** CLASSES ***** #

13

14   /* Class Pet.

15    * The class contains one attribute: name.

16    * The class contains four methods:

17    * - _ _construct()

18    * - eat()

19    * - sleep()

20    * - play()

21    */

22   class Pet {

23      public $name;

24      function _ _construct($pet_name) {

25         $this->name = $pet_name;

26      }

27      function eat() {

28         echo "<p>$this->name is eating.</p>";

29      }

30      function sleep() {

31         echo "<p>$this->name is sleeping.</p>";

32      }

33

34      // Pets can play:

35      function play() {

36         echo "<p>$this->name is playing.</p>";

37      }

38

39   } // End of Pet class.

40

41   /* Cat class extends Pet.

42    * Cat overrides play().

43    */

44   class Cat extends Pet {

45      function play() {

46         echo "<p>$this->name is climbing.</p>";

47      }

48   } // End of Cat class.

49

50   /* Dog class extends Pet.

51    * Dog overrides play().

52    */

53   class Dog extends Pet {

54      function play() {

55         echo "<p>$this->name is fetching.</p>";

56      }

57   } // End of Dog class.

58

59   # ***** END OF CLASSES ***** #

60

61   // Create a dog:

62   $dog = new Dog('Satchel');

63

64   // Create a cat:

65   $cat = new Cat('Bucky');

66

67   // Create an unknown type of pet:

68   $pet = new Pet('Rob');

69

70   // Feed them:

71   $dog->eat();

72   $cat->eat();

73   $pet->eat();

74

75   // Nap time:

76   $dog->sleep();

77   $cat->sleep();

78   $pet->sleep();

79

80   // Have them play:

81   $dog->play();

82   $cat->play();

83   $pet->play();

84

85   // Delete the objects:

86   unset($dog, $cat, $pet);

87

88   ?>

89   </body>

90   </html>

Explanation / Answer

here is your modified code : ---------->>>>>>>>>

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
  class Pet{
   private $name;
   private $type;

   function __construct($pet_name,$pet_type){
    $this->name = $pet_name;
    $this->type = $pet_type;
   }

   function setName($petName){
    $this->name = $petName;
   }

   function setType($petType){
    $this->type = $petType;
   }

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

   public function getType(){
    return $this->type;
   }

   function talk(){
    echo "<p>$this->name is talking.</p>";
   }

   function eat(){
    echo "<p>$this->name is eating.</p>";
   }

   function sleep(){
    echo "<p>$this->name is sleeping.</p>";
   }

   function play(){
    echo "<p>$this->name is playing.</p>";
   }
  }

  class Cat extends Pet{
   function play(){
    $n = $this->getName();
    echo "<p>$n is climbing.</p>";
   }

   function talk(){
    $n = $this->getName();
    echo "<p>$n meows.</p>";
   }
  }

  class Dog extends Pet{
   function play(){
    $n = $this->getName();
    echo "<p>$n is fetching.</p>";
   }

   function talk(){
    $n = $this->getName();
    echo "<p>$n barks.</p>";
   }
  }


  $dog = new Dog('Satchel','Bulldog');
  $cat = new Cat('Bucky','Tabby');

  $pet = new Pet('Rob','Anynomous');

  //Feed Them
  $dog->eat();
  $cat->eat();
  $pet->eat();

  //Nap Them :
  $dog->sleep();
  $cat->sleep();
  $pet->sleep();

  //Have them play:
  $dog->play();
  $cat->play();
  $pet->play();

  //Have them talk
  $dog->talk();
  $cat->talk();
  $pet->talk();

  //Type of the pets
  $n = $dog->getName();
  $t = $dog->getType();
  echo "<p>$n Type : $t.</P>";
  $n = $cat->getName();
  $t = $cat->getType();
  echo "<p>$n Type : $t.</P>";
  $n = $pet->getName();
  $t = $pet->getType();
  echo "<p>$n Type : $t.</P>";
?>
</body>
</html>