The Quadratic Object

Function and Object

Page 1 of 1

4 Replies - 1027 Views - Last Post: 10 November 2008 - 01:59 PM

#1 ronzam2002  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 12-June 08

The Quadratic Object

Post icon  Posted 09 November 2008 - 07:10 AM

Create a Quadratic object based on the model in the figure.
this is the formula for quadratic equation: x1=x2 = -b+/- sqrt(b^2-4*a*c)/2*a

Quadratic

Properties a,b,c
Methods Quadratic() //Constructor
setA() //Change a
setB() //Change b
setC() //Change c

checkRoots() //Return the positive roots
posRoots() //Return the number of roots
negRoot() //Return the negative root
print() //Print the values in a,b, c

Well I already made a code of this, but is not working. I don't know what are the errors in this code. Can anyone find it and fix it...!!!!

<html>
<head><title>The Roots of Quadratic Function Page</title></head>
<body bgcolor = "gold">
<i><The Cirle Page</i>
<p>This program will calculate the positive and negative roots of the quadratic equation.</p>

  

 <br>

<IMG SRC="http://fym.la.asu.edu/~tturner/MAT_117_online/Study_guides/UsefulFormulasfortheFinalfiles/quadratic-formula.gif" WIDTH="250" HEIGHT="150" BORDER="0" ALT="text describing the image">
 <ol>
   <li>Enter the value of a.
   <li>Enter the value of b.
	<li>Enter the value of c.
  </ol>


 <script language="Javascript">
 <!--
  function getFloat(message)
{
  
  var x = parseFloat(prompt(message, ""));
  while(isNaN(x))
	   x = parseFloat(prompt("Error! Non-numeric Data\n" + message, ""));
  return x;

}
 function roundPlaces(number, places)
 {
   return Math.round(number* Math.pow(10, places))/ Math.pow(10,places);
}

  function qposRoot()
 {
   return roundPlaces(-this.b+Math.sqrt(Math.pow(this.b,2)-4*this.a*this.c)/2*this.a,2);

 }
  function qnegRoot()
  {
   return roundPlaces(-this.b-Math.sqrt(Math.pow(this.b,2)-4*this.a*this.c)/2*this.a,2);
   }
  function qPrint()
  {
  document.write("<br><br>a= " + this.a);
  document.write("<br>b=" + this.b);
  document.write("<br>c=" + this.c);
  document.write("<br>x1= " + this.posRoot());
  document.write("<br>x2= " + this.negRoot());
}
  function setA(a)
  {
  this.a=a;
 }
  function setB(b)
  {
  this.b=b;
  }
  function setC(c)
  {
  this.c=c;
  }
  function Quadratic(a)
  {
   this.a=a;
   this.posRoot=qposRoot;
   this.negRoot=qnegRoot;
   this.setA=setA;
   this.print=qPrint;
  }
function Quadratic(b)
  {
   this.b=b;
   this.posRoot=qposRoot;
   this.negRoot=qnegRoot;
   this.setB=setB;
   this.print=qPrint;
  }
function Quadratic(c)
  {
   this.c=c;
   this.posRoot=qposRoot;
   this.negRoot=qnegRoot;
   this.setC=setC;
   this.print=qPrint;
  }
   
//-->
</script>
</body>
</html>   

 


Is This A Good Question/Topic? 0
  • +

Replies To: The Quadratic Object

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1201
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: The Quadratic Object

Posted 09 November 2008 - 09:11 AM

Okay, after a quick look through the code you never actually call to the functions, so it isn't running anything. In reality you only really need one function to take all three of the quadratic variables (a b and c) so you may want to try to get that working.

Here is a quick quadratic code I made that you can use for reference:
<script>
function Quadratic(a, b, c){
  this.a = a;
  this.b = b;
  this.c = c;
  this.percision = 2;
  this.setA = function(val){
	a = val;
  }
  this.setB = function(val){
	b = val;
  }
  this.setC = function(val){
	c = val;
  }
  this.setPercision = function(val){
	this.percision = val;
  }
  this.toPower = function(){
	var retVal = 1;
	for(var i=0; i<this.percision; i++){
	  retVal *= 10;
	}
	return retVal;
  }
  this.toPercision = function(value){
	pow = this.toPower();
	return Math.round(value*pow)/pow;
  }
  this.findRoot = function(){
	return [this.toPercision((-b-Math.sqrt((b*b)-(4*a*c)))/(2*a)), this.toPercision((-b+Math.sqrt((b*b)-(4*a*c)))/(2*a))];
  }
}
var test = new Quadratic(-2, 7, 15);
test.setB(0);
test.setPercision(5);
alert(test.findRoot()[0]);
</script>


The functions are all fairly simple to, it has setA, setB, setC, setPercision, toPercision (used by findRoot), toPower (used by toPercision), and findRoot (returns a tuple (an array of length 2)).

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 ronzam2002  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 12-June 08

Re: The Quadratic Object

Posted 09 November 2008 - 07:26 PM

Hi.. I tried to execute your code, it only displayed an alert window, it says only 2.73861. I don't know if it is correct.
I thought the program will ask for a value for a, b, and c and display the values of a,b, c, and the resalt of the 2 roots. Well the problem is not quite clear for me, so I'm not sure if your solution is right.





View PostBetaWar, on 9 Nov, 2008 - 08:11 AM, said:

Okay, after a quick look through the code you never actually call to the functions, so it isn't running anything. In reality you only really need one function to take all three of the quadratic variables (a b and c) so you may want to try to get that working.

Here is a quick quadratic code I made that you can use for reference:
<script>
function Quadratic(a, b, c){
  this.a = a;
  this.b = b;
  this.c = c;
  this.percision = 2;
  this.setA = function(val){
	a = val;
  }
  this.setB = function(val){
	b = val;
  }
  this.setC = function(val){
	c = val;
  }
  this.setPercision = function(val){
	this.percision = val;
  }
  this.toPower = function(){
	var retVal = 1;
	for(var i=0; i<this.percision; i++){
	  retVal *= 10;
	}
	return retVal;
  }
  this.toPercision = function(value){
	pow = this.toPower();
	return Math.round(value*pow)/pow;
  }
  this.findRoot = function(){
	return [this.toPercision((-b-Math.sqrt((b*b)-(4*a*c)))/(2*a)), this.toPercision((-b+Math.sqrt((b*b)-(4*a*c)))/(2*a))];
  }
}
var test = new Quadratic(-2, 7, 15);
test.setB(0);
test.setPercision(5);
alert(test.findRoot()[0]);
</script>


The functions are all fairly simple to, it has setA, setB, setC, setPercision, toPercision (used by findRoot), toPower (used by toPercision), and findRoot (returns a tuple (an array of length 2)).

Hope that helps.

This post has been edited by ronzam2002: 09 November 2008 - 07:35 PM

Was This Post Helpful? 0
  • +
  • -

#4 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: The Quadratic Object

Posted 10 November 2008 - 12:44 PM

In your example you don't have anywhere the program would ask for inputs, so in BetaWar's example he has:
var test = new Quadratic(-2, 7, 15);



If you want to have it prompt for the values, you could put something like
var a = prompt("A is .. "); var b = prompt("B is.. "); 
var c = prompt("C is .. ");
var test = new Quadratic(a, b, c);



It doesn't appear you have much understanding of javascript, so if this is for a course I suggest you go through the code to figure out how it actually works.

This post has been edited by mocker: 10 November 2008 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1201
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: The Quadratic Object

Posted 10 November 2008 - 01:59 PM

There was a minor error with the code (which caused it to be off in its results a bit) but I have fixed that:

<script>
function Quadratic(a, b, c){
  this.a = a;
  this.b = b;
  this.c = c;
  this.percision = 2;
  this.setA = function(val){
	this.a = val;
  }
  this.setB = function(val){
	this.b = val;
  }
  this.setC = function(val){
	this.c = val;
  }
  this.setPercision = function(val){
	this.percision = val;
  }
  this.toPower = function(){
	var retVal = 1;
	for(var i=0; i<this.percision; i++){
	  retVal *= 10;
	}
	return retVal;
  }
  this.toPercision = function(value){
	pow = this.toPower();
	return Math.round(value*pow)/pow;
  }
  this.findRoot = function(){
	var a = this.toPercision(((-1*this.b)-Math.sqrt((this.b*this.b)-(4*this.a*this.c)))/(2*this.a));
	var b = this.toPercision(((-1*this.b)+Math.sqrt((this.b*this.b)-(4*this.a*this.c)))/(2*this.a));
	return [b, a];
  }
}
var test = new Quadratic(-2, 7, 15);
test.setB(0);
test.setPercision(5);
alert(test.findRoot()[0]);
</script>


Basically, mocker is correct, you have to add the input on your own, it is set with values A B and C being set to (-2, 7, and 15) respectively.

When you call to the findRoot() function it returns an array of answers (which if you try to alert out will say "Object" so you have to tell it what item in the array you want to see hence the findRoot()[0], which shows the first element in the array returned.

I also agree that you should go through the code and try to learn from it.

Hope that makes sense.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1