CMPE 261 - Object Oriented and Concurrent Programming

  1. A design is given (as per Software Engineering course, but we may need to produce simple ones ourselves)
  2. You already know how to implement a well defined, moderate complexity algorithm into a function.

Before you start

Java review

Dynamically typed languages

Statically typed languages

What's the advantage of strict typing?

Java is strictly object oriented

Java basic data types

    void
    boolean
    byte int long
    float double
    String // Why is it upper case? This is what we will learn in this course

Review exercise 1

Write a Java command line program to print out factorials of numbers from 1 to 100. Make sure to compute factorials with a separate function.

What is wrong with the output?

Check that you remember:

Solution 1

    class Factorials1{
      public static void main(String[] args){
        for(int i=0; i<=100; i=i+1) {
          System.console().format("Factorial of %d is %d \n",i,factorial(i));
        }
      }

      static int factorial(int n) {
        if ((n>=0) && (n<=1))
          return 1;
        else
          return n*factorial(n-1);
      }
    }

Review exercise 2

Make sure your function in the previous example catches the case when the number is negative, and you've put Javadoc comments in your program.

Check that you remember:

Solution 2

    /** A class to print out factorials of numbers from 1 to 100 */
    class Factorials2 {
      public static void main(String[] args){
        try {
          for(int i=0; i<=100; i=i+1) {
            System.console().format("Factorial of %d is %d \n",i,factorial(i));
          }
        } catch (Exception e) {
          System.console().format("An exception occured: %s",e.toString());
        }
      }
      /** A method to return factorial of a  given number */
      static int factorial(int n) throws Exception {
        if (n<0)
          throw new Exception("Cannot compute factorial of a negative number");
        else if ((n>=0) && (n<=1))
          return 1;
        else
          return n*factorial(n-1);
      }
    }

Review exercise 3

Improve your program to accept a number \(n\) as a command line argument, and print factorials from 1 to \(n\).

Check that you remember:

Solution 3

    /** A class to print out factorials of numbers from 1 to n, which is given as the first command line argument */
    class Factorials3 {
      public static void main(String[] args){
        try {
          if (args.length()<1) {
            System.console().format("Missing argument");
            return;
          }
          int limit=Integer.parseInt(args[0]);
          for(int i=0; i<=limit; i=i+1) {
            System.console().format("Factorial of %d is %d \n",i,factorial(i));
          }
        } catch (Exception e) {
          System.console().format("An exception occured: %s",e.toString());
        }
      }
      /** A method to return factorial of a  given number */
      static int factorial(int n) throws Exception {
        if (n<0)
          throw new Exception("Cannot compute factorial of a negative number");
        else if ((n>=0) && (n<=1))
          return 1;
        else
          return n*factorial(n-1);
      }
    }

A new problem

A class as a template of object state

Class vs Object?

Conventions