您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

Untitled-1.GIF (357576 bytes)


Apply Index

Apply Page # 3

Activity 3 – Addition applet

(1) Compile and run the applet Add1.java .

This program works out 2+2. Make sure that you see the result on the screen.

(b) In this program the result of the addition was assigned to a variable called `result'. While this is a sensible name for something that store the result of a calculation, the choice of variable names is entirely at the discretion of the programmer. Modify the program Add1.java, so that the variable is called `fred'. Note that you will have to change more than one line.

(c) In (b) I suggested changing the variable name from `result' to `fred'. While this works, why is this a silly thing to do?

(d) Enter, and try to run the program ` Add1_bad.java '. It won't work. What is the error message, and what does it mean?

(e) Using `Add1.java' as an example, write a Java applet that works out 1234 x 4321. In Java, as in most programming languages, we can't use the letter `x' to mean multiplication (because it might be the name of a variable). So we use an asterisk `*' instead.

(f) Using `Add1.java' as an example, write a Java applet that works out 2 / 5 (two divided by 5).
And the answer is NOT zero. If you get zero, try to figure out why and put it right.

Discussion/Suggested solution to Activity 3

Back to Content

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg