|
-
May 13th, 2007, 07:41 AM
#1
Help!
I've just started doing Java, and as usual, everything is going over my head at this point of time, can someone please help me with these codes, please?
I'll most greatful.
A JavaScript program is intended to accept two numbers from a user,
then write the sum and difference of the two numbers to the browser
window. For example, if the user inputs36 and 24 the program should
write
The sum of 36 and 24 is 60
The difference of 36 and 24 is 12
However, the current version (Figure6) contains several errors. Your
task is to identify them, correct the program, and write a short
explanation of each of your changes.
<SCRIPT LANGUAGE="JavaScript">
// A program to find the sum and difference of two numbers
var firstNum;
var secondNum;
firstNum = window.prompt('Please enter the first number
′′
, '');
firstNum = ParseFloat(firstNum);
secondNum = window.prompt('Please enter the second number', '');
secondNum = parseFloat(secondNum);
document.write'The sum of ' + first + ' and ' + secondNum + ' is
' + (firstNum + secondNum) + '<BR>');
document.write('The difference of ' firstNum + ' and ' +
secondNum + ' is ' + (firstNum + secondNum) + <BR>);
</SCRIPT>
Figure 6 A JavaScript program containing errors
Q4
(a)Identify as many errors as you can, then rewrite the program
correcting the errors, and paste the corrected version into the
document containing your solution. Your tutor will find it helpful
if you highlight where you have made the changes. [6]
(b)For each different error you found write a very brief explanation
of what was wrong in the original code and what change you
needed to make. [6]
(ii) State whether each of the following evaluates to true or false in
JavaScript.
(a)7 >=11 – 2 * 2;
(b)'pluto' > 'jupiter';
(c) '66' == 66;
(iii)An imaginary internet clothing store operates the following delivery
charges.
•
For orders worth less than £25 the delivery charge is £3.00.
•
Orders worth at least £25 but less than £100 are delivered free.
•
Orders worth £100 or more are delivered free and in addition the
customer receives a free gift.
We have started writing a JavaScript program that prompts a user to
enter the value of their order, then tells them which of the categories
above will apply.
// A delivery charge program
var orderValue; // How much the order is worth
orderValue = window.prompt('How much is the order worth in
Pounds Sterling?','');
document.write('The value of your order is £' + orderValue
+ '.<BR>');
// Insert your code here
document.write('<BR>');
document.write('Thank you for shopping with us.');
(a)Using program_7.3.9 as a guide, complete the code above so that
it will work out which band the order falls in,then display a
suitable message on the screen using the document.write()
method. Please note the following points.
1 You will need to add appropriate HTML tags before and after
the code, as well as completing the JavaScript where
indicated. program_7.3.9will show you what HTML tags to
use. You should give the window the title 'Delivery charges'.
2 There is no need for your program to check that the user's
input is actually a valid number; you may assume that this will
always be the case.
3 You should use indentation to help make the structure of your
program clear, in the same way program_7.3.9does.
4 A copy of the incomplete program as shown above in Figure7
can be found in the file Q4(iii).html that you downloaded
with this assignment.
Insert a copy of your completed program into the document
containing your solution. [8]
(b)Run your program with each of the following inputs. (Note that
you only need to enter the figures, without any £ sign.)
• 24
• 25
• 99
• 100
For each run insert a screen shot of the browser window into the
document containing your solution.
Please, please, please, I need to submit it on May 15th.
-
May 13th, 2007, 08:17 AM
#2
Re: Help!
two points:
- Is this javascript or java? This is strictly a java forum.
- Where's your code? You must do the work and you alone. Others can give help w/ specific questions regarding your code, but we must see the code. Without code, you imply that you wish someone else to do your work which is insulting to us and harmful to you. You were told this before on your previous post.
Good luck
/Pete
Last edited by petes1234; May 13th, 2007 at 08:21 AM.
-
May 13th, 2007, 08:35 AM
#3
Re: Help!
Pete, mate, I'm not asking for you do to my homework, I'm asking for tips on how to go about doing them, because I'm just starting with Java.
I needed help in relation to how to explain the errors. Sorry if I came over the wrong way.
-
May 13th, 2007, 08:44 AM
#4
Re: Help!
Then I would suggest you read both:
http://www.codeguru.com/forum/showthread.php?t=366302
And the "Before You Post..." entry that is at the top of EVERY forum.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 13th, 2007, 10:43 AM
#5
Re: Help!
 Originally Posted by viper12
Pete, mate, I'm not asking for you do to my homework, I'm asking for tips on how to go about doing them, because I'm just starting with Java.
I needed help in relation to how to explain the errors. Sorry if I came over the wrong way.
I see no information in your post about your attempts to solve the problem. All I see is a copy of your assignment and a general request for help together with a deadline.
If you do some of the work and show it, then I will look upon this differently. Until then, your message and intent speak for themseves loud and clear.
Last edited by petes1234; May 13th, 2007 at 10:50 AM.
-
May 13th, 2007, 11:15 AM
#6
Re: Help!
<SCRIPT LANGUAGE="JavaScript">
// A program to find the sum and difference of two numbers
var firstNum;
var secondNum;
firstNum = window.prompt('Please enter the first number', '');
firstNum = ParseFloat(firstNum);
secondNum = window.prompt('Please enter the second number', '');
secondNum = parseFloat(secondNum);
document.write('The sum of ' + first + ' and ' + secondNum + 'is' + (firstNum + secondNum) + '<BR>');
document.write('The difference of ' firstNum + ' and ' +
secondNum + ' is ' + (firstNum + secondNum) + <BR>);
</script>
I don't know where to find a software that will execute that program for me. Do you know of any?
-
May 13th, 2007, 11:34 AM
#7
Re: Help!
As mentioned above, this is not java but javascript, a separate language that is only slightly related to java (just as c++ is slightly related to java). I don't know much about javascript except that it is interpreted and I believe runs on a browser such as Internet Explorer, or perhaps better, Mozilla Firefox.
When in doubt, google it out. A couple of sites for you:
Core JavaScript Guide
About JavaScript
JavaScript Tutorial
-
May 13th, 2007, 11:39 AM
#8
Re: Help!
And still not within code tags [which were covered in both of the references in Reply #4]
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 13th, 2007, 11:42 AM
#9
Re: Help!
Looking at the tutorials, I see that Internet Explorer will run javascript w/out modification, so you have a way of testing your programs right in your computer. You should create a text file w/ notepad and give it an extension .html. Then in the file place the following text:
Code:
<html>
<body>
<SCRIPT LANGUAGE="JavaScript">
// A program to find the sum and difference of two numbers
var firstNum;
var secondNum;
firstNum = window.prompt('Please enter the first number', '');
firstNum = ParseFloat(firstNum);
secondNum = window.prompt('Please enter the second number', '');
secondNum = parseFloat(secondNum);
document.write('The sum of ' + first + ' and ' + secondNum + 'is' + (firstNum + secondNum) + '<BR>');
document.write('The difference of ' firstNum + ' and ' +
secondNum + ' is ' + (firstNum + secondNum) + <BR>);
</script>
</body>
</html>
Then double click on the file and follow your browser instructions. The prog won't work til you fix it though, so you'll have to play with it. Also, don't forget to allow for blocked content when running this.
Last edited by petes1234; May 13th, 2007 at 11:53 AM.
-
May 13th, 2007, 11:55 AM
#10
Re: Help!
ps: just look at the tutorials. I don't know javascript, but it didn't take long to fix it, and shouldn't take you long either.
-
May 13th, 2007, 12:28 PM
#11
Re: Help!
Thanks a lot, Pete. Sorry for interpreting my intentions in the wrong way, just that I'm a beginner, and a soddy one to say the least.
--------
// A program to find the sum and difference of two numbers
var firstNum;
var secondNum;
firstNum = window.prompt('Please enter the first number', '');
firstNum = parseFloat(firstNum);
secondNum = window.prompt('Please enter the second number', '');
secondNum = parseFloat(secondNum);
write('The sum of ' + (firstnum) + ' and ' + (secondNum) + 'is' + (firstNum + secondNum) + '<BR>');
write('The difference of ' + (firstNum) + ' and ' + (secondNum) + ' is ' + (firstNum - secondNum) + '<BR>');
--------
Oh, and I'm a Mac.
-
May 13th, 2007, 12:51 PM
#12
Re: Help!
 Originally Posted by viper12
Thanks a lot, Pete.
welcome
 Originally Posted by viper12
Oh, and I'm a Mac.
No prob. Your browser should run javascript as well. If not, download mozilla's firefox.
and congrats on solving your code.
ps: please learn to use code tags. It's as simple as pressing a button in the forum editor.
Code:
I type in code here, then highlight this text, then press the Code button at the top.
-
May 13th, 2007, 01:15 PM
#13
Re: Help!
Will do. I'm more into html than Java, but the work I do needs me to crash course through javascript unforunately, which is a bugger in itself.
Thanks for the help, again, I will see you around here.
-
May 13th, 2007, 01:33 PM
#14
Re: Help!
 Originally Posted by viper12
I'm more into html than Java, but the work I do needs me to crash course through javascript unforunately, which is a bugger in itself.
Well, as has already been explained, JavaScript is not at all the same thing as Java - they are different languages, with only the word 'java' in common.
This is a Java forum - you might find a JavaScript forum more useful if you want help with JavaScript...
I can live with doubt and uncertainty. I think it's much more intersting to live not knowing than to have answers which might be wrong... In physics the truth is rarely perfectly clear, and that is certainly universally the case in human affairs. Hence, what is not surrounded by uncertaintly cannot be the truth...
R. Feynman
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|