CO523 (fundamentals of programming and logic) |
[ class1 | bluej | exercises ]
The purpose of this class is to get you using BlueJ and have a go at writing Java programs in it.
BlueJ, if you've not used it before, is an environment for developing Java programs. Our 1st-year CS students are taught Java programming solely within this environment, and as a result, most 2nd and final-year modules will probably expect people to use it. However, if you're happier using a different environment then that's absolutely fine. This includes things like Eclipse (a professional IDE — integrated development environment), or if you prefer, doing your Java at a command-line or with a simple editor (vi/vim in UNIX, PFE in Windows). BlueJ is undoubtedly the simplest way however. Here's a screenshot of it:
That's what BlueJ looks like when running from raptor (which is the CS undergraduate Unix machine), but it looks pretty much the same when running from Windows (because it's all Java).
First of all, you should make sure that you can start BlueJ successfully — find it from the menus. Then start a new project. You'll need to store your BlueJ projects, for this module and others, somewhere (or if you're not using BlueJ, somewhere to park your files generally). I'd recommend storing things on raptor (the CS Unix host), which you can get access to by mapping an appropriate drive in Windows.
If you haven't done so already, type in the 'hello world' program and make sure this works. In BlueJ, use the "new class" button (top-left) to create a class template. The default BlueJ class has a lot of default/example contents, which for this exercise you should prune (delete) to just leave the basic class, then put in some new contents:
public class Hello { public static void main (String args[]) { System.out.println ("Hello me!"); } }
Run this program and check that it does what you would expect — print out "Hello me!" on the screen. To run the program from within BlueJ, right-click on the "Hello" class which will have appeared on the main form and select the "main" method that appears there. It will ask you for some arguments, simply leave these as the default — normally these represent the command-line arguments passed to a program (but we don't need any here, "args" is not used within the main body of the program).
So far you've seen some fairly straightforward Java. However, computer programs are often complex things (e.g. Windows, Solaris, Word, Excel, etc.) and as such, we need fairly complex ways of expressing what we want the computer to do. This is a gradual process, we'll start simply and go from there! For the curious, a pretty serious bug was found in Excel recently.
Here's the main method body a program that prints a triangle of stars:
public static void main (String args[]) { System.out.println ("*"); System.out.println ("**"); System.out.println ("***"); System.out.println ("****"); }
From the source-code, you can pretty much see what this program is going to do. More useful would be a program that allows us to specify the number of stars to print. Unfortunately, getting input from the user in Java can be fairly tricky, but for now we'll just have the program decide. Furthermore, rather than filling up the 'main' method with code for printing starts, we'll put that in a separate method and call it from 'main'. This is code reuse — if we have a method that prints some stars, we can call it from anywhere, not just 'main'.
Start by creating a new project, and into the body of the class, type (or copy+paste) this:
private static void stars (int n) { // your code here! } public static void main (String args[]) { stars (4); }
Compile and run this program. It should do absolutely nothing!
The object of this exercise is to modify the above program so that it prints lines of stars, where the parameter "n" indicates how many lines of stars to print. To do this, you'll need to know about how Java programs make decisions and repeat sections of code. There are two control structures which you need to know about first, if and while. These look something like this:
if (condition) { // code executed if 'condition' is true } while (condition) { // code executed repeatedly as long as 'condition' is true // unless you want code repeated forever, something in here // should make 'condition' become false at some point }
The 'if' can also have an 'else' part, e.g.:
if (condition) { // code executed if 'condition' is true } else { // code executed if 'condition' is false }
Furthermore, the 'if' can be chained, giving a behaviour that tests each condition until it finds a true one and executes the code there. For example:
if (condition1) { // code executed if 'condition1' is true } else if (condition2) { // code executed if 'condition2' is true } else if (condition3) { // code executed if 'condition3' is true } else { // code executed if none of the conditions are true }
For both the 'if' and 'while', there needs to be some condition. This is a Java expression (in the mathematical sense) that evaluates to either true or false. We'll talk properly about these in the coming lectures, but to get you started, here's a really bad way of implementing the 'stars' method:
if (n == 0) { // print nothing! } else if (n == 1) { System.out.println ("*"); } else if (n == 2) { System.out.println ("*"); System.out.println ("**"); } else if (n == 3) { System.out.println ("*"); System.out.println ("**"); System.out.println ("***"); } else { System.out.println ("Sorry, n too big!"); }
This is nasty for two reasons. Firstly, there is duplicated code — the code that prints a single star is repeated 3 times. Secondly, we only handle fixed values of 'n' (between 0 and 3 inclusive for this code). Fixing the first problem isn't too hard — the 'if' can be restructured to avoid duplicating print statements:
if (n >= 1) { System.out.println ("*"); } if (n >= 2) { System.out.println ("**"); } if (n >= 3) { System.out.println ("***"); }
This is better, but it still cannot handle arbitrary values for 'n' — and we certainly don't want to be writing massive programs! You should implement a 'stars' method which uses a while loop (or two of them, nested) to print out the desired number of stars.
The 'println' method that you've used so far to print things to the screen automatically adds a newline, so that whatever's output next appears on the next line. To avoid this there is a 'print' method, which prints without adding a newline (so whatever's printed next appears after it on the same line). For example, here's a (bad) way of printing 4 stars on a single line:
System.out.print ("**"); System.out.print ("*"); System.out.print ("*"); System.out.println ("");
As a further example, here's an example piece of code that prints the same thing 3 times (note the additional variable declaration):
int n = 0; while (n < 3) { System.out.println ("Hello repeated world!"); n = n + 1; }
Last modified 2nd October 2009 |