Learn Programming in Java

Seaman in Java (Variables)

<<Previous | ToC | Next >>
 
If you got here by doing all the design of the previous seven pages, then you are looking at your English code in the panel below. Otherwise you should be looking at my version, either the half you got from six pages back or the back half from only one page back. It's best if you arrange your computer screen so that this browser window is off to one side -- I suggest the left edge of your screen -- and the BlueJ "Hello" program editing window off to the other side, so you can see both at once, side by side. You will be typing in the BlueJ window, and reading my advice here.

Translating a good English program design into Java is not much more than recognizing which of the Five Concepts in the table below each line represents, and then spelling that concept in Java.

Variables

Each of your English variables will become a Java variable, same spelling and everything (single word, starts with a letter, only letters and digits), but in Java you need to tell the compiler what type the variable is. If your variable is text, the name of something or somebody, or you are building a line to be printed, then the type you need is String (see Variables in Lesson #2). The bottom of that same page also tells about variables that hold a single character, type char.

If your variable is used to count things or hold the number of something, then you need the whole-number type int as described in Lesson #2n). That same page also tells about variables that hold a simple true/false value, type boolean.

We started Lesson #5 introducing arrays. There are no such things as arrays in the English language, groups of things, so of course our English program has nothing that looks like arrays. Some of the places where have String variables, we might prefer to make them arrays of type char. Do you think you can do that? For example, we need to step through the word being guessed to look at individual characters, it's a good array candidate. This one line both declares and initializes an array of 99 characters, do you think that is enough for the longest word somebody might need to guess?

char[] word = new char[99];

So figure out what the program does with each variable, what its type should be, and then put a declaration for that variable at the top of the white space in your Hellow program. Don't forget to put a semicolon at the end of each statement line. Then click the "Compile" button to make sure there are no mistakes.

Java is very unforgiving about mistakes -- and that is a good thing! Our first Venus probe crashed and was essentially worthless because the Fortran compiler did not catch the kinds of mistakes that Java catches -- so you need to be very careful, and be glad the compiler caught it before somebody spent a million dollars and your program failed when it was needed (and you weren't around to fix it).

If there are mistakes, look carefully at the first of them to see what you did wrong, so you can fix it. That will usually clear up several following errors, so keep trying to compile after each repair. If you can't figure out what is wrong, get help. You don't necessarily know what to look for, nor what the error messages mean the first time you do this, but after a while you won't need so much help. That's what the instructor is here for.

The other part of the "Variables" line in the Five Concepts table below (but not shown) is the calculation of variable values. In English we have been using the command word "let" to signify that is what is happening. In Java the "let" is implied, the compiler can figure it out from the assignment operator "=". Otherwise it looks pretty much the same (except you need a semicolon at the end). Can you do that for the variable assignments (all the "let"s) in the English program? Leave blank lines as place holders for the lines you are not translating yet. Then click the "Compile" button to make sure there are no mistakes.

Character input and text output you are already familiar with in Java. Why don't you convert those English commands too? Click the "Compile" button to make sure there are no mistakes. Of course the program still cannot run correctly because the conditionals and iteration are not there yet. We'll do that after you turn the page.

Five Basic Concepts
Sequence
Iteration
Conditional
Variables
Input/Output

  <<Previous | ToC | Next >>

Revised: 2021 June 3