Copyright © Cay S. Horstmann 2011 Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

Introduction

Cover page image

Cay S. Horstmann | Nguyễn Hứa Phùng

Course Objectives

The “Cambrian Explosion” of Languages

Machine Language

Assembly Language

High-Level Language

Compiler

Interpreter

Virtual Machine

Programming Language Classification

FORTRAN

FORTRAN

C     99 BOTTLES OF BEER ON THE WALL
      INTEGER BOTTLS
      DO 50 I = 1, 99
      BOTTLS = 100 - I
      PRINT 10, BOTTLS
10    FORMAT(1X, I2, 31H BOTTLE(S) OF BEER ON THE WALL.)
      PRINT 20, BOTTLS
20    FORMAT(1X, I2, 19H BOTTLE(S) OF BEER.)
      PRINT 30
30    FORMAT(34H TAKE ONE DOWN AND PASS IT AROUND,)
      BOTTLS = BOTTLS - 1
      PRINT 10, BOTTLS
      PRINT 40
40    FORMAT(1X)
50    CONTINUE
      STOP
      END

FORTRAN Features

LISP

LISP

(define (down n k)
  (if (< n k) '()
    (cons n (down (- n 1) k))))

(define (plural? n . up)
  (let ((.. string-append)
        (num (number->string n))
        (bot " bottle")
        (ltr (if (null? up) "n" "N")))
    (case n ((0) (.. ltr "o more" bot "s"))
            ((1) (.. num bot))
            (else (.. num bot "s"))

(define (verse n)
  (let ((.. string-append)
        (top (plural? n 1))
        (mid (plural? n))
        (nxt (plural? (if (= n 0)
                          99
                          (- n 1))))
        (beer " of beer")
        (wall " on the wall")
        (actn (if (= n 0)
                  "Go to the store and buy some more, "
                  "Take one down and pass it around, ")))
    `(,(.. top beer wall ", " mid beer ".")
       .  ,(.. actn nxt  beer wall "."))))

(define (sing verse)
  (let ((n newline))
    (display (car verse)) (n)
    (display (cdr verse)) (n) (n)))

(for-each sing (map verse (iota 99 0)))

LISP Features

ALGOL 60

ALGOL 60

'begin'

'comment'
    99 Bottles of Beer on the Wall
;

    'integer' 'procedure' bottles(n);
    'value' n;
    'integer' n;
    'begin'
        'if' n < 1 'then' outstring(1, "no more ") 'else' outinteger(1, n);
        'if' n = 1 'then' outstring(1, "bottle") 'else' outstring(1, "bottles");
        outstring(1, " of beer");
    'end';

    'integer' i;

    'for' i := 99 'step' -1 'until' 1 'do' 'begin'
        bottles(i); outstring(1, " on the wall, ");
        bottles(i); outstring(1, "\n");
        outstring(1, "take one down and pass it around, ");
        bottles(i - 1); outstring(1, " on the wall.\n");
    'end';

'end'

ALGOL 60 Features

C

C

#define MAXBEER (99)

void chug(register int beers) {
    char howmany[8], *s;
    s = beers != 1 ? "s" : "";
    printf("%d bottle%s of beer on the wall,\n", beers, s);
    printf("%d bottle%s of beeeeer . . . ,\n", beers, s);
    printf("Take one down, pass it around,\n");

    if(--beers) sprintf(howmany, "%d", beers); else strcpy(howmany, "No more");
    s = beers != 1 ? "s" : "";
    printf("%s bottle%s of beer on the wall.\n", howmany, s);
}

main() {
    int beers;
    for(beers = MAXBEER; beers; chug(beers--))
      puts("");
    puts("\nTime to buy more beer!\n");
}

C Features

C++

C++

#include <iostream>

template<int I>
class Loop {
public:
  static inline void f() {
    cout << I << " bottles of beer on the wall," << endl
         << I << " bottles of beer." << endl
         << "Take one down, pass it around," << endl
         << I-1 << " bottles of beer on the wall." << endl;
    Loop<I-1>::f();
  } 
};

class Loop<0> {
public:
  static inline void f() {
    cout << "Go to the store and buy some more," << endl
         << "99 bottles of beer on the wall." << endl;
  }
};


main() {
  Loop<3>::f();
}

C++ Features

Java

Java

class Verse {
    private final int count;

    Verse(int verse) {
        count = 100-verse; 
    }

    public String toString() {
        String c =
            "{0,choice,0#no more bottles|1#1 bottle|1<{0} bottles} of beer";
        return java.text.MessageFormat.format(
            c.replace("n","N")+" on the wall, "+c+".\n"+
            "{0,choice,0#Go to the store and buy some more"+
            "|0<Take one down and pass it around}, "+c.replace("{0","{1")+
            " on the wall.\n", count, (count+99)%100);
    }
   
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++)
            System.out.println(new Verse(i));
    }
}

Java Features

Java is not the End of History

Assessment

Homework

Labs

???

References

Web Sites

Questions?