next up previous contents
Next: 3. Introduction to Object-oriented Up: Algorithms and Data Structures Previous: 1. Introduction and Overview   Contents

Subsections

2. Simple Algorithms and Review of Java

How are data processing algorithms designed? Write a program to display the following.

*        *
 *      *
  *    *
   *  *
    **

Q 2. Now make it general enough to take a parameter `height'.

Let's build up to it.

//----- Prog1.java ---------------------------------
import java.io.*;
class Prog1{
  public static void main(String args[])
  {
    PrintStream o= System.out;
    o.print('*');
    o.print('\n');
  }
}

2.1 Sequence

Now, print: *****

//----- Line5.java ---------------------------------
// prints 5 '*' line on screen
//--------------------------------------------------
import java.io.*;

class Line5{
  public static void main(String args[])
  {
    PrintStream o=System.out;

    o.print('*');
    o.print('*');
    o.print('*');
    o.print('*');
    o.print('*');

    o.print('\n');
  }
}

//----- Line51.java ---------------------------------
// format now clearly shows parallel between data structure and program
// structure (program wrapping removed)

o.print('*'); o.print('*'); o.print('*'); o.print('*'); o.print('*'); o.print('\n');

2.2 Repetition

//----- Line5r.java ---------------------------------
// prints 5 '*' line on screen using repetition
class Line5r{
  public static void main(String args[])
  {
    int n= 5;
    for(int i= 0; i< n; i++){
      o.print('*');
    }
    o.print('\n');
  }
}

2.2.1 Sequence of Repetitions

Now we want to print:

*
**
***
****
*****

//----- Tr1.java ---------------------------------
// prints a backwards facing triangle
//*
//**
//***
//****
//***** 
// Analysis:
//        Stars
// Line 0: 1
//      1: 2
//      2: 3   etc.
//     --------------------- 
//--------------------------------------------------
import java.io.*;

class Tr1{
  public static void main(String args[])
  {
    PrintStream o= System.out;

    int nstars= 1;
    for(int i= 0; i< nstars; i++){
      o.print('*');
    }
    o.print('\n');

    nstars= 2;
    for(int i= 0; i< nstars; i++){
      o.print('*');
    }
    o.print('\n');

    nstars= 3; // etc...
}

2.2.2 Repetitions of Repetitions -- Nested

We can improve a lot on Tr1.java.

//----- Tr1r.java ---------------------------------
// Analysis:
//        Stars
// Line 1: 1
//      2: 2
//      3: 3   etc.
//     ---------------------
// i.e. generalising: 
// Line j  j 
//--------------------------------------------------
class Tr1r{
  public static void main(String args[])
  {
    for(int j= 0; j< 5; j++){
      //---- this does the stars -------------
      for(int i=0; i<=j; i++){
        o.print('*');
      }
      //--------------------------------------
      o.print('\n');
    }
  }
}

2.2.2.0.1 Data Pattern -- Program Pattern

//----- Tr2.java ---------------------------------
// can you see the program has the same pattern as the data
//--------------------------------------------------
class Tr2{
  public static void main(String args[])
  {
    o.print('*'); 
    o.print('\n');
    o.print('*'); o.print('*'); 
    o.print('\n');
    o.print('*'); o.print('*'); o.print('*'); 
    o.print('\n');
    o.print('*'); o.print('*'); o.print('*'); o.print('*'); 
    o.print('\n');
    o.print('*'); o.print('*'); o.print('*'); o.print('*'); o.print('*'); 
    o.print('\n');
  }
}

2.3 Procedures -- Methods ...Functions

2.3.1 Without Parameters

//----- Tr2p.java ---------------------------------
// prints filled triangle of '*' on screen using procedures 
//--------------------------------------------------
class Tr2p{
  private static PrintStream o = System.out;
  public static void main(String args[])
  {
    p1(); nl(); 
    p2(); nl();
    p3(); nl();
    p4(); nl();
    p5(); nl();
  }

  static void p1(){
    PrintStream o= System.out;
    o.print('*');
  }
  //etc...
  static void p5(){    
    PrintStream o= System.out; 
    o.print('*'); o.print('*'); o.print('*'); o.print('*'); o.print('*');
  }

  static void nl(){    
    o.print('\n');
  }
}

Question. When is it wise to create a procedure?

2.3.2 Procedures with Parameters

//----- Tr3r.java ---------------------------------
// using repetition and functions/methods
// Analysis:
//        Stars
// Line 1: 1
//      2: 2
//      3: 3   etc.
//     -------------i.e. generalising: 
// Line j  j 
//--------------------------------------------------
class Tr3r{
  public static void main(String args[])
  {
    int h= 5;

    for(int j=1; j<= h; j++){
      stars(j);
      nl();
    }
  }
  static void stars(int n)
  {
    for(int i=0; i< n; i++){
      o.print('*');
    }
  }  
  static void spaces(int n)
  {
    for(int i=0; i< n; i++){
      o.print(' ');
    }
  }
  static void nl()
  {
    o.print('\n');
  }
}

Exercise: change h and see if it generalizes okay.

Equipped with these procedures, we are now in business, and can create nearly any pattern in very quick time.

//----- Tr4.java ---------------------------------
//*****
//****
//***
//**
//* 
// see Tr3r.java
// Analysis:
//        Stars
// Line 0: 5
//      1: 4
//      2: 3   etc.
//     ---------------------
// i.e. generalising: let h be height 
// Line j  no. stars = h-j

// notice how little change from Tr3r.java -- just //1, //2
//--------------------------------------------------
class Tr4{
  public static void main(String args[])
  {
    PrintStream o= System.out;
    int h= 5;

    for(int j=0; j< h; j++){ //1
      stars(h-j); //2
      nl();
    }
  }

Exercise: change h and see if it generalizes okay.

Now, getting more difficult ...

//----- Tr5.java ---------------------------------
//    *
//   **
//  ***
// ****
//***** 
// Analysis:
//        Spaces  Stars
// Line 0: 4        1
//      1: 3        2
//      2: 2        3   etc.
//     ---------------------
// i.e. 
// Line j: 4-j      j+1 
// see Tr3r.java
//--------------------------------------------------
class Tr5{
  public static void main(String args[])
  {
    int nlines= 5; 

    for(int j=0; j< nlines; j++){
      spaces(4-j);
      stars(j+1);
      nl();
    }
  }
}

But Tr5.java will not generalise - change nlines to 8 and you get a mess.

//----- Tr6.java ---------------------------------
// from Tr5.java
//    *
//   **
//  ***
// ****
//***** 
// Analysis: -- let's generalise (height= h) & count from 1
//        Spaces  Stars
// Line 1: h-1      1
//      2: h-2      2
//      3: h-3      3   etc.
//     ---------------------
// i.e. 
// Line j: h-j      j 
//--------------------------------------------------
import java.io.*;

class Tr6{
  public static void main(String args[])
  {
    int h= 5; 

    for(int j=1; j<= h; j++){
      spaces(h-j);
      stars(j);
      nl();
    }
  }
}

Exercise: Based on Tr6.java, analyse, design and implement a program to do:

*****
****
***
**
*

//----- Tr10.java ---------------------------------
//            Analysis
//             Sp  St
//************ 0   12 
// **********  1   10
//  ********   2    8
//   ******    3    6    
//    ****     4    4
//     **      5    2
// Let's generalise (height= h) & count from 0
//        Spaces  Stars
// Line 0: 0        h*2
//      1: 1        (h-1)*2
//      2: 2        (h-2)*2   etc.
//     ---------------------
// i.e. 
// Line j: j        (h-j)*2 
//--------------------------------------------------
import java.io.*;

class Tr10{
  public static void main(String args[])
  {
    int h= 6; 

    for(int j=0; j< h; j++){
      spaces(j);
      stars((h-j)*2);
      nl();
    }
  }
}

Exercise: In Tr10.java, change h and see if it works okay.

//----- Tr20.java ---------------------------------
//            Analysis
//             Sp1 St  Sp2  St
//
//*        *    0  1    8   1
// *      *     1  1    6   1    
//  *    *      2  1    4   1
//   *  *       3  1    2   1
//    **        4  1    0   1

// And let's generalise (height= h) & count from 0
//        Sp1       Sp2
// Line 0: 0        (h-1)*2
//      1: 1        (h-2)*2
//      2: 2        (h-3)*2   etc.
//     ---------------------
// i.e. 
// Line j: j        (h-j-1)*2 
//--------------------------------------------------
import java.io.*;

class Tr20{
  public static void main(String args[])
  {
    int h= 5; 

    for(int j=0; j< h; j++){
      spaces(j);
      stars(1);
      spaces((h-j-1)*2);
      stars(1);
      nl();
    }
  }
}

Exercise: In Tr20.java, change h and see if it works okay.

2.4 Selection

//----- Tr10sel.java ---------------------------------
// as Tr10.java, but selecting stars, stripes
//************
// ----------
//  ********
//   ------
//    ****
//     --
//--------------------------------------------------
class Tr10sel{
  public static void main(String args[])
  {
    int h= 6; 

    for(int j=0; j< h; j++){
      spaces(j);
      if(j%2==0)stars((h-j)*2); // even number if remainder of j/2 == 0
      else dashes((h-j)*2);
      nl();
    }
  }
  static void dashes(int n)
  {
   PrintStream o=System.out;
    for(int i=0; i< n; i++){
      o.print('-');
    }
  }
}

2.5 Exercises

1. Write a Java Program to display an arbitrary filled rectangle of *s - specified by h(eight) and w(idth);

2. Write a Java Program to display a rectangle outline, $ 5 \times 5$.

3. Write a Java function, printHexDigit(int n) which will print a Hexadecimal digit -- 0, 1, ...9, 10 (A), ...15 (F) as follows. Design your own font for 3 onwards.

*****    *   *****    *****
*  **    *       *        *
* * *    *   *****    *****
**  *    *   *            *
*****    *   *****    *****


next up previous contents
Next: 3. Introduction to Object-oriented Up: Algorithms and Data Structures Previous: 1. Introduction and Overview   Contents
jc 2005-11-16