Introduction to Programming: Additional work for week 5

Once we start using structured statements, the importance of good layout of Java code becomes crucial. Where you insert spaces or break lines in Java makes absolutely no difference to the Java compiler (except you can't insert a space or break a line in the middle of a word, and a space in a string literal (i.e. something beginning with " and ending with ") means a space). But it makes a big difference to how your code appears to the human reader, and thus how easy it is to understand. We have also encountered other issues of "programming style", for example how convoluted an expression has to be before it's best to break it into separate parts and use a separate variable to store the value of each part temporarily as the final value is calculated.

Advice on good programming style varies between authors, for example Barnes recommends always using curly brackets with loops, whereas others (myself included) think overuse can make code cluttered and they ought to be omitted in loops whose body is only one statement. However, there are many elements of good programming style, for example indenting substatements in loops and ifs, which everyone agrees on. Here is some reading on programming style:

Basic coverage of the Java primitive types and the arithmetic and logic operators can be found from many of the course notes and tutorial sites listed in the Worldwide Notes and resources page. A good set of summary slides can be found by clicking the button marked "Index" on the slide found here (the message on this slide should be borne in mind as well). The "lecture 4" slides cover the operators. A good summary of the basics can be found in the Week 2 section of Elliotte Rusty Harold's Java notes, which also covers some other aspects of Java which we will cover later. It would also be a good idea at this stage to read Lynn Andrea Stein's material on expressions and statements.

Apart from reading, you should now have enough Java to be able to cope with some simple programming exercises. You already do some in your scheduled lab times, but you should so more on top of that. Some simple arithmetic exercises can be found in the local notes section here. You will find more exercises in your textbooks: we have now covered the first seven (except for chapter 4) chapters of Barnes' book, most of chapters 1, 2, 5 and 6 from the Horstmann textbook, most of the first five chapters of the Bishop textbook, large parts of the first five chapters of the Deitel textbook, and most of the first three chapters of the Pohl textbook. They all have exercises in them! Remember, all the code mentioned in the Barnes book, the Horstmann book and the Pohl book is available from within the directory:

/import/teaching/BSc/1st/ItP

Exercises from the Barnes book

On page 104 of the Barnes book, exercise 5.8 mentions a Calculator program. You can find the code for that program in
/import/teaching/BSc/1st/ItP/Barnes/Sequence/Calculator
To copy it into your own directory create a subdirectory called Calculator by typing into a Linux window:
mkdir Calculator
then copy all the files ending in .class from the directory above into your new directory by typing:
cp /import/teaching/BSc/1st/ItP/Barnes/Sequence/Calculator/*.class Calculator
Now go into your Calculator directory by typing:
cd Calculator
You do not need the .java files that were used to create the .class files that you have there now. You can run the Calculator program by typing:
java CalculatorMain
Please note that although I have spelt out exactly how to copy these files into your own directory, really by now you shouldn't need to have to be told how to do that. You have already been given documention on using Linux and how to do basic things like making directories and copying files, and you were told to work through these. I won't tell you how to do it again.

When you run the Calculator program you get a window with a calculator in it that accepts input consisting of numbers and arithmetic operators and processes them with the Java precedence. You can use it, as instructed, to check your answers to Barnes's exercise 5.9.

The BarChartMinder and GraphMinder programs mentioned in chapter 6 of Barnes's book can be found in the directory:

/import/teaching/BSc/1st/ItP/Barnes/Selection/ChartLib
You will need to copy the files ending in .class and .java to do exercises 6.4-6.8 and 6.22-6.25 of the Barnes book. The files ending in .java are the ones you may need to alter to do the exercises, the ones ending in .class are used by the ones ending in .java, but you don't need to alter them or know what's in them, just assume they work correctly and provide you with the objects you need.

Programs from the Horstmann book

You will find the program Richter.java from Chapter 5, page 195 of the Horstmann book, and the program Tax.java from page 201 of the same chapter in the directory:
/import/teaching/BSc/1st/ItP/Horstmann/ch5
They make use of the class ConsoleReader.class to read values, which is also found there. Only the .java files are there, but you will find if you compile a file like Richter.java it will also automatically compile ConsoleReader.java to produce ConsoleReader.class so you do not have to do that separately.

Matthew Huntbach
25th October 2000