Problem Solving and Computing (CSC-103 98S)
Outline of Class 7: Introduction to Java
Held: Tuesday, February 10, 1998
- The book has finally arrived in the bookstore. Please go pick up
copies.
- Many of you are using your journals only for the journal assignments.
You should be writing notes about problem solving in general and
problems in specifid as you go.
- I'd strongly encourage you to continue to discuss problems
with each other after class. A number of you reported getting stuck and
not knowing what to do next. Talking to someone else working on the same
problem (even someone else stuck on the same problem) can help.
- A number of you didn't reflect on
journal assignment five
(why did Sam change the wording of a problem?).
- So, why did I reword the triangles question? For a number of reasons.
First, I wanted you to think about variants of the problem. I also
thought that the variants would help you think about some issues that
you might not have come up with as readily (e.g., that we might have
triangles that were not isocoles). I worried that the use of slopes
might consuse some of you. Finally, I saw at least one interpretation of
the previous problem that seemed to permit an infinite number of triangles.
- Today we'll be working at our computers, so you're free to pick your
partners. I'd prefer that you alternate responsibilities and you may
even want to keep two computers running (one for instructions; one for
doing the work).
- Today's assignment is
computing assignment 4.
- For Thursday, work on
journal assignment 6.
As we've discussed in an earlier class, there are many models of
programming (i.e., describing algorithms). Java (the language
we'll be using this semester) works with two such models: object-oriented
programming and imperative (also known as procedural) programming.
- Computer Scientists have developed a number of strategies for looking
at algorithm and data design, including
- procedural / imperative
- object-oriented
- functional
- logical
- declarative
- While individual definitions of each category may differ, most
definitions have some similarities.
- As you might guess, object-oriented languages concern themselves with
objects.
- You might have a sense of what a "real world" object is.
- In computerese, an object is a collection of information
(sometimes called "attributes") and operations that
the object can perform (called "methods").
- We often categorize objects into classes. A class specifies
common aspects of a set of objects. These aspects are often
generic attributes and specifications of methods (E.g., "all objects in
this class have a color, a size, and can draw themselves.")
- When creating new objects, we must specify the class to
which they belong.
- Most object-oriented languages support inheritance, in which
classes can be based upon other classes. For example, we
might say that trees are a subclass of plants.
- A subclass "inherits" the attributes and methods of its
parent class.
- Many object-oriented languages are event-driven. That is,
you can specify "when this event happens, call this method
of this object."
- A key aspect of object-orientation (and good program design in any
language) is information hiding. In general, objects
should know what other objects do, but not how they
do it.
- For example, you might know that a
Menu object can
draw menus on the screen, but it's not important to you how it
does it.
- In a procedural (or imperative) language, programs
are written as collections of what I term "basic actions" which
are then sequenced with "control structures".
- Basic actions are typically of one of the following forms:
- Get a value from memory
- Store a value in memory
- Compute a value from other values
- Read a value from an input device
- Write a value to an output device
- We usually reserve names for values and assign types to those
values.
- Such reserved names are called variables.
- There are a variety of control structures in imperative languages.
The basic ones include
- Sequencing
- Conditionals
- Loops
- Function calls
- In imperative languages, instructions are often presented in sequences,
and the instructions are then executed one-by-one.
- Conditionals select one of a number of options, based on a selector
value. The most common conditional is the
if
statement. In most imperative languages, if
selects between two alternative sequences of actions (or decides
whether or
not to execute a single alternative) based on the value of
a boolean expression.
- Loops repeat a sequence of actions some number of times. The
number of times can be fixed, or it can depend on a condition
that is regularly checked.
- Function calls are used to describe and use sub-algorithms.
When you call another function, the body of that function is executed.
- Java is an object-oriented language developed by Sun Microsystems.
- It was ostensibly developed for developing large programs.
- Many of these programs were to run on autonomous systems, like
toasters.
- With the popularization of the World-Wide Web, it has also
become popular as a language for creating web
applications.
- It is also a reasonably good introductory language.
- Java looks a lot like C and C++, two popular languages. It has many
differences from the two.
- It is object-oriented (which C is not but C++ is)
- It has a different view of object-orientation than C++.
- It provides automatic memory management (yay!).
- We'll see some others as time goes on (some even in the
next topic).
- Sun says that Java is "a simple, object-oriented, distributed,
interpreted, robust, secure, architecture neutral,
portable, high-performance, multithreaded, and dynamic
language."
- Simple. Compared to many modern languages, there is not
too much to the core of Java. However, a great
deal of functionality exists in the standard libraries,
and you will eventually need to learn those libraries.
- Object-oriented. Java focuses on the creation of classes
and objects. Every Java program begins with a central
class or object.
- Distributed. Java permits you to place different portions
of a program on different servers, to segment
processing between servers, and so on and so forth.
- Interpreted. Although a compiler translates Java code
to another format (byte codes), an interpreter is
required to execute or further translate the byte
codes.
- Robust. Java provides a number of facilities for
ensuring that things don't go wrong or that,
when they do, the programmer provides for
problems.
- Secure. The Java interpreter will often limit what a
program can do (from accessing the file system to
reading memory outside of a valid range). We often
say that Java gives each program a sandbox
to play in.
- Architecture neutral. A Java program will run
identically (except for speed) on every platform.
Unlike most languages, which can have a different implementation
on every platform, Java has specific requirements for
essentially every part of the language (from the
amount of storage used for various data types to the
algorithms used for numeric computation).
- Portable. See the sections above on architecture neutral and
distributed.
- Multithreaded. A program can have separate threads of
computation, and it is relatively easy to manage
those threads.
- Dynamic. New objects can easily be created, and new
classes can be loaded as the program runs.