-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassesExercises.php
More file actions
99 lines (74 loc) · 2.34 KB
/
Copy pathClassesExercises.php
File metadata and controls
99 lines (74 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
class MyClass {
// Define a constructor method
public function __construct()
{
// Output a message indicating that the class has been initialized
echo "MyClass class has initialized !". "\n";
}
}
// create an instance of the MyClass class
$userclass = new MyClass;
// Q2
class Introduction{
public $message = 'Hello All, I am ';
// Define a method named introduce which accepts a parameter $name
public function introduce($name)
{
// Concatenate the message with the provided name and return the result
return $this -> message.$name;
}
}
// Create an instance of the user_message class
$mymessage = new Introduction();
// Call the introduction method of the $mymessage object and output the result
echo $mymessage -> introduce('Scott')."\n";
// Q3
class Factorial{
protected $_n;
public function __construct($n)
{
// check if the parameter $n is not an integer
if(!is_int($n)){
throw new InvalidArgumentException('Not a number or missing argument');
}
// Assign the value of $n to the protected property $_n
$this -> _n = $n;
}
// Define a method named result to calculate the factorial of the number
public function result()
{
// Initialize the factorial variable to 1
$factorial = 1;
// Iterate from 1 to the value of $_n
for ($i = 1; $i <= $this-> _n; $i++) {
// Multiply the factorial by the current value of $i
$factorial *= $i;
}
// Return the calculated factorial
return $factorial;
}
}
// Create an instance of the factorial_of_a_number class with the argument 5
$newfactorial = new Factorial(5);
// Call the result method of the $newfactorial object and output the result
echo $newfactorial -> result() . "\n";
// Q4
class Sort{
protected $_asort;
public function __construct(array $asort)
{
$this -> _asort = $asort;
}
// Define a method named alhsort to sort the array in ascending order
public function alhsort(){
$sorted = $this -> _asort;
// Sort the copied array in ascending order
sort($sorted);
// Return the sorted array
return $sorted;
}
}
$newSort = new Sort(array(11, 2, 4, 0 , -9));
print_r($newSort -> alhsort()). "\n";
?>