PDA

View Full Version : simple java help (homework)



collicott
03-01-2009, 01:47 AM
Cant figure out why this is not working any help would be great.

function bulletf2()
{
var bulletspeed = 1325;
var bulletDistance;
var bulletTime;
var bulletDistance = document.getElementById('InID2');
bulletTime= bulletDistance/ bulletspeed;
var bulletTime = document.getElementById('OutID2');

}



//-->
</script>
</head>
<body bgcolor="#C0C000">
<H1> #2. Midterm Test Practice for <font color="blue">extra</font></H1><HR/>
<!-- ---------------------------------------------------------------------- -->
<!-- Copy of your java script code for show! -->
<!-- ---------------------------------------------------------------------- -->
<P style="color: #0000ff; border=2">a view of your javascript code...</P>
<textarea style="background-color: #ffffcc;" cols=80 rows=8>
</textarea><hr/>
<P style="color: #800080">2. A bullet travels at 1325 meter's per second.
Write a program that will input the Distance and Show the Time.</P>
<H3>bullet Input Distance <input type="text" size="20" ID="InID2" onkeyup="bulletf2()" /></H3>
<H3>bullet Output Time <input type="text" size="40" ID="OutID2" /></H3>

Falkon303
03-01-2009, 02:17 AM
dude it's for a mid-term, I suggest you google the hell out of javascript.

I'll help you do this, but don't shoot yourself in the foot (pun!).

Nile
03-01-2009, 02:21 AM
1) You have to select the value, not the element
2) You have to assign the elements value bulletTime, not assign bulletTime's value to the element.

There is also a much easier way to do this which I have already done... I don't know if I should give it to you considering it's homework.

Nile
03-01-2009, 02:27 AM
Your code doesn't work...
You're missing a slash in it's.. should be: it\'s.
(btw: taking out the document.write, the code doesn't work. :p)

Falkon303
03-01-2009, 02:28 AM
Your code doesn't work...
You're missing a slash in it's.. should be: it\'s.

fixed it, thanx. :)

Nile
03-01-2009, 02:30 AM
Your code doesn't work though... Were you trying to make it work? Or help him?

Falkon303
03-01-2009, 02:33 AM
Cant figure out why this is not working any help would be great.

function bulletf2()
{
var bulletspeed = 1325;
var bulletDistance;
var bulletTime;
var bulletDistance = document.getElementById('InID2');
bulletTime= bulletDistance/ bulletspeed;
var bulletTime = document.getElementById('OutID2');

}



//-->
</script>
</head>
<body bgcolor="#C0C000">
<H1> #2. Midterm Test Practice for <font color="blue">extra</font></H1><HR/>
<!-- ---------------------------------------------------------------------- -->
<!-- Copy of your java script code for show! -->
<!-- ---------------------------------------------------------------------- -->
<P style="color: #0000ff; border=2">a view of your javascript code...</P>
<textarea style="background-color: #ffffcc;" cols=80 rows=8>
</textarea><hr/>
<P style="color: #800080">2. A bullet travels at 1325 meter's per second.
Write a program that will input the Distance and Show the Time.</P>
<H3>bullet Input Distance <input type="text" size="20" ID="InID2" onkeyup="bulletf2()" /></H3>
<H3>bullet Output Time <input type="text" size="40" ID="OutID2" /></H3>


here's the deal...
Let's say you have an input element right?
<input id="ninja" name="ninja" value="el">

ok, first off, if we want to replace this element's value, we use this
document.getElementById('ninja').value - "new text";

what we are doing is targetting the element based on the id="ninja" part of <input id="ninja" name="ninja" value="el"> .
what was wrong in your formula was that you were trying to divide raw string values.... first you have to wrap each value, like - parseFloat(bulletDistance) / parseFloat(the otherthing).

I think some of your values set for variables were absolutely blank as well...
Assign them like this ----
var BulletThingy = document.getElementById("whateverelement").value;

now to change the value of an element it's the other way...
document.getElementById("whateverelement").value = "7";

parseFloat("numericvalue") tells javascript that the values are to be treated as mathematical (correct me if I am wrong guys).

Nile
03-01-2009, 02:35 AM
Ehh... Almost...
Here was trying to divide raw html objects... In javascript, it does:



[HtmlInputObject] / 1325

Or something like that.

(your code doesn't work!)

Falkon303
03-01-2009, 02:40 AM
Your code doesn't work though... Were you trying to make it work? Or help him?


I screwed it up when I was trying to be witty.... damn you pride! Foiled again!


<script type="text/javascript">
function bulletf2(value)
{
var bulletspeed = 1325;
var bulletDistance = value;
var bulletTime;
var bulletDistance = document.getElementById('InID2').value;
bulletTime= parseFloat(bulletDistance)/ parseFloat(bulletspeed);
document.getElementById('OutID2').value = bulletTime;
}
//-->
</script>
</head>
<body bgcolor="#C0C000">
<H1> #2. Midterm Test Practice for <font color="blue">extra</font></H1><HR/>
<!-- ---------------------------------------------------------------------- -->
<!-- Copy of your java script code for show! -->
<!-- ---------------------------------------------------------------------- -->
<P style="color: #0000ff; border=2">a view of your javascript code...</P>
<textarea style="background-color: #ffffcc;" cols=80 rows=8>
</textarea><hr/>
<P style="color: #800080">2. A bullet travels at 1325 meter's per second.
Write a program that will input the Distance and Show the Time.</P>
<H3>bullet Input Distance <input type="text" size="20" ID="InID2" onkeyup="bulletf2(this.value)" /></H3>
<H3>bullet Output Time <input type="text" size="40" ID="OutID2" /></H3>

Nile
03-01-2009, 02:42 AM
Well... if were giving answers:


<script type="text/javascript">
var calculation = {
metersps: 1325,
calcTime: function(me, outputEl){
me.value = parseFloat(me.value);
outputEl.value = parseFloat(me.value) / this.metersps;
},
calcDistance: function(me, outputEl){
me.value = parseFloat(me.value);
outputEl.value = parseFloat(me.value) * this.metersps;
}
};
</script>
Distance:<br />
<input type="text" name="distance" id="inputDist" onkeyup="calculation.calcTime(this, document.getElementById('inputTime'));" />
<br /><br />
Time:<br />
<input type="text" name="time" id="inputTime" onkeyup="calculation.calcDistance(this, document.getElementById('inputDist'));" />
<br />


That one can go both ways... You can type the distance or time.

Falkon303
03-01-2009, 02:47 AM
fricking elite coders.....

*runs home sobbing*

hahaha!

I'll have to check your code out. I just hope people who "get answers" know that the answers aren't the answer.... It's learning how stuff works that is important. Specially in school.

I think your answer is very cool Nile... I am convinced that is the best one to use for the extra credit actually.

Twey
03-01-2009, 03:29 AM
I've run all of these solutions through my Java compiler and none of them compile! What's up with that?!

:rolleyes:

Nile
03-01-2009, 03:37 AM
Lol... javascript. I should make a blog post about that!

Twey
03-01-2009, 04:31 AM
Try to separate your pure code from your impure code. In the case of Nile's script, for example, though the namespacing object is (or should be, given the convention for capitalising namespaces) called 'Calculation', the functions within it actually do more than that: they handle input and output, via DOM manipulation, as well. This can (and should) be separated from the actual calculation functions, since it's really a separate task.

Likewise, there are several repetitive tasks going on here that could really be handled generically.

Also, everyone, watch your markup.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Distance Calculations</title>
<script type="text/javascript">
var Operator = {
'/' : function(a, b) { return a / b; },
'*' : function(a, b) { return a * b; }
};

Math.generateTriangle = function(tipName,
bottomLeftName,
bottomRightName) {
var r = {
_tip: tipName,
_bl: bottomLeftName,
_br: bottomRightName
};

r['find' + tipName.toTitleCase()] = Operator['*'];
r['find' + bottomLeftName.toTitleCase()] = Operator['/'];
r['find' + bottomRightName.toTitleCase()] = Operator['/'];

return r;
};

function bind(obj, fun, args) {
return function() {
if (obj === true)
obj = this;

var f = typeof fun === "string" ? obj[fun] : fun;

return f.apply(obj, Array.prototype.slice.call(args || [])
.concat(Array.prototype.slice.call(arguments)));
};
}

Array.map = function(f, a) {
for (var i = a.length - 1, r = []; i >= 0; --i)
r[i] = f(a[i], i);

return r;
};

String.upperFirst = function(s) {
return s.charAt(0).toUpperCase() + s.substr(1);
};

String.prototype.toTitleCase = function() {
return Array.map(String.upperFirst, this.split(" ")).join(" ");
};

String.prototype.toUnderscored = function() {
return this.toLowerCase().replace(/ /g, "_");
};

var Forms = (function() {
function getValue(form, element) {
if (typeof form === "string")
form = document.forms[form];

return form.elements[element].value;
}

function withElements(form, func, result) {
if (typeof form === "string")
form = document.forms[form];

return function() {
var r = func.apply(this,
Array.map(bind(true, getValue, [form]),
arguments));

return result ? (form.elements[result].value = r) : r;
};
}

function wrapTriangleMethod(obj, form, triangle, dep1, dep2, ind) {
obj['set' + triangle[ind].toTitleCase()] = bind(true,
wrapForm(form,
triangle['find' + triangle[ind].toTitleCase()],
triangle[ind].toUnderscored()),
[triangle[dep1].toUnderscored(), triangle[dep2].toUnderscored()]);
}

function wrapTriangle(form, triangle) {
var r = { _triangle: triangle },
wrapper = bind(true, wrapTriangleMethod, [r, form, triangle]);

wrapper('_br', '_bl', '_tip');
wrapper('_tip', '_br', '_bl');
wrapper('_tip', '_bl', '_br');

return r;
}

return {
getValue: getValue,
withElements: withElements,
wrapTriangle: wrapTriangle
};
})();

var Physics = {
Speed: Math.generateTriangle("distance", "speed", "time")
};

var SpeedCalculator = Forms.wrapTriangle("speed_calc", Physics.Speed);
</script>
</head>

<body>
<form action="" id="speed_calc">
<table>
<tr>
<th>Speed:</th>
<td>
<input type="text" name="speed" value="0">
</td>
<td>
<input type="button"
value="Calculate"
onclick="return !!SpeedCalculator.setSpeed();">
</td>
</tr>
<tr>
<th>Distance:</th>
<td>
<input type="text" name="distance" value="0">
</td>
<td>
<input type="button"
value="Calculate"
onclick="return !!SpeedCalculator.setDistance();">
</td>
</tr>
<tr>
<th>Time:</th>
<td>
<input type="text" name="time" value="0">
</td>
<td>
<input type="button"
value="Calculate"
onclick="return !!SpeedCalculator.setTime();">
</td>
</tr>
</table>
</form>
</body>
</html>

Untested.

Nile
03-01-2009, 04:35 AM
Isn't it kind of unreasonable to have code that long, when it could be 4 lines long?

Twey
03-01-2009, 05:08 AM
To some extent. The version I posted above was horrifically over-engineered for a Javascript homework assignment. However, if you're going to be using this as part of a big system, no. Everything but one line of that JS (excepting handlers) is in reusable components. That means that if you create a similar page for, say, voltage/current/resistance, it's basically two lines of code*:
Physics.Voltage = Math.generateTriangle("voltage", "current", "resistance");
var VoltageCalculator = Forms.wrapTriangle("voltage_calc", Physics.Voltage);

* OK, and an include — make it three lines, then.

collicott
03-02-2009, 03:36 AM
I screwed it up when I was trying to be witty.... damn you pride! Foiled again!


<script type="text/javascript">
function bulletf2(value)
{
var bulletspeed = 1325;
var bulletDistance = value;
var bulletTime;
var bulletDistance = document.getElementById('InID2').value;
bulletTime= parseFloat(bulletDistance)/ parseFloat(bulletspeed);
document.getElementById('OutID2').value = bulletTime;
}
//-->
</script>
</head>
<body bgcolor="#C0C000">
<H1> #2. Midterm Test Practice for <font color="blue">extra</font></H1><HR/>
<!-- ---------------------------------------------------------------------- -->
<!-- Copy of your java script code for show! -->
<!-- ---------------------------------------------------------------------- -->
<P style="color: #0000ff; border=2">a view of your javascript code...</P>
<textarea style="background-color: #ffffcc;" cols=80 rows=8>
</textarea><hr/>
<P style="color: #800080">2. A bullet travels at 1325 meter's per second.
Write a program that will input the Distance and Show the Time.</P>
<H3>bullet Input Distance <input type="text" size="20" ID="InID2" onkeyup="bulletf2(this.value)" /></H3>
<H3>bullet Output Time <input type="text" size="40" ID="OutID2" /></H3>

thanks for your help, everyone who posted. I actually understand how to do the whole document.getbyelement thingy. Couldnt have done it without you!! Thanks guys
collicott