Question

PHP Create a Bicycle class. Class attributes include: brand, model, year, description, and weight_kg (weight in...

PHP

Create a Bicycle class. Class attributes include: brand, model, year, description, and weight_kg (weight in kilograms).

Write three methods (functions) for the Bicycle class: name, weight_pounds, and set_weight_pounds. The name function should return the concatenation of brand, model, and year. The weight_pounds function should convert kilograms to pounds (kg * 2.2046226218) and return it as a floatval data type. The set_weight_pounds function should convert pounds to kilograms ( floatval($value) / 2.2046226218). When the set_weight_pounds function is called, it also passes to the function the weight in pounds. The function then converts $value to kilograms.

Create two objects and set the each of the properties for the objects. Then display the name of each object, the weight in kilograms, and the weight in pounds.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Open any text editor

2. Type the following code

<!–– basic html syntax ––>
<html>
<head>
<title>phpclass</title>
</head>
<body>  
</body>
</html>
<!–– basic php class ––>
<?php
//class definition
class Bicycle {
   //variables
   private $brand;
   private $model;
   private $year;
   private $description;
   private $weight_kg;
   //constructor
   function __construct( $brand, $model, $year, $description, $weight_kg ) {
       $this->brand = $brand;
       $this->model = $model;
       $this->year = $year;
       $this->description = $description;
       $this->weight_kg = $weight_kg;
   }
   //function to get the name
    function name() {
       //returning the name
       return $this->brand." ".$this->model." ".$this->year;
   }
   //function to convert kg to pounds
    function weight_pounds() {
       //returning the weight in pound
       return floatval($this->weight_kg * 2.2046226218);
   }
   //function yo get the weigth in kg
   function set_weight_pounds($value){
       //returning value in kg
       return floatval($value) / 2.2046226218;
   }
   //end of class
}
//creating first object
$obj = new Bicycle("Avon","Atlas",2012,"heavy duty ", 23.5);
//calling function to get the name
echo "Name is ". $obj->name();
//moving to new line
echo "<br>";
//calculating the weight in pounds by calling function and storing the value
$weight=$obj->weight_pounds();
//printing the weight in pounds
echo "Weight in pounds is $weight";
//moving to new line
echo "<br>";
//calling function to convert the pounds weight in kg again
echo "Weight in kg is ". $obj->set_weight_pounds($weight);

//moving to new line
echo "<br>";
//creating Second object
$obj1 = new Bicycle("Hercules","Ninza",2018,"Terrain", 12.6);
//calling function to get the name
echo "Name is ". $obj1->name();
//moving to new line
echo "<br>";
//calculating the weight in pounds by calling function and storing the value
$weight=$obj1->weight_pounds();
//printing the weight in pounds
echo "Weight in pounds is $weight";
//moving to new line
echo "<br>";
//calling function to convert the pounds weight in kg again
echo "Weight in kg is ". $obj1->set_weight_pounds($weight);

?>

3. Save as index.php

4. Execute it using any web server (I used xampp server)

Output:

Add a comment
Know the answer?
Add Answer to:
PHP Create a Bicycle class. Class attributes include: brand, model, year, description, and weight_kg (weight in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ HELP! Create a class and name it Payslip. This class should have the following attributes...

    C++ HELP! Create a class and name it Payslip. This class should have the following attributes or properties: name, pay grade, basic salary, overtime hours, overtime pay, gross pay, net pay and withholding tax. Define the accessors and mutators of the Payslip class based on its attributes or properties. This class should also have the following methods: determinePayGradeAndTaxRate and computePay. Create another class and name it Employee. This class will contain the main method. In the main method, instantiate an...

  • In Java Create a class Worker. Your Worker class should include the following attributes as String...

    In Java Create a class Worker. Your Worker class should include the following attributes as String variables: Name, Worker ID, Worker Address, Worker City, Worker State Create the constructor that initializes the above worker attributes. Then make another class name HourlyWorker that inherits from the Worker class.   HourlyWOrker has to use the inherited parent class variables and add in hourlySalary and billableHours. Your HourlyWorker class should contain a constructor that calls the constructor from the Worker class to initialize the...

  • create a java class named Person that has 3 attributes: name, age and weight. include a...

    create a java class named Person that has 3 attributes: name, age and weight. include a default and parametrize constructor. include all setters and getters needed and a method that is used to display the information about the person

  • python code? 1. Create a class called Person that has 4 attributes, the name, the age,...

    python code? 1. Create a class called Person that has 4 attributes, the name, the age, the weight and the height [5 points] 2. Write 3 methods for this class with the following specifications. a. A constructor for the class which initializes the attributes [2.5 points] b. Displays information about the Person (attributes) [2.5 points] c. Takes a parameter Xage and returns true if the age of the person is older than Xage, false otherwise [5 points] 3. Create another...

  • 1. An airplane model can be described (highly oversimplified) by the following attributes • model: string...

    1. An airplane model can be described (highly oversimplified) by the following attributes • model: string • emptyweight: double • number of seats: int • fuel Consumption: double a) Define a class AirplaneModel with the attributes defined above. b) Your class should contain the following member functions: • A default constructor that initializes the class’s attributes. • A constructor that sets the attributes. • string getName() to get the model’s name • double getEmptyWeight() to get the model’s empty weight...

  • Create an abstract class Employee. Your Employee class should include the following attributes: First name (string)...

    Create an abstract class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings.      Create another class HourlyEmployee that inherits from the abstract Employee class.   HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should...

  • Create an abstract class Employee. Your Employee class should include the following attributes: First name (string)...

    Create an abstract class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings.      Create another class HourlyEmployee that inherits from the abstract Employee class.   HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should...

  • Create a Python file (book.py) that has a class named Book. Book class has four attributes,...

    Create a Python file (book.py) that has a class named Book. Book class has four attributes, title (string), author(string), isbn(string), and year(int). Title - the name of the book. Author - the name of the persons who wrote the book. ISBN - is a unique 13 digit commercial book identifier. Year - the year the title was printed. Your class should have the following methods: (1) __init__(self, title, author, isbn, year): This method called when an object (book) is created...

  • Create a Python file (book.py) that has a class named Book. Book class has four attributes, title...

    Create a Python file (book.py) that has a class named Book. Book class has four attributes, title (string), author(string), isbn(string), and year(int). Title - the name of the book. Author - the name of the persons who wrote the book. ISBN - is a unique 13 digit commercial book identifier. Year - the year the title was printed. Your class should have the following methods: (1) __init__(self, title, author, isbn, year): This method called when an object (book) is created...

  • c++ driver and car are independed classes 1. Create a class Car, which has a color, engine, horsepower, fuel, FuelLevel, year of manufacturing and driver which can be defined by name, age, licen...

    c++ driver and car are independed classes 1. Create a class Car, which has a color, engine, horsepower, fuel, FuelLevel, year of manufacturing and driver which can be defined by name, age, licenseNumber. Create the corresponding OOP in For each class (Driver, Car) write a header file and the class implementation -In the main function do the following: from that class in main function. L.1 Write a function that will print the total number of objects created 12 Define an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT