Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Represents an array of positive numbers
- *
- */
- class PositiveNumericArray {
- //Add a private member variable here
- private $inputs;
- private $sum;
- /*
- * Creates an instance of the PositiveNumericArray class
- *
- * @param array of integers
- */
- public function __construct(array $inputs) {
- $this->inputs = $inputs;
- }
- /**
- * getSum() returns the numeric sum of the input array elements
- * example the input array(3,2,1) should return 6 (3+2+1)
- *
- * @return integer total numeric content of the array
- */
- function getSum() : int {
- if($this->inputs != null){
- foreach ($this->inputs as $key => $value) {
- $this->sum = $this->sum + $value;
- }
- return $this->sum; //add code here to calculate the sum
- }else{
- return 0;
- }
- }
- /**
- * getSmallest() returns the lowest numeric item of the input array elements
- * example the input array(3,2,1) should return 1
- *
- * Note that for empty arrays the method is allowed to return any integer,
- * this will be solved in the next assignment
- *
- * @return integer lowest item of the array
- */
- function getSmallest() : int {
- if(count($this->inputs) > 0){
- $temp = 0;
- for($i = 0; $i < count($this->inputs); $i++){
- for($y = $i+1; $y < count($this->inputs); $y++){
- if($this->inputs[$i] > $this->inputs[$y]){
- $this->temp = $this->inputs[$i];
- $this->inputs[$i] = $this->inputs[$y];
- $this->inputs[$y] = $this->temp;
- }
- }
- }
- return $this->inputs[0];
- }else{
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement