Exercise PHP Language

Asked

Viewed 41 times

-5

Can someone help me:

In PHP: Create a class that has two methods; Method 1 calculates the module of the division of the registration number (any number) by an integer (you decide which integer to use); Method 2 will print on the screen the result obtained by the calculation performed in Method 1.

  • 3

    What is your question? What have you tried to do?

1 answer

1

It is not the purpose of the site to solve the question but rather to ask questions. But it seems you are new so I will answer your question. We can define this class in many ways, but I chose to make it as easy as possible. Follow the code:

<?php
    class Modulo{

        private $resto;

        public function restoDivisao($matricula, $denominador) {
            $this->resto = $matricula%$denominador;
        }

        public function imprimeResto() {
            echo $this->resto;
        }
    }
?>

Here we define the class that has only one parameter, which is the rest of the division. It is a private parameter, that is, it can only be accessed by the class’s own methods. To use this class you can do it as follows:

$resto = new Modulo;
$resto->restoDivisao(9,4);
$resto->imprimeResto();

First we instantiate the class, creating an object and storing it in the $resto variable. Then we call métido restoDivisao(), passing as parameters the number of the registration (any number) and the divisor. This method makes the calculation of the rest and stores it in the private attribute. Then we call the method imprimeResto() that takes the attribute we set in the previous method and prints on the screen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.