import javabook.*;
/*
This is an introductory first
program.
Hopefully it works just fine!
*/
class HelloWorld
{
public static void main(String[]
args)
{
System.out.println("Hello
world!");
System.out.println("This
is my first Java program!");
}
}
8. Click the "Compile" button, and wait for it to display an error message
at the bottom or the text "Class compiled - no syntax errors." If there are
any errors, fix them and recompile.
9. When your class has compiled correctly, find the project window and right-click
on the HelloWorld class rectangle.Chose "void main(args)", which sends a
message to the class to run the method called main. When the "BlueJ: Method
Call" window pops up, just click "OK" to send no arguments to the method.
10. If all went successfully, the BlueJ Terminal Window should pop up and
you should see the text "Hello world!" inside it. Congratulations!
11. What do "/*" and "*/" do?
12. Change the first appearance of the word "println" to "print". What happens?
13. Close the "Terminal Window" and the "Hello World" Java code window. Exit
BlueJ by clicking on the "Project" menu, then clicking "Quit."
import javabook.*;
/*
This program will allow you to draw a picture
by dragging a mouse while
holding the left button. To erase the picture,
click the right mouse button.
*/
class FunTime
{
public static void main(String[] args)
{
SketchPad doodleBoard;
// Declare SketchPad object named doodleBoard
doodleBoard = new SketchPad();
// Create the SketchPad object
doodleBoard.setVisible(true);
// Make doodleBoard visible
doodleBoard.toFront();
// Make doodleBoard appear in front
}
}
7. Compile your program and fix errors until it compiles successfully.
8. Right-click the "FunTime" class in the project window and choose "void
main(args)" to run the program.
9. If all goes successfully, you should see a SketchPad window in front of
you. Have fun with it! When done, close the window to end the program.
10. What does "//" do? How is it different from "/*" and "*/"?
11. Close the Java source window, if still open, and leave just the Project
window remaining.
// Declare, create, and
display a MessageBox
MessageBox messageBox;
messageBox = new MessageBox(mainWindow);
messageBox.show("I Love Java!!!");
}
}
2. Compile and run the program. Enter integers when the program asks you to. What does the operation "%" do? How about "/"? (Try running the program several times with different input.)
3. Now change the type of a and b from "int" to "double", and change the
two "getInteger" method calls to "getDouble". Recompile. How
does the behavior of "%" change? Get rid of the "%" line and recompile.
Question 1: How does the behavior of "/" change?
Question 2: The type "double" is badly named--what does it mean in
this context?
4. Suppose you want to "comment out" a block of code that's bugging you.
That is, you want to make the compiler ignore some code without actually removing
the code from your file. The /*...*/ style comments give you a convenient
tool for doing this. Try commenting out everything in main.. Recompile.
Question 3: What happens, and why? How can you fix the problem?
Question 4: Which style of comments is appropriate for documentation
in your code, and which style of comments is appropriate for commenting out
blocks of code at a time?
5. Put the answers to Questions 1-4 in documentation at the top of the program. Add the names of all students in your group.
6. Submit this program electronically. To find out how to do so, go to the class home page at http://www.mathcs.carleton.edu/faculty/dmusican/cs117w02 and click on the link "Electronically submitting your work."
fahrenheit = 1.8 x celsius + 32
Make sure to add the names of all students in your group to documentation
at the top of the program, and electronically submit your work.