Home Previous Next

CSC205::Lecture Note::Week 01
Assignments | Code | Handouts | Resources | Email Thurman { compufoo::Twitter | Facebook | YouTube}
GDT::Bits:: Time  |  Weather  |  Populations  |  Special Dates

Overview Assignment(s)
  1. [assessment] #day0 [due 1/17/02018]
  2. [assessment] #email [due 1/19/02018]
  3. [program] #MakeHelloWorld [due 1/21/02018]
HandoutsCSC205 Syllabus | About CSC205 | How To Submit Assignments

Code HelloWorld.java

Syllabus Review

It is the student's responsibility to read and understand the syllabus. Some of the more important points are:

The following information was copied from the Maricopa.edu website.

MCCCD Official Course Description for CSC205.

   "Covers Object-Oriented design and programming; elementary data 
    structures; arrays; lists; stacks; queues; binary trees; recursion; 
    searching and sorting algorithms.  Prerequisites: CSC110, or 
    permission of Instructor."

MCCCD Official Course Competencies for CSC205.

 1. Describe how modern Software Engineering techniques are 
    used in program development. 
 2. Implement programs that use Object-Oriented Programming 
    techniques: classes, aggregate classes, inheritance, 
    interfaces, abstract classes, and polymorphism. 
 3. Write applications that handle run-time errors by 
    using Exception Handling techniques. 
 4. Write programs using basic data structures such as 
    arrays, ArrayLists, and Vectors. 
 5. Implement linked lists, stacks, and queues using 
    Object-Oriented techniques. 
 6. Implement Binary Search Trees and use recursive methods 
    as part of the implementation. 
 7. Analyze and compare the efficiency of different searching 
    and sorting algorithms, including algorithms that are 
    implemented recursively. 
 8. Write applications that read and write text and Object files. 
 9. Discuss social and ethical issues related to Computer Science. 
10. Implement a simple Graphical User Interface as part of 
    an application. 

{TopOfPage} {Oracle.com::API Specification | Tutorial} {Derek Banas Videos} {Data Structure Visualizations}

CSC205 Competency 1: Modern Software Engineering Techniques

Wikipedia.org::Software development methodology

{TopOfPage} {Oracle.com::API Specification | Tutorial} {Derek Banas Videos} {Data Structure Visualizations}

CSC205 Competency 2: Object-Oriented Programming Techniques

Introduction to Classes

A class is a blueprint or template that describes an object.

Class Syntax
   classDeclaration {
Syntax of a class declaration:
   [ modifiers ] class ClassName [ extends SuperClassName ]
                                 [ implements InterfaceNames ]

   Some example modifiers are:  public, abstract, final 
   If no modifier is specified, then it defaults to "package."

The class body contains static data members, instance data members, static methods and instance methods.

By convention, class names begin with an uppercase letter.

About Constructor Methods

A constructor is a special method that is used to help guarantee that objects are initialized to a "valid" or "sane" state when they are created (instantiated).

{TopOfPage} {Oracle.com::API Specification | Tutorial} {Derek Banas Videos} {Data Structure Visualizations}

Review: The Anatomy of a Class

Let's assume we have the following class definition.

   public class ClassAnatomy {
      static int key;
      int color;

      public static void main(String[] a) {
         int i;
      public void foo() { }

The names key and color are member variables. In other words, they are members of the class and are defined outside of any method.

The member variable key is a class variable and color is an instance variable. All static defined variables are class variables.

All member variables are initialized to zero (all-bits-zero) by the JVM.

The variable named i is a local variable and it is local to the method named main(). The variable i can only be used inside the main() method. Local variables are not initialized to any known value and in many cases that Java compiler will not let an un-initialized local variable be used.

The variable named a is a local variable to the method named main(). It is also called a parameter. Parameters are initialized to the value passed as an argument when the method is called.

Method main() is a class method and method foo() is an instance method. Instance methods can only be used with a specific instance of the class.

Aggregate Classes

Aggregate classes are used to implement has-a relationships.


   class Game {
      Controller c;

   class Controller {

A Game object has-a Controller object.


Inheritance allows classes to be defined in terms of other classes.

In Java, inheritance is implemented when a class extends another class.

Polymorphism is when an object of a child class is used as an object of its parent class. In other words, an object of a given class can have multiple forms, either as it own class or as any class it extends.

By default, all classes extend the Object class that comes with the JSDK. Therefore, every object is-an object of class Object.

Some benefits obtained from using inheritance are.

You are not limited to just one layer of inheritance. The inheritance tree, or class hierarchy, can be as deep as needed.

In general, the further down in the hierarchy a class appears, the more specialized its behavior. [Class extension is most commonly used for specialization.]

The following is an example class hierarchy.

                     (account#, balance)
                    |                    |
            CheckingAccount       SavingsAccount
                                |                     |
                          PassBookAccount     InvestmentAccount
   public class BankAccount extends Object {...}
   public class CheckingAccount extends BankAccount {...}
   public class SavingsAccount extends BankAccount {...}
   public class PassBookAccount extends SavingsAccount {...}
   public class InvestmentAccount extends SavingsAccount {...}

   CheckingAccount  "is-a"  BankingAccount  and  PassBookAccount
   "is-a"  SavingsAccount  and so on.

	Note:  BankAccount extends Object.

Java supports single-inheritance only (i.e. a class can extend only one class).

Typical characteristics of a derived class are.

The representation of the parent class cannot be altered (i.e. neither data nor functionality [behavior] can be removed).

Functionality defined in the parent class can be modified by overriding the appropriate methods in the derived class.

If a parent method is overridden in the child class and the child wants to invoke the parent's method, then the super keyword must be used to qualify the method name.

   class Foo {
      void echo() {...}

   class Bar extends Foo {
      void echo() { 
         super.echo();  //invoke echo() implemented in the parent class

From 1999 (so it might be dated/stale): JavaWorld.com::How to avoid traps and correctly override methods from java.lang.Object

{TopOfPage} {Oracle.com::API Specification | Tutorial} {Derek Banas Videos} {Data Structure Visualizations}


From the The Java Programming Language by Arnold, Gosling, Holmes.

The fundamental unit of programming in Java is the class, but the fundamental unit of object-oriented design is the type. Interfaces define types in an abstract form as a collection of methods or other types that form the contract for that type.

An interface is defined using the keyword interface rather than class.

An interface contains no instance variables, nor does it contain any method implementations. You cannot create instances of an interface.

   interface Foo { ... }

   //! Foo f = new Foo();   // not legal

An interface can declare three kinds of members:

All interface members are implicitly public.

A class guarantees to implement the methods of an interface by using the implements keyword when the class is defined.

   class X implements Y

Any class that implements an interface must define (i.e. implement) the methods, if any, declared in the interface.

A class can implement more that one interface. In addition, a class can extend another class and still implement one or more interfaces.

   class X implements Y, Z { ... }
   class X extends A implements Y, Z { ... }

To an extent, interfaces support the concept of multiple inheritance.

Common interfaces from the java.lang package:

An interface can be used to "tie" together classes that are in separate inheritance trees.

                Assignment               Interface:  Submitable
            Exam   Program   Homework
            ...    ...       ...

              Game                       Interface:  Submitable
  Guessing  Yahtzee   Blackjack
  ...       ...       ....

Another example...

An interface can be used to encapsulate a collected of related manifest constants (i.e. simulate an enum in C).

   interface ExitStatus {
      int EXIT_SUCCESS = 0;
      int EXIT_FAILURE = 1;

When an interface declares named constants, then they are implicitly defined to be public, static, and final. [blank finals are not allowed]

Methods declared in an interface are implicitly abstract.

An interface can extend (i.e. inherit) other interfaces.

   interface Sortable { ... }
   interface Searchable { ... }
   interface Container extends Sortable, Searchable { ... }
Any class that implements interface Container must also implement the Sortable and Searchable interfaces.

Although you cannot instantiate an object using an interface, you can use an interface as the type for variable and assign to that variable any object whose class implements the interface. This allows for polymorphic run-time binding.

   interface Container { ... }

   class List implements Container { ... }

   List list = new List();
   Container c = list;  // list is a container

From a naming convention perspective, many interfaces have a -able suffix applied to them.

{TopOfPage} {Oracle.com::API Specification | Tutorial} {Derek Banas Videos} {Data Structure Visualizations}

Abstract Classes and Methods

An abstract class is one that is used for derivation only. Objects cannot be instantiated from an abstract class. Attempting to do so at compile-time is a syntax error. Attempting to do so at run-time causes an error.

An abstract class can contain data and methods. Some of the methods may in turn be declared to be abstract. Abstract methods have no implementations.

If a class contains an abstract method, then it must be an abstract class. However, you can have an abstract class that doesn't have any abstract methods.

An abstract method is a method that is declared, but the method's implementation is found in one of the derived classes. In otherwords, the classes that extend an abstract class are responsible for providing implementations of the abstract methods.


      abstract class SomeClassName { ... }

The Number class in the java.lang package represents the abstract concept of numbers. It is used as a superclass to the wrapper classes (e.g. Integer and Float).

   public abstract class Number implements java.io.Serializable {
       public abstract int intValue();
       public abstract long longValue();
       public abstract float floatValue();
       public abstract double doubleValue();
       public byte byteValue() { return (byte)intValue(); }
       public short shortValue() { return (short)intValue(); }

Each wrapper class (e.g. Byte, Integer, etc.) extends Number (except for Character).

The Number class ensures that there is common functionality across all classes that extend it (e.g. intValue()).

Every class that inherits from Number must implement the methods that are defined to be abstract. The Number class contains a couple of non-abstract methods (shortValue() and byteValue()); in other words, these methods are implemented in the class. If you were to write a class that extends Number, then you would have to check to see if the non-abstracts methods work for you specific type of number. If they don't, then you can provide implementations for the non-abstract methods by overriding them.

You cannot instantiate a Number object. You can, however, have a handle (i.e. object variable) to a Number object (e.g. Number num). The values that can be assigned to a Number object variable are handles to objects instantiated from classes that extend the Number class.

   Number num = new Integer(100);  // a typecast is not necessary

Docs.Oracle.com::Tutorial::Abstract Methods and Classes

{TopOfPage} {Oracle.com::API Specification | Tutorial} {Derek Banas Videos} {Data Structure Visualizations}


Exceptions are "exceptional" conditions that can occur while a program is executing (i.e. running). Some exceptions are caused due to software defects (e.g. using a null object handle, or indexing beyond the bounds of an array).

   String s = null;
   String s1 = "hello";
   if (s.equals(s1)) { ... }     //this will cause an exception
   int[] array = new int[10];
   array[array.length] = 200;    //this will cause an exception

Exceptions are also generated by errors that are beyond the control of the program (e.g. the program needs to access a file that doesn't exist, or the program needs to fetch a webpage given an invalid [mal-formed] URL).

Exceptions can be "caught" by programs, but if an exception does occur that isn't caught, then the program (assuming it is non-graphical) terminates. (A GUI program returns to the user interface).

Exceptions are objects that are created from the Exception and RuntimeException classes.

   Error               Exception
   .....               /  ...  \
                  IOException   RuntimeException 
                    (checked)     (unchecked)

There are two categories of exceptions: checked and unchecked. Checked exceptions must be caught (otherwise, your program will not compile). Unchecked exceptions typically are not caught (i.e. they cause your program to terminate when they occur).

A method can throw an exception when it encounters a situation it cannot handle. There are numerous methods that come with the JCL (java class library) that throw exceptions.

A method declaration must contain a list of the the checked exceptions it may throw.

   public int someMethod() throws IOException, MalformedURLException

A method does not need to declare RuntimeException inherited exceptions that it may throw.

If a method overrides a method from a parent class, then the child method cannot throw more checked exceptions that the parent method does.

A method can say it throws exceptions that it really doesn't throw.

Catching an Exception

Whenever a method is called that throws a checked exception, then that method call must be called from within a try block and an exception handler (catch block) must be implemented.

   try {
   } catch (IOException e) {

If an exception occurs, then the flow control of the program jumps to a catch block. All remaining statements, if any, within the try block are skipped. If no exception is thrown, then the catch blocks are skipped.

A try block must be a compound statement. Ditto for the catchblocks.

The catch blocks are passed an argument: a reference to the exception (which may or may not be used).

Throwing an Exception

An exception is thrown as follows.

   throw new EOFException();


   throw new EOFException("file's empty");
      //supply additional information about the exception
      //this information can help the client "fix" the problem

A method that throws an exception does not return to the caller.

An exception can be re-thrown. You may want to do this in the event an exception occurs: you catch it locally to clean up any locally allocated objects and/or system resources, and then "pass" the exception on to those who know about handling it.

Exception A can be caught, and exception B can be thrown.

Creating an Exception Class

You can create your own exceptions by extending Exception or some child class of Exception.

   class MyException extends Exception {
      public MyException() {}
      public MyException(String msg) { super(msg); }

The following exception terminology was obtained from Just Java by Peter van der Linden:

Note Java Other Languages
An error condition that happens at run time Exception Exception
Causing an exception to occur Throwing Raising
Capturing an exception that has just occurred, and executing statements to resolve it in some way Catching Handling
The block that does this Catch clause Handler
The sequence of call statements that brought control to the method where the exception happened Stack trace Call chain
Miscellaneous Comments

Detect errors at a low level, handle them at a high level.

Use exceptions only for exceptional situations.

YouTube.com::CS 61B Lecture 14: Exceptions

{TopOfPage} {Oracle.com::API Specification | Tutorial} {Derek Banas Videos} {Data Structure Visualizations}

Home Previous Next