Introduction to Object-Oriented/Internet Programming with Java III
The objective of this course is to help you to understand some fundamental basic
concepts behind the Java technology. The audience is assumed to have little or no
prior exposure to Java and Object-Oriented programming. Here we will cover the basics
of the Java technology. The topics will help you to understand how to use Java to
create, access, and support Java applications and applets. We will also discuss the
portability features of Java and how they are changing the way Web users access applications
at the desktop level.
In the next few sessions, we will:
- Discover why Java provides the answer to so many of today's computing problems.
- Introduce object-oriented programming concepts and how they apply to Java programming.
- Learn how Java applets are changing the way that client-server architectures
are implemented.
- Understand how the Java Virtual Machine and "Just-In-Time" technology
offer considerable advantages over the long accepted C++ development and run-time
environments.
- Peruse the Java application programming interface (API) for all of the packages
offered for applet and application programming, using the Java Development Kit (JDK)
1.1.
- Introduce the new Java 2 (formerly JDK 1.2) platform.
- Implement a completely platform independent Java application which takes advantage
of graphical user interface (GUI) components that operate on any Java supported platform,
including UNIX, Windows95, WindowNT, and MacOS.
Topics:
From Last Time...
Extending A Class (Inheritance)
- The extends keyword designates that the current class is a sub-class of the given
class
- extends <==> sub-class <==> inherits
- example: class BarChart extends DataChart{};
- DataChart is the super-class for BarChart
- BarChart is a sub-class of DataChart
- A class has exactly one super-class
- All non-private variables and methods are inherited
Method Overriding
- Description
- method has the same name and argument types as one defined in the
super-class
- the sub-class method is executed instead of the super-class method
- the super variable can be used to invoke the super class
- Example:
Method Overloading
- Description
- method has the same name, but different arguments than
- method in current class
- method in super-class
- the appropriate method is invoked based on arguments supplied by invoking method
- very useful for "legacy" interfaces or defining default values
- Example:
Method Overloading Example
Fundamentals
- Object class
- obj2 = obj1.clone() - returns exact copy of current object
- obj1.equals(obj2) - returns true if contents of current object obj1 equals that
of the given object obj2
- obj1.copy(obj2) - copies the contents of obj2 into obj1
- obj.toString() - returns String object representing contents
- System class
- System.out -> stdout PrintStream object
- Syste.err -> stderr PrintStream object
- System.in -> stdin InputStream object
- System.exit(int) -> exit the Virtual Machine
More Fundamentals
- String class
- length() -returns length of string
- case-conversion methods
- indexing and region matching methods
- substrings, replacing and trimming methods
- Special syntax rules:
- String str1 = "Hello World";
- String str1 = "Hello" + "World";
- String str2 = str1.concat("!!!");
- String str2 = "Hello".concat(" World")+"!!!";
- System.out.println(str1 + "Hello"+"World"+56.78);
Application vs. Applet
- Application
- Java class(es) intended to be executed in a standalone manner
- main() method must be defined somewhere
- OS command line parameters are passed to main() method
- Applet
- Java class(es) intended to be executed from within a Web browser
- main() method is not needed - init() defined instead
- <applet> tag used in HTML to invoke class and define parameters
- applet can read parameters like environment variables
- access to browser environment is provided -e.g., windows and menus
Hello World Applet
- HelloWorld Class extends the Applet class
- all public or protected instance variables are inherited
- all public or protected methods can be over-ridden
- init() method is called when the applet is initially executed by the browser
(e.g., applet host)
- paint() method is called whenever the applet display area needs to be
refreshed
- nothing is displayed before the first call to paint()
- start() and stop() methods are called when the applet thread should
begin and end execution
- destroy() method is called just before the applet dies
- The import directive is used to tell compiler to look in the given package
to resolve variable and method references
- Applet <===> java.Applet.Applet
- println() <=X=> System.out.println()
- The standard PrintStream objects, System.out and System.err
are mapped to
- stdout and stderr in the appletviewer
- "Java Console" in Netscape Navigator
Exercise
- Edit, compile and execute the HelloWorld Applet
- Edit, compile and execute the HelloWorld Application
- Modify the HelloWorld Application to echo its command line arguments to stdout
The HelloWorldFrame application
At the last session, we did a study of the HelloWorldFrame Java application. We will
now start modifying that program to incorporate new elements. The resulting program
will do the following:
The Java application will display a button with "Hello World" and
when the button is pushed an odd number of times, the button will display "I
am odd" and the same message is printed to the console. When the button is pressed
an even number of times, the button will display "I am even" and the same
message is displayed to the console.
Here a set of code fragments will be supplied, and you will put them together and
create the program that would accomplish the above tasks.
Resources
One of the main source of Java resources is the Sun Java website:
http://www.javasoft.com/
Detail documentations can be found at:
http://www.javasoft.com/docs/index.html
Also see http://www.oreilly.com/catalog/books/javanut2/
for Java examples available for downloading as noted in the Java in a Nutshell reference.
Several good books:
Java in a Nutshell by David Flanagan, from O'Reilly.
Java - How to Program by Deitel & Deitel, from Prentice Hall.
Inside Java by Siyan & Weaver, from New Riders Publishing.
The Java Series, from Addison-Wesley.
Paul Lee
update: 3/29/99