CISC 370 Lecture Notes for Class No. 4, February 17, 2000
Object-Oriented Programming

Course Home Prev Lect Next Lect Example Progs Exercise Solns
CoreJava Progs
Documentation
Gen'l Docs Java 2 APIs Java Glossary Term Proj Info

Assignment

Graded Assignment #2 (continued - one exercise assigned last time). Due Tuesday, Feb 22.

Reading:
None
Exercises: Rewrite the payroll application so that the dialog between the program and the computer is in the format given in the problem description below. The solution given in the file
$CLASSHOME/example-progs/grossPay/Payroll.java does not strictly adhere to this format.

To prepare programming assignments for submission, do the following for each exercise.

script	        // Produces a file named typescript
whoami 
date 
pwd 
cat *.java 
javac *.java     // mainFile should be the name of the file containing
java mainFile    // your main() method
exit		 // to exit script
Now print the typescript file for submittal at the beginning of lecture and email a copy to Sivaram at burra@cis.udel.edu.

Today's Lecture Topics

  1. A payroll problem as another example of OO programming.

    Develop a Java program to determine the gross pay for employees of a company. The company pays straight-time for the first 40 hours worked and time-and-a-half for hours worked in excess of 40. Your program should prompt for the employee's first and last names, the number of hours worked, and the hourly rate of each employee. The program should then determine and display the gross pay for each employee. The interaction between the program and user should look as follows.
    Enter employee's first name (quit to end): Ying
    Enter last name: Zhang
    Enter hours worked: 50.5
    Enter hourly rate of employee: 11
    The gross pay for Ying Zhang is $613.25
    
    Enter employee's first name (quit to end): Samuel
    Enter last name: Smith
    Enter hours worked: 33
    Enter hourly rate of employee: 10
    The gross pay for Samuel Smith is $330.00
    
    Enter employee's first name (quit to end): quit
    
    A solution is given in the directory $CISC370HOME/example-progs/grossPay/ .

  2. Examples of OOP terminology. In many ways, OOP technology is an effort to mimic in the software production process the successes in producing physical devices. It is often helpful in designing and implementing objects to think about physical analogies. So in this spirit the following table is given.

    OOP TERM(S) PHYSICAL EXAMPLE(S) SOFTWARE EXAMPLE(S)
    Class definition Blueprint
    House plan
    Cookie cutter
    Any class definition, for example Name
    Object
    Instance of class
    Device made from blueprint
    House built from plan
    Cookie
    Created by the Java new operator
    User interface The controls on a clock radio for on/off, volume, setting time setting the alarm, etc. The public components (usually methods) of a class. But sometimes other components like the public constants Math.PI, Color.blue
    Encapsulation
    Data hiding
    The cabinet that hides the electronic and other components that "implement" a clock radio Provided by the Java access modifiers private, protected, and package access
    Instance variables The hidden physical components of
    the clock radio - electronics,
    speakers, etc.
    The data declared in a class definition.
    But not the local variables declared in methods
    and constructors.
    The current state of an object Current settings of the physical components that make up the object. E.g., the setting of the on/off switch, volume control, alarm setting, etc. Current value of all the instance variables
    Inheritance Using pre-existing plans for objects w/o change to design a new object. For example, the designer of the clock radio can take an existing clock and existing radio and use them to design a clock radio. A new box can be built around both but some of their public controls are used in the new device. The Dialog class in the package java.awt.* inherits from the class Window. A Dialog is a Window that accepts input from the user.

  3. RELATIONSHIPS BETWEEN CLASSES
    Relationship Physical Examples Software Example
    Uses PC uses a network class Payroll uses methods from the class Console, but does not contain a Console obj
    Containment (has such an object as a part - usually hidden - of the current object) PC has a Pentium chip. Usually all the controls of the contained component are hidden by the object that contains it. class Employee has a component that is an object of the Name class
    Inheritance (is a) The clock radio is both a clock and a radio. Usually some of the controls of the inherited component are part of the public interface of the new object.

  4. Some classes have no object defined by data like an employee class does. In an employee class you have data items like the name, SS#, salary, etc. A class w/o such data items may simply provide a coherent set of operations. For example, the class corejava.Console that provides the functionality of converting external representations of data to internal representations. This is analogous to the functionality provided by a modem that converts computer signals to telephone signals and vice-versa.

  5. Roles of various aspects of the Java language in OOP

    - Accessor attributes: public, private
    - new operator
    - constructors
    - methods

  6. A class Complex . This is just a beginning of the implementation.

    Complex numbers are numbers of the form a + ib, where a and b are real and i*i = -1. a is called the real part and b the imaginary part.

    Let a + ib and c + id be two complex numbers. Here are the arithmetic rules for complex numbers.

    Zero rule: a + ib == 0 if and only if a == 0 and b == 0.

    Addition rule: (a + ib) + (c + id) = (a+c) + i(b+d)

    Subtraction rule: (a + ib) - (c + id) = (a-c) + i(b-d)

    Multiplication rule: (a + ib) * (c + id) = (ac-bd) + i(ad+bc)

    Define a class Complex and provide a constructor

        Complex( double a, double b)
        
    for constructing the Complex number a + ib.

    Also provide three public (non-static) methods

        add( Complex Y) to add two complex numbers
        subtract( Complex Y) to subtract Y from this
        

    Back to the CISC 370 homepage.

    This page has been accessed times since 16 Feb 2000.

    Corrections, suggestions and comments to Bob Caviness

    Copyright 2000 B. F. Caviness