Most Sample Code from "Java How to Program, Fifth Edition" from Deitel and Deitel.
Packages
The following is taken from pages 228 and 229 (figure 6.6) of Deitel's book:
"As we have seen, Java contains many predefined classes that are grouped into
categories of related classes called packages.
Together, we refer to these packages as the Java Application Programming
Interface (Java API), or the Java class library.
...import declarations specify the classes required to compile a Java program.
For example, a program includes the declaration
import javax.swing.JApplet;
to specify that the program uses class JApplet from the javax.swing package."
Figure 6.6 from Java How to Program (5th Edition) has some of the packages in the java API:
Creating our own packages
Use keyword "package" and then the location of the package
We would want to create our own package if we have a class that is useful and we want to share with others.
page 381 of Deitel states "Every package name should start with your Internet domain name in reverse order. For example, for the Internet domain name deitel.com, the package name should begin with com.deitel"
Code is:
Time1.java
TimeTest1.java
Package Access
"If no access modifier (public, protected or private) is specified for a method or variable when it is declared in a class, the method or variable is considered to have package access." (pg 384, Deitel)
Code: PackageDataTest.java
Things to try:
Inheritance and Abstract Classes
The following is from the On-line Course: "Implementing Object-Oriented Programming with Java Technology", Module 2, Topic 5, pages 1 and 2
"An abstract class is a class with at least one abstract operation (an operation for which there is no implementation or code specified). Assume you need to implement classes for a circle, rectangle, and triangle. These shapes must have operations to set the area and determine the shape's perimeter. Each shape implements these operations differently, so you decide to create an abstract class, Shape."
"You can declare an abstract class by marking it with the abstract keyword. Likewise, in the declaration of a method without implementation, the keyword abstract is used. Any attempt to instantiate an abstract class results in a compiler error."
Code:
Point.java (Point extends Shape)
Circle.java(Circle extends Point)
Cylinder.java(Cylinder extends Circle)
AbstractInheritanceTest.java(Implementation file)
********Notice: In "Cylinder.java" code like the following:
public double getVolume() { return super.getArea() * getHeight(); }What does super mean? It means call the super class's "getArea()" function. The super class is another way of saying parent class. super.getArea() is calling Circle's getArea function.
Things to try:
Interface
"An interface declaration begins with the keyword "interface" and
contains a set of public abstract methods. Interfaces may also
contain public static final data. To use an interface, a class
must specify that "implements" the interface and must declare
each method in the interface with the signature specifiied in the
interface declaration. If the class does not implement any method
of the interface, the class is an abstract class and must be
declared abstract. Implementing an interface is like signing
a contract with the compiler that states, "I will declare all the
methods specified by the interface"
...An interface is typically used in place of an abstract class
when there is no default implementation to inherit .ie. no instance
variables and no default method implementations." (page 467, Deitel)
You can turn the abstract Shape class into an interface by making the following changes:
public interface Shape { public double getArea(); public double getVolume(); public String getName(); }
Notice: the functions are just prototypes; they don't do anything. Anything that implements Shape will have to specify the details of what these functions do
public class Point extends Object implements Shape
public double getArea() { return 0.0; } public double getVolume() { return 0.0; }
Review of Applets
Code:
SquareIntegers.java
SquareIntegers.html
*applet needs init, start, or paint function (no main)
*SquareIntegers extends JApplet
"The on-screen display area for an object of class JApplet has a
content pane--an object of class Container from the java.awt
package--to which the GUI components must be attached so that they
can be displayed at execution time.
"...when the applet executes, all GUI components attached to the
applet's content pane are displayed" (page 222, Deitel)
Notice the "getContentPane()" and the syntax to "add" GUI components to the applet's content pane.
See below to add more GUI components:
Adding GUI (Graphical User Interface) Components and Making them Interactive
Follow these steps to add a button and a textfield:
JTextField myField; JButton myButton;
container.setLayout (new FlowLayout());
This lays things out from left to right
myField = new JTextField (10); myField.setEditable(true); container.add(myField); myField.setText("Testing"); myButton=new JButton("My Button"); container.add(myButton);
import java.awt.event.*;
public class SquareIntegers extends JApplet implements ActionListener
public void actionPerformed (ActionEvent actionEvent) { //If the button is clicked if (actionEvent.getSource()==myButton) { showStatus ("You hit the button"); } //If you press Enter in the textfield else if (actionEvent.getSource()==myField) { showStatus ("You changed the field"); } }
myField.addActionListener(this); ... myButton.addActionListener(this);
The code:
SquareIntegers2.java
SquareIntegers2.html