-
Login to a Windows NT machine and navigate (via MyComputer or Windows Explorer)
to your folder on the H:\ drive. Create a folder called cosc135
(or programming1 etc.) and in that new folder, create a subfolder called
lab01.
-
Open another navigation window and proceed to the folder G:\MathCS
(the dropfolders drive). Arrange your desktop so that both the BreezyGUI
subfolder on the G-drive and your newly created cosc135 folder on
the H-drive are visible.
-
Drag the BreezyGUI folder onto your cosc135 folder. You should now
have a copy of the classes in the BreezyGUI package.
We will be using CodeWarrior from Metrowerks for our programming environment.
Click here for a brief explanation
of how to start CodeWarrior and setup a new project. Go ahead and setup
a project called HelloWorld in your lab01 folder on the H-drive.
The class name should be HelloWorld. Since we'll be using BreezyGUI to
construct our windowing interfaces for our programs, our java source files
will look something like this:
/* ProgramName.java
Author:
Date Started:
Description:
*/
import java.awt.*;
import BreezyGUI.*;
public class ProgramName extends GBFrame {
// GUI design and declaration goes here
// data and variable declarations
public void buttonClicked (Button pressedButton){
// handle the click of all buttons
here
} // end of buttonClicked method
public static void main ( String[] args){
// create a new object, set its
size and turn it on
Frame frm = new ProgramName();
frm.setSize (widthInPixels,
heightInPixels);
frm.setVisible (true);
} // end of main method
} // end of class ProgramName
You'll have to add details to the topmost comments, change the three
occurences of ProgramName to match the name of the file (remember, the
class name must match exactly the filename without the .java extension),
and select a width and height for the setSize method. In a Java
source file, any text between /* and */ is considered a comment (even if
it extends over several lines). As well, any text after // on a single
line is also a comment. Look at the code above for examples of both types
of comments.
-
In CodeWarrior, copy the code above into the HelloWorld.java file. You
can double click on the HelloWorld.java file in the project window to open
an editor window. Be sure you change ProgramName to HelloWorld.
-
In the GUI design section, add the line
Button messageButton
= addButton ("Click to Print",1,1,1,1);
This line adds a button named "messageButton" in the first row, first
column of our GUI grid. The button is 1 column wide and 1 row tall. In
general, BreezyGUI items take the parameters (initial value, row, column,
width, height).
-
To make our button do something when we click it, add the line
messageBox ("Hello
there great big world!");
to the buttonClicked method, between the curly brackets { and
}. The messageBox command will open a box that contains the String you
pass in. Anything in double quotes is considered a String in Java.
-
Select Run from the Project menu in CodeWarrior (near the
bottom), and observe any errors you receive. Try to fix your errors. If
you need help, ask. When you get your application to run, be sure to close
the messageBox by clicking on the OK button, and then the DOS window by
pressing [ENTER]. Be sure to save your project, once you get it to run.
-
Sometimes we will have several strings that we need to print together.
Java allows us to concatenate strings using the + operation. Briefly, "string1"
+ "string2" is "string1string2". You can also treat literal numbers, like
4 or 9, as strings, thus (in Java), "I am " + 39 + " years old."
gives "I am 39 years old." (notice the spaces), whereas "I
am" + 39 + "years old." gives "I am39years old.". In the
data-variable section of your HelloWorld.java file, add a string data field
called myName. For me, the line would be
String myName = "Tom";
Modify your code so that the messageBox displays the message Hello
world. My name is ????? (where ???? is your name, and you use +
myname inside the messageBox method call. Once your code runs, print
out the messageBox command you used here:
-
Add another variable called age to the class file. The line should be something
like (if you are 39 years old):
int age = 39;
Modify the messageBox command so that the printout reads "My name
is ??? and I am ??? years old" (again, the ??? are filled in by the variables
name and age). Record your messageBox command below (once you get the code
to run).
-
Edit your last messageBox command by adding \n (backslash n with
no spaces) just before the and I am part of your messageBox command.
Describe what the \n did to the printed material.
-
Hopefully your messageBox command contains something like + age +
in its input. Change age to age*2 and explain what happened
to the output.
-
Now replace the age*2 with age / 0. Describe what happened.
-
Fix your HelloWorld.java file by replacing the age / 0 with just
age again. Run the code to make sure it compiles and runs correctly.
Now delete the semicolon at the end of the messageBox command. Try to run
the program and explain what happens.
-
Put the semicolon back (be sure your code runs) and delete the word public
from the main method declaration line. Try to run the code and explain
what happens.
-
Put the word public back in your code and make sure it compiles
and runs. Once it does, save your project one last time. In your cosc135
folder, create a subfolder whose name is your name (my subfolder would
be called TomLinton). Copy your lab01 folder (with your HelloWorld.* files)
into the folder with your name. You should be able to right-click and drag
the folder, then select Copy (not Move). Open another navigation
window and navigate to G:\Lintont\cosc135. That folder should have a Handin
subfolder. Drag the folder with your name on it onto the Handin folder
icon. In the future, you can hand stuff in by placing it in your folder,
inside my cosc135\Handin folder. If you finish, turn in this paper before
you leave.
-
Look at the FahrenheitToCentigrade.java
file. You will soon be asked to code a PiggyBank.java file. This program
will get an integer number of pennies, nickels, dimes and quarters from
the user and report the number of dollars and leftover change equivalent
to these coins. For example, 6 pennies, 8 nickels, 12 dimes, and 6 quarters
would be 3 dollars and 16 cents. Your GUI will require four integer input
fields and a compute button. Draw a picture of your GUI and try to create
the code (based on the default.java template
above) that constructs your GUI. For now, you can leave the buttonClicked
method empty (just as it is in default.java). All you're aiming for is
the GUI design.