Plain text version | Style: Default, Black and White, Bath

University of Bath Logo
Dr Marina De Vos
Department of Computer Science
Monday, 20-Nov-2017 23:11:05 GMT

Programming I (CM10134 - CM50147): Lab 6

You will need to show all your code for the following exercises.


  • Exercise on testing and debugging.
    Open the Bricks project from Chapter 6. Test it. There are at least four errors in this project. See if you can find them and fix them. Spend no longer than 30 minutes on this task. You won’t fail if you don’t find all the errors but use the exercise to practice the techniques discussed in the lecture.What techniques did you use to find the errors? What technique did you find most useful?


  • Exercise on designing classes
    Design an application for the game Connect 4 (also called 4 in a row). What classes, instance fields and methods would you need?


  • Executing without BlueJ: Tasks 1-4
    To create and run a Java program without BlueJ you need to use an editor. You can use a word processor, such as Microsoft Word, but you have to be careful that the files you create are pure text files and that their file type is .java .Other editors you can use include Notepad, Wordpad, TextPad (www.textpad.com), emacs (a Unix editor) as well as many others.

    If you haven’t already done so, READ APPENDIX E IN THE BLUEJ BOOK (ignore section E.2 and the references to the game class).
    The following information should also help.

    > Start Unix:
    Start -> All programs -> Unix Applications -> Amos, Midge, Mary
    Login using BUCS username and password

    > Write code
    To use emacs editor: emacs FileName.java &
    (The & means that you can still access the command line while the editor is open).
    Note that you can still create the source code in BlueJ and then run everything in Unix. This is left up to you.
    Save all required classes as .java files in the same folder.

    > Compile code
    Compile your code by executing the command: javac FileName.java
    The FileName corresponds to the class that contains the main function.

    > Fix any errors
    If you have made no errors and the file successfully compiles then the system should respond with a prompt.

    > Execute
    To run your program, execute the command: java FileName

    Tasks 1-4

    1. Copy the following code and save as a .java file
      	public class HelloWorld
      	{
      		public static void main(String[] args)
      		{
      			System.out.println(“Hello World”);
      		} 
      	}
      Explain each part of the signature public static void main(String[] args)
      Compile and run the code.

    2. Create an application that prints out a tree shape such as the following:
               *
              ***
             *****
            *******
           *********
              ***
      	***
              ***
      		
      The user provides the program with the width of the tree (9 in the example) and the length of the trunk (3 for the example).

    3. Create a class TestAccount that creates at least two instances of BankAccount and calls the BankAccount instance methods on the BankAccount objects.

    4. Using recursion, write an application that returns the nth Fibonacci number.
      The Fibonacci sequence is the following: 1 1 2 3 5 8 13 21 34 55 ...
      Each number is the sum of the preceding two numbers in the sequence. We calculate the nth Fibonacci number with the following formula: fib(n) = fib(n-1) + fib(n-2)
      Take fib(0) = 1 and fib(1) = 1.

      Recursion
      Recursion is a programming technique that isn't specific to the Java programming language. The technique involves solving problems through method calling. Consider the following method that computes factorials, where n! is the product of 1x2x3x...xn with 0! defined to be 1 :
      	public int factorial(int n) 
      	{
        		if(n == 0) {
          			return 1;
        		} 
      		else {
          			return n * factorial(n - 1);
        		}
      	}
      You should have noticed that the method calls itself. This is an example of recursion.
      Rules of recursion: There must be a stopping condition and every recursive call must get one step closer to the stopping condition. In the factorial method the stopping condition is n==0 so each call to the factorial method must be with n one step closer to being 0 (i.e., n-1).