Question

Write a JavaScript program to get the volume of a Cylinder with four decimal places using...

Write a JavaScript program to get the volume of a Cylinder with four decimal places using object classes. Volume of a cylinder : V = πr2h where r is the radius and h is the height of the cylinder.

Your solution must include an HTML and a JavaScript file, with a button to create the cylinder, inputs for the cylinder's radius and height and an output to show the cyclinder's volume.

Your solution must also use an object for the cylinder, a constructor for passing the radius and height, and a getter for returning the volume.

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

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new web page with name "CylinderVolume.html" is created, which contains following code.

CylinderVolume.html :

<!DOCTYPE html>

<html lang="en">

<head>

<!-- title for web page -->

<title>Cylinder Volume</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- <script> refers to external javascript file -->

<script src="CylinderVolume.js"></script>

</head>

<body>

Radius :

<!-- textbox for radius of Cylinder -->

<input type="text" id="txtRadius" placeholder="Radius">

<br><br>

Height :

<!-- textbox for height of Cylinder -->

<input type="text" id="txtHeight" placeholder="Height">

<br><br>

<!-- button to call the javascript file -->

<input type="button" value="Volume of Cylinder" onclick="cylinderVolume()">

<br><br>

<!-- div to display cylinder volume -->

<div id="displayVolume"></div>

</body>

</html>

*******************************

CylinderVolume.js :

//External Javascript file

//Javascript class

class Cylinder

{

//constructor

constructor(radius, height) {

this.radius = radius;

this.height = height;

}

//method to return the volume of cylinder

getVolume() {

return Math.PI*this.radius*this.radius*this.height;

}

}

//function to calculate volume of cylinder

function cylinderVolume()

{

//get radius entered by user

var radius=document.getElementById("txtRadius").value;

//get height entered by user

var height=document.getElementById("txtHeight").value;

//creating object of Cylinder class

var c=new Cylinder(radius,height);

//calling method to get the volume of cylinder

var volume=c.getVolume();

//display cylinder volume on the div

document.getElementById("displayVolume").innerHTML="Volume of Cylinder : "+volume.toFixed(4);

}

======================================================

Output : Open web page CylinderVolume.html in the browser and will get the screen as shown below

Screen 1 :CylinderVolume.html

Screen 2 :Screen showing volume of cylinder

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
Write a JavaScript program to get the volume of a Cylinder with four decimal places using...
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
  • Need help with these scenarios Question 1 Write a JavaScript program to get the volume of...

    Need help with these scenarios Question 1 Write a JavaScript program to get the volume of a Cylinder with four decimal places using object classes. Volume of a cylinder : V = πr2h where r is the radius and h is the height of the cylinder. Your solution must include an HTML and a JavaScript file, with a button to create the cylinder, inputs for the cylinder's radius and height and an output to show the cyclinder's volume. Your solution...

  • Design the Xamarin Android Program to find the Volume of a Cylinder Volume of Cylinder =...

    Design the Xamarin Android Program to find the Volume of a Cylinder Volume of Cylinder = 3.14 * radius*radius*height; Display the output using a dialog box. Provide the input validation to accept numbers only File coding is one should be xml file and another one is C# file for coding Android coding

  • Write a program using the class to find out the area of a circle and volume...

    Write a program using the class to find out the area of a circle and volume of a cylinder. [* area of a circle=πr2, the volume of a cylinder=πr2*h where h is the height of the cylinder and r is the radius of the circular end of the cylinder] 1. The member variable should be private 2. Use public member functions to execute your program. 3. Use set ( ) function to access the private members 4. Use another approach...

  • Write a calculator program using JavaScript in HTML in the same HTML file. (There will only...

    Write a calculator program using JavaScript in HTML in the same HTML file. (There will only be 1 HTML file containing everything that you use for this program.) *************JavaScript Functions should be written in the HTML <head> .............. </head> tag, **************** (Please be mindful of the formatting of the text of your program. Meaning that do not copy paste everything in a single line. Add clear comments for a better understanding of your program) as follows Assignment: create a form...

  • Please help me to write a program in C++ with comments and to keep it simple...

    Please help me to write a program in C++ with comments and to keep it simple as possible. Thank you! Here is my program I have done before and that need to be change: #include <iostream> #include <iomanip> using namespace std; // function prototype – returning one value double cylvol(double, double); // Note: two data types inside brackets double surfarea(double, double); // Note: two data types inside brackets int main() { double r, l, V, S; cout << setprecision(2) <<...

  • The volume of a right circular cylinder is calculated by Trh where ris radius and his...

    The volume of a right circular cylinder is calculated by Trh where ris radius and his height Write a user-defined MATLAB function to compute the volume given radius and height. For the function name and arguments use Volume - Volume cylinder (Radius, Height). You do not need to call the function, you need to write the function as is would be in your current folder available to be called from a program. The function will take in the indicated inputs,...

  • Write a javascript file Create an object constructor (using new and function ) to hold a...

    Write a javascript file Create an object constructor (using new and function ) to hold a recipes information. It has to include properties for title (a string), servings (a number), and ingredients (an array of strings). It also has to have a method called print(nOfServings). in your app, create a single object using this contractor and call its print method On separate lines (a Document.write() statement for each), log (display on console) the recipe information so it looks like: myObj.print(2)...

  • JavaScript - Build with Node.js Follow the instructions bellow for a good rate Create a program...

    JavaScript - Build with Node.js Follow the instructions bellow for a good rate Create a program that will add, remove, get and display all items, stored in JSON file, based on user input (i.e. using yargs). You should choose your own individual purpose - it can be a warehouse, ticketing system, grading book, etc. Make sure you create an appropriate JSON object with real-life parameters - for example for user registration system you should use id, email, and a name....

  • C++ Chapter 10 defined the class circleType to implement the basic properties of a circle. (Add t...

    C++ Chapter 10 defined the class circleType to implement the basic properties of a circle. (Add the function print to this class to output the radius, area, and circumference of a circle.) Now every cylinder has a base and height, where the base is a circle. Design a class cylinderType that can capture the properties of a cylinder and perform the usual operations on the cylinder. Derive this class from the class circleType designed in chapter 10.   Some of the...

  • You shall write a very basic web server in Javascript that will run via nodejs. Your...

    You shall write a very basic web server in Javascript that will run via nodejs. Your project shall include the following line: var paul   = require('/homes/paul/HTML/CS316/p3_req.js'); Your project will accept HTTP requests via URLs in the following format:       /[a-zA-Z0-9_]*.cgi Here are the steps you must perform for .cgi URLs: 1) call http .createserver(myprocess) my process() is a function you will write to process requests from the user via their browser 2) create a mylisten() function that takes the following parameters:...

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