This activity is meant to introduce you to the JAVA environment. The idea is to become familiar with the
basic buttons and commands. If you don’t understand the code, don’t worry, this is an introductory handout.
- On your N drive create a new folder where you will keep your java programs. Call it JAVA-Feb10
- Open the JCreator Folder which is found in the OESS English folder on you desktop. Start Jcreator
(not the Java Web Start).
- If a ‘Failed loading Workspace’ or 'Tip Of The Day" box appears ignore or cancel it.
- Under File, select New... Three Tabs will appear (Files, Projects, Workspaces).
- Select the Projects tabs and highlight Basic Java Application. Type in the Project name as Project1
and in the Location box browse through your directory to the JAVA-Feb10 folder you created. Click Ok.
- Under File, select New... Select the Files Tab and highlight Java File.
- Before clicking OK you will have to make a name for your new java file. Call it temp1 and under
Location it should say N:\JAVA-Feb10\Project1\. Click OK.
You should now have an empty window with a flashing cursor and a number 1 next to it. This is where we
will write our code. In this area type out the following code, exactly as it is written. This will be your first
program. (We’re going to skip the “Hello World” example).
- Wait a few seconds and then Process Completed. should appear in the bottom window.
If there are syntax errors then Java will indicate where these occurred. Go back and make sure you typed in
the code exactly as it is above.
- Now click on the Execute File button. Your first program should now run. Congratulations.
- Alter the pre-defined string so that it contains “Wow, Java is so incredibly easy!”
- Change the basic outputted string from “This is another way!” to “I can program anything!”
Recompile and then Execute your new program.
**You now know the basic structure of a Java program, how to define string variables, and how to output strings.**
**You also know how to put comments into a program by using the // symbols**
- Without closing any screens, Go to File and New and call the new program temp2.
num1 = 6;
num2 = 4;
System.out.println(“Let’s add “ + num1 + “ and “ + num2);
num3 = num1 + num2;
System.out.println(num3);
}
}
Change the code so that the program subtracts the two numbers. You should change your output stmt too.
- Now compile the temp2 example and execute it to make sure it runs.
- Delete temp2.class. Go back to JCreator and hit the Execute File button. What happened?
By hitting the Compile File button you can remake the temp2.class file.
**You now know how to define integer variables, how to assign them values, and how to manipulate them**
**You know the importance of class files, and how they’re created by compiling the java file**
**You have also seen a little bit about how programs are stored in directories or where they live**
Just to test things out, what happens if you add an extra } at the end of your code?
Notice that small syntax errors can cause major problems.
What happens if you change the name of the class from temp2 to temporary2?
So the class name needs to be the same as the actual file name.
- Copy the SavitchIn.java file from W:\2002 - 2003 Pre-Service\Kitto-E38\--JAVA-- into your
N:\JAVA-Feb10\Project1 directory.
Currently this is only a java file, so it can’t be used by other programs, nor can it be executed. (It is like the
temp2.java file, it is just code, it’s useless without SavitchIn.class).
- To create a SavitchIn.class file go to JCreator and open up the SavitchIn.java file. Hit the Compile File
button creating SavitchIn.class in the N:\JAVA-Feb10\Project1\classes directory.
- Now close the SavitchIn.java file in JCreator. Since SavitchIn.class now exists we can make calls to it
from other class files that are in the same class directory.
- Again, go to File and New… and call the third program temp3.
class temp3 {
public static void main (String[] args){
int num1;
System.out.println("Enter a number: ");
num1= SavitchIn.readLineInt();
System.out.println ("Here's the number: " + num1);
}
}
**You now know one way to read integers and strings from the screen**
Time to take the training wheels off …. Your task is to write a program called temp4 that will:
(C’mon, don’t be lazy, this program can be written in only 11 lines…maybe less)
class temp1 {
public static void main(String[] args){
String string1;
string1 = "This is one way to output to the screen.";
System.out.println(string1);
System.out.println("This is another way!");
}
}
class temp2 {
public static void main(String[] args){
int num1, num2, num3;
num1 = 6;
num2 = 4;
System.out.println(“Let’s add “ + num1 + “ and “ +
num2);
num3 = num1 + num2;
System.out.println(num3);
}
}
class temp3 {
public static void main (String[] args){
int num1;
System.out.println("Enter a number: ");
num1= SavitchIn.readLineInt();
System.out.println ("Here's the number: " + num1);
}
}
class temp3 {
public static void main (String[] args){
String name;
int odobefore, odoafter, liters, mileage;
double cost;
}
}
Green text indicates alterations you must make to the program in order for it to
run. You will have to create a method and then create an appropriate call to that
method.
// ***PROCEDURE SUBNUMS***
public static int subnums (int x, int z)
{
int y = x - z;
return (y);
}
// ***PROCEDURE TOSCREEN***
public static void toscreen (int add, int sub)
{
System.out.println ("Add the numbers you get: " + add);
System.out.println ("Sub the numbers you get: " + sub);
}