Anda di halaman 1dari 7

A Step-By-Step Approach To Java Programming

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).

//here are two ways to output strings


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!");
}
}

- After typing the code click on the Compile File button.

- 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**

Steve Floyd, Jeff Giovanatti, Mike Walsh - 2003


A Step-By-Step Approach To Java Programming
Now we’re going to write a program that deals with integers.

- Without closing any screens, Go to File and New and call the new program temp2.

- Input the following code.

//here is an example of adding integers


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);
}
}

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.

- In My Computer go to your directory N:\JAVA-Feb10\Project1\classes. There should be two files,


temp1.class and temp2.class.

- 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.

Steve Floyd, Jeff Giovanatti, Mike Walsh - 2003


A Step-By-Step Approach To Java Programming
Our third program will read information from the user. This is not as straight forward as Turing. To make
things easier we will be using a class called SavitchIn. Essentially SavitchIn is a set of input procedures that
have already been created by someone else (Mr.Savtich).

- 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.

//here is a program that uses SavageIn to get input

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);
}
}

- Change int num1; line to String name;


Change “Enter a number: “ to “Enter your name: “
Change SavitchIn.ReadLineInt(); to SavitchIn.ReadLine();
Change all occurrences of num1 to name

- Execute the new program.

**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:

-prompt the user for their name


-prompt the user for their odometer reading before leaving the house this morning
-prompt the user for their odometer reading upon arrival at Althouse.
-prompt the user for the amount of liters used to drive here this morning (we’ll assume they somehow
know this)
-output to the user a friendly message telling them how many km’s their car gets per liter.
-assuming gas is 79.9 cents/liter, output to the user how much it cost them to attend this Java
workshop.

(C’mon, don’t be lazy, this program can be written in only 11 lines…maybe less)

Steve Floyd, Jeff Giovanatti, Mike Walsh - 2003


A Step-By-Step Approach To Java Programming
//here are two ways to output strings

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!");
}
}

//here is an example of adding integers

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);
}
}

//here is a program that uses SavageIn to get input

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);
}
}

Steve Floyd, Jeff Giovanatti, Mike Walsh - 2003


A Step-By-Step Approach To Java Programming

//here is a program that uses SavageIn to get input

class temp3 {
public static void main (String[] args){

String name;
int odobefore, odoafter, liters, mileage;
double cost;

System.out.println("Enter your name: ");


name= SavitchIn.readLine();
System.out.println("The odo reading before leaving:
");
odobefore = SavitchIn.readLineInt();
System.out.println("The odometer reading upon
arrival: ");
odoafter = SavitchIn.readLineInt();
System.out.println("Enter the amount of gas used (L):
");
liters = SavitchIn.readLineInt();

mileage = (odoafter - odobefore) / liters;


System.out.println (name + ",you averaged " + mileage + "
km's/L");

cost = liters * 79.9 / 100;


System.out.println (name + ",you paid $" + cost + " to get
here!");

}
}

Steve Floyd, Jeff Giovanatti, Mike Walsh - 2003


A Step-By-Step Approach To Java Programming

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.

public class temp9


{
public static int addnums (int x, int z)
{
int y = x + z;
return (y);
}

//create a method that multiplies the numbers

public static void outtoscreen (int add, int mult)


{
System.out.println ("Add the numbers you get: " + add);
System.out.println ("Sub the numbers you get: " + sub);
}

public static void main (String [] args)


{
int firstnum = 5;
int secondnum = 10;
int add, ______;

add = addnums (firstnum, secondnum);


//call a method that multiplies the numbers
outtoscreen (add, ______);
}
}

Steve Floyd, Jeff Giovanatti, Mike Walsh - 2003


A Step-By-Step Approach To Java Programming

public class temp1


{
// ***PROCEDURE ADDNUMS***
public static int addnums (int x, int z)
{
int y = x + z;
return (y);
}

// ***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);
}

// ***MAIN PROGRAM STARTS HERE***


public static void main (String [] args)
{
int firstnum = 5;
int secondnum = 10;
int add, sub;

add = addnums (firstnum, secondnum);


sub = subnums (firstnum, secondnum);
toscreen (add, sub);
}
}

Steve Floyd, Jeff Giovanatti, Mike Walsh - 2003

Anda mungkin juga menyukai