Beginning Programming -- Java

HTML is one of the very simplest programming languages. You can do lots of things with it, but nearly all of them are related to formatting things (text, pictures, etc.) on a page. There are many other kinds of computer languages. Some, like HTML, are used for very specific purposes. For instance, there are languages designed especially for working with math calculations or for displayin graphics. Others are general-purpose and are designed to let programmers do many different kinds of things.

The Computer Science department at CSU has chosen to use Java as the first programming language we teach new students. Java is an extremely powerful general-purpose language. However, it can be hard to learn at first because it has lots of special functions and requires some special set-up. The class helpers are available to try to help you past the start-up parts as easily as possible.

We have put together some very basic Java programs for you to try. We invite you to try each of these programs. First try the program as written, then modify it so it does something similar to but different from what it originally did.

When you have played with some of the programs, think about something you would like to try to program. Work with one of the helpers to figure out whether you could do all or part of this in our time. Decide what you want to do, and how to get started. Ask for help as you need it!

But first ... if you have not already done so, review The basic steps for writing a program and try at least one of the following programs (try all of them if you are new to Java).

The first program we suggest people write for any computer language simply tells the computer to print out a line of text, such as "Hello World."

Following our suggested model, here is:
* The problem statement:
      Print out a line of text saying "Hello World"
* The outline:
      Print "Hello World"
(Such a very simple program needs neither an outline ...)
* The pseudo-code:
      Print "Hello World"
(... nor pseudo-code :-)

Now use a text editor such as gedit to enter the code for this program from this file into a text file named helloWorld.java. Java is fussy. The file name must exactly match what the program shows, in this case in the line starting with "public class" (the second text line in the file) and it must have .java as an extension. You may cut the text and copy it into your text editor (you may have to pick Paste from the text editor Edit window to make it work right) or you may re-type it. In this case, it can be valuable to re-type, so your fingers can begin to learn the details of the language. Try to duplicate the indentation and spacing; they help make the program more readable. Note that anything on a line following a double slash (//) is a comment. Comments explain what is happening and make the program more readable.

The compiler we use for Java programs is called javac. To compile your program type
javac helloWorld.java
If you had errors in your typing you will see some kind of complaint before the prompt comes back. Go re-check your program, see if you can find what's wrong, fix it, and try again. Ask for help if you need it.
If you typed everything correctly in about a minute the prompt will simply come back, if you list your directory you will see a new file, called helloWorld.class. The compiler took your code and converted it into machine language.

To run your program, type
java helloWorld
You should see the line "Hello World!" appear.

Try changing the program (maybe change the text and/or print more than one line), re-compile it, and run it again. Did it do what you expected?

When you are ready, move on to the next program. The second Java program we have provided adds two numbers together. Following our suggested model, here is:
* The problem statement:
      Add two numbers together and display the sum
* The outline:
      Add two numbers
      Display the sum.
* The pseudo-code:
      Add two numbers
      Display the sum.

The source for this program is in this file.

Again, use the text editor to enter the program (this time into file addition.java); use the javac compiler to compile the program
javac addition.java
and use java to run the program
java addition

Try changing the program; maybe you can have it subtract the numbers, or do various equations with more than two numbers. You will quickly find that it is more convenient to be able to read the numbers from the terminal than to have to type them each time.

The third program reads two numbers, adds them, and prints the result. Following our suggested model, here is:
* The problem statement:
      Add two integers entered by the user, then print the sum.
* The outline:
      Read two numbers
      Add them
      Print the result
* The pseudo-code:
      Read two numbers
      Add them
      Print the result

The source for this program is in this file. If you use exactly what we have written, the file must be named addReadLine.java

See if you can change this program to do something else.

Here is a simple program that shows you how to read an integer, a word, and a single character:

More programs:
Scanner_Test.java lets you enter from the keyboard and use an integer, a word, and a single character. Remember, the file into which you copy the program must be named EXACTLY Scanner_Test.java or it won't complile properly.
Operator.java Let the user enter numbers and an operator; perform that operation on the numbers (use n to quit)