Name: ________________
ID:_______________
Required files:
Step 1.
Make a copy of the two required programs,
compile them, and execute HelloWorld
. Record the results.
Your instructor will provide you with information on copying files,
compiling Java programs, and executing compiled Java programs.
Step 2.
Rename HelloWorld.java
as
helloworld.java
(i.e., change the capitalization)
and try to recompile it. Record any error messages you observe.
What do those error messages suggest about Java?
When you have entered your response, rename helloworld.java
to HelloWorld.java
.
The following steps are primarily applicable to command-line development environments, like Sun's JDK.
Step 3.
Remove SimpleOutput.java
(source
code) but keep SimpleOutput.class
(compiled code) and the
two versions of HelloWorld
.
(Your directory or folder should contain only HelloWorld.java
,
HelloWorld.class
, and SimpleOutput.class
.)
Execute HelloWorld
.
Record the results. If the program doesn't work, suggest why not. If
the program continues to work, suggest why.
Step 4.
Make another copy of SimpleOutput.java
(source
code) and delete SimpleOutput.class
(compiled code).
(Your directory or folder should contain only HelloWorld.java
,
HelloWorld.class
, and SimpleOutput.java
.)
Execute HelloWorld
. Record the results. If the program
doesn't work, suggest why not. If the program continues to work,
suggest why.
Step 5.
Compile SimpleOutput.java
.
Delete the line from HelloWorld.java
that reads
import SimpleOutput;
Try to recompile and execute the program. If the program successfully recompiles, suggest why. If it does not, suggest why not.
After entering an answer, you may wish to read the notes on this problem.
Step 6.
Remove SimpleOutput.java
(source code),
SimpleOutput.class
(object code), and
HelloWorld.class
(object code). You should now have only
HelloWorld.java
in your directory or folder. Attempt to
recompile and execute HelloWorld
. If the program
successfully recompiles, suggest why. If it does not, suggest why not.
Step 7.
Make a copy of SimpleOutput.java
. Delete
HelloWorld.class
if it exists. You should now have
SimpleOutput.java
and HelloWorld.java
in
your folder or directory. Try to recompile and execute
HelloWorld
. Does it compile successfully? Did you
expect it to? Did any new files appear in your directory? What does
this suggest about Java?
Required files:
Before you begin,
if you have not done so already, make copies
of SimpleOutput.java
and HelloWorld.java
and compile them.
Step 1. Insert the following lines to
HelloWorld.java
right before the end of the main
method.
out.print("Welcome to Java."); out.println("We hope you enjoy yourself.");
Execute HelloWorld
. Record the output. Is this what you
expected? If not, suggest why not.
Step 2. Recompile HelloWorld.java
and
then execute it. Record the output. Did you get what you expected?
If not, suggest why not.
Step 3. What do the results of the first two steps suggest?
Step 4. What is the difference between
print
and println
?
Step 5. Replace the line in HelloWorld.java
that reads
out.println("Hello World");
with one that reads
println("Hello World");
Recompile and execute the program. What happens? What does this suggest about Java?
If you introduced any errors in this step, repair them.
Step 6. Replace the line in HelloWorld.java
that reads
SimpleOutput out = new SimpleOutput();
with one that reads
SimpleOutput out;
Recompile and execute the program. What happens? What does this suggest about Java? If you introduced any errors, repair them.
Required files:
In case you've forgotten, here is the code for Greetings.java
.
import SimpleInput; import SimpleOutput; /** * A simple Java program that illustrates textual input and output. * * @author Samuel A. Rebelsky * @version 1.1 of March 1998 */ public class Greetings { /** * Prompt for the user's name and print a greeting. */ public static void main(String[] args) { // The object we're using to read input. SimpleInput in = new SimpleInput(); // The object we're using to print output. SimpleOutput out = new SimpleOutput(); // The user's name. String name; // Prompt for the name. out.print("What is your name? "); // Get the name. name = in.readString(); // Print a friendly message. out.print("Hello "); out.print(name); out.println(", welcome to the joys of Java."); } // main(String[]) } // Welcome
Step 1. If you have not already done so, make a copy of
the required programs, compile them, and execute
Greetings
. Record the results.
Step 2. At times, programmers forget to enter the quote that ends a string. Replace the line that reads
out.print("Hello ");
with one that reads
out.print("Hello );
(i.e., remove the ending quotation mark). What difference do you expect this change to make?
Step 3. Attempt to recompile and execute Greetings
.
What happened? Why?
After entering your answers, correct the error.
Step 3. Another common mistake is to leave out semicolons. Replace the line that reads
String name;
with one that reads
String name
(i.e., drop the semicolon). What difference do you expect this change to make?
Step 5. Attempt to recompile and execute Greetings
.
What happened? Why?
After entering your answers, correct the error.
Step 6. Replace the line that reads
out.print("What is your name? ");
with one that reads
out.print("What is your name? ")
(that is, drop the semicolon).
What difference do you expect this change to make?
After recording your answer, attempt to recompile and execute
Greetings
. Explain what happened.
After entering your answers, correct the error.
Step 7. What do your answers to steps 5 and 6 suggest?
Step 8.
Another common mistake is to mistype a variable name. For example,
we might decide to use output
instead of out
.
Change only the line that reads
SimpleOutput out = new SimpleOutput();
to read
SimpleOutput output = new SimpleOutput();
What difference do you expect this change to make?
After recording your answer, attempt to recompile and execute
Greetings
. Explain what happened.
After entering your answers, correct the error.
Step 9.
Yet another common mistake is to forget to end multi-line comments.
Remove the */
that appears just before
public class Greetings {
What difference do you expect this change to make?
After recording your answer, attempt to recompile and execute
Greetings
. Explain what happened.
After entering your answers, correct the error.
Step 10.
Remove the ``*/
'' that appears just before
public static void main(String[] args) {
What difference do you expect this change to make?
After recording your answer, attempt to recompile and execute
Greetings
. Explain what happened.
After entering your answers, correct the error.
Step 11. It is likely that your answers for steps 9 and 10 were different. Can you explain why?
Required files:
Step 1. If you have not already done so, make a copy of
the required programs, compile them, and execute
Greetings
. Record the results.
Step 2. Extend the program to read in some other fact about the user (e.g., a major or home town) and to print out a response to that fact. Before making the changes, write down the new lines of code. After extending the program, you should recompile and execute the program.
If you make mistakes, the Java compiler will print some error messages. Write down any error messages you receive and a possible explanation of those errors.
Step 3. Remove the semicolon after
import SimpleInput;
Try to recompile the program. Record the error messages. Reinsert the semicolon to repair the error you just created.
Step 4. Remove other semicolons from the program and try to recompile. What semicolons did you remove and what error messages did you get? Record the error messages and corresponding semicolons. Repair those errors.
Required files:
Before you begin,
if you haven't done so already, obtain copies of
SimpleInput.java
and SimpleOutput.java
and compile them.
Step 1.
Obtain a copy of
BasicComputations.java
,
compile it, and execute it. Try a variety of inputs (small numbers,
large numbers, positive numbers, negative numbers, things that aren't
numbers) and record the results for each input. What do the results
suggest about Java's treatment of numbers?
Step 2.
After the line that prints the square in
BasicComputations.java
, add lines that read
out.print(val); out.print(" + 1 is "); out.println(val+1);
What do you think this will do? Compile and execute the program to test your hypothesis. Record any discrepancies and suggest why such discrepancies might occur.
Step 3.
Change all the instances of double
to int
. Change readDouble
to readInt
. Recompile the program. Execute the program
and enter the value 2
. Record the output. Execute the
program and enter the value 2.4
. Record the output. What
does this suggest?
Optional applet experiment
Required files:
Step 1.
Make a copy of the two files. Compile HelloWorldApplet.java
.
Load the applet, using whatever technique your instructor specifies.
(You might load it in a browser like Netscape Navigator or Microsoft
Internet Explorer; you might run an application like Sun's
appletviewer.) Describe the output.
Step 2.
In helloworld.html
, change the width of the applet from
400 to 20. What do you expect will happen when you reload the applet?
Step 3.
Reload the applet, using the modified helloworld.html
.
What happened? Explain why.
After recording your answer, restore the width to 400.
Step 4.
Explain how to update this program to print "Hi there!"
rather than "Hello world"
.
Step 5.
Make the change you suggested in the previous step, recompile
HelloWorldApplet
, and run it. If you did not
get the expected results, update your answer to step 4, and
try again.
Step 6. Change the line that reads
paintBrush.drawString("Hello world", 10, 20);
to one that reads
paintBrush.drawString("Hello world", 100, 20);
What effect do you expect this change to have?
Step 7.
Recompile HelloWorldApplet
and run it. What results
did you observe? What do these results tell you about the second
parameter to drawString
?
When you are done, change that parameter from 100 back to 10.
Step 8. Change the line that reads
paintBrush.drawString("Hello world", 10, 20);
to one that reads
paintBrush.drawString("Hello world", 10, 100);
What effect do you expect this change to have?
Step 9.
Recompile HelloWorldApplet
and run it. What results
did you observe? What do these results tell you about the third
parameter to drawString
?
When you are done, change that parameter from 100 back to 20.
Step 10.
What code might you write to print "Hello"
at the
top left of the screen and "Goodbye"
at the bottom
right?
Step 11.
Try including that code in HelloWorldApplet
. Recompile
and run it. If necessary, keep experimenting until you get it right.
Optional applet experiment
Required files:
Before you begin,
if you have not already done so, make a copy of the required files,
compile HelloWorldApplet
, run it, and record the
results.
Step 1. Remove the line that reads
extends Applet
What effect do you expect this change to have?
Step 2.
Attempt to recompile and execute HelloWorldApplet
. What
happens? Explain why.
After you are done, restore the line that reads
extends Applet
Step 3. Remove the line that reads
import java.awt.Graphics;
What effect do you expect this change to have?
Step 4.
Attempt to recompile and execute HelloWorldApplet
. What
happens? Explain why.
After you are done, restore the line that reads
import java.awt.Graphics;
Step 5. Change the line that reads
public void paint(Graphics paintBrush) {
to one that reads
public static void paint(Graphics paintBrush) {
What effect do you expect this change to have?
Step 6.
Attempt to recompile and execute HelloWorldApplet
. What
happens? Explain why.
After you are done, restore the line that reads
public void paint(Graphics paintBrush) {
Step 7.
Make a copy of HelloWorldApplet.java
called
Example.java
. Replace the body of paint
with
paintBrush.drawString("An example applet");
What do you expect to happen when you try
to compile Example.java
? Try to compile it and explain
the error messages.
Step 8.
Replace all the instances of HelloWorldApplet
in
Example.java
with Example
. What do
you expect to happen when you try to compile it? Verify your
answer by trying to compile it.
Step 9.
Hopefully, Example
compiled successfully. Next, make
a copy of helloworld.html
called example.html
.
Run your new applet using example.html
. What happens?
Why?
Step 10.
If you have not already realized that you need to do so, update
example.html
so that it uses Example.class
instead of HelloWorld.class
.
[Front Door] [Introduction] [Code]
Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.
Source text last modified Tue Oct 26 12:50:56 1999.
This page generated on Tue Oct 26 15:37:32 1999 by Siteweaver.
Contact our webmaster at rebelsky@math.grin.edu