Assignment 6
using php
Create a Web application that get the radius value for user, when user click area button: computeArea should be executed, and when user click Circumference button: computeCircumference should be executed.
1. Class User, requirements are:
- Three properties (id, name, email).
- Setters and getters for each property.
- Helper method to validate email if valid as the following format (letters+-+digits@letter+digits or just letters .com).
- Helper method to validate id if valid as the following format (UR+-+4 digits).
- Any object of user should be initialized automatically at creation time.
2. Class Circle , requirements are:
- Two properties: radius, constant PI.
- Setter and getter for radius.
- Constructor initialize radius.
- Destructor print “ Circle class Destructor”
- Helper method to validate if radius is number when set value to it.
- Two methods: computeArea , computeCircumference.
Area= radius^2 * PI
Circumference= 2*radius*PI
Expert Answer
- Comment
<?php // Class User class User { // Three properties (id, name, email) private $id; private $name; private $email; // Setter method for id function set_id($id) { if($this->is_valid_id($id)) { $this->id = $id; } } // Setter method for name function set_name($name) { $this->name = $name; } // Setter method for email function set_email($email) { if($this->is_valid_email($email)) { $this->email = $email; } } // Getter method for id function get_id() { return $this->id; } // Getter method for name function get_name() { return $this->name; } // Getter method for email function get_email() { return $this->email; } // Helper method to validate email if valid private function is_valid_email($email) { return filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/@.+\./', $email); } // Helper method to validate id if valid as the following format (UR+-+4 digits) private function is_valid_id($id) { return (bool) preg_match('/^UR-[0-9]{4}$/', $id); } } // Create an object of User $user = new User(); $user->set_id("UR-1234"); // Set id $user->set_name("Jerry"); // Set name $user->set_email("jerry@tom.com"); // Set email // Display the details echo "<b>Name:</b> ".$user->get_name()."<br>"; echo "<b>ID:</b> ".$user->get_id()."<br>"; echo "<b>Email:</b> ".$user->get_email()."<br>"; // Class Circle class Circle { // Two properties: radius, constant PI. private $radius; const PI = 3.14; // Constructor to initialize radius. function __construct($radius) { if($this->is_valid_radius($radius)) { $this->radius = $radius; } } // Destructor to print “Circle class Destructor” function __destruct() { print "Circle class Destructor"; } // Setter method for radius function set_radius($radius) { if($this->is_valid_radius($radius)) { $this->radius = $radius; } } // Getter method for radius function get_radius() { return $this->radius; } // Helper method to validate if radius is number when set value to it. private function is_valid_radius($radius) { if($radius > 0) { return true; } else { return false; } } // Method to compute compute area of circle function computeArea() { return self::PI * $this->radius * $this->radius; } // Method to compute circumference area of circle function computeCircumference() { return 2 * self::PI * $this->radius; } } if(isset($_POST['radius'])) { // Create an object of Circle $circle = new Circle((int) $_POST['radius']); if(isset($_POST['computeArea'])) { // when user click Circumference button: computeCircumference will be executed. print "<br>Radius: ".$circle->get_radius()."<br>Area: ".$circle->computeArea()."<br>"; } if(isset($_POST['computeCircumference'])) { // when user click Area button: computeArea will be executed. print "<br>Radius: ".$circle->get_radius()."<br>Circumference: ".$circle->computeCircumference()."<br>"; } } ?> <html> <head> <title>Circle</title> </head> <body> <br /><br /> <form action="" method="POST"> Radius: <input type="number" name="radius" /> <br /><br /> <button type="submit" name="computeArea">Area</button> <button type="submit" name="computeCircumference">Circumference</button> </form> </body> </html>
Screenshot of the code:
Sample Execution and INPUTS and OUTPUTS:
when user clicks Circumference button:
when user clicks Area button:
**If you have any doubt, please ask in the comment. I will definitely help you.**
ليست هناك تعليقات:
إرسال تعليق