last updated 2/13/07
SDT's have two characteristics:
Java comes with a few types built in (sometimes called primitive types)
short, for example, is a integer data type. The set of values (domain) is well defined: -32768 to 32767 (short version of int ) and the operations +,-*, /, <, ,=, etc. are well defined.
| Primitive | Wrapper class | Domain approximations |
|---|---|---|
| int | Integer | +/- 2 billion |
| boolean | Boolean | TRUE/FALSE |
| float | Float | +/-10^(+/-34) |
| double | Double | +/-10^(+/-304) |
| short | Short | -32768 to 32767 |
The Java class enforces an encapsulation structure. An ADT (abstract data type) is easily defined
public class Password { private String passStr; public void GetPassword(); public boolean ValidUser(); };
The public components make up the interface (of functions, maybe some constants and variables) available for use by the client programmer.
The private components are necessary for successful use by the compiler, but its components (variables, maybe some functions) are inaccessible by the client programmer.
| Modifier |
Visibility |
| public | Within the class, subclasses in the same package, subclasses in other packages, uses everywhere |
| private | Within the class |
| protected | Within the class, subclasses in the same package, subclasses in other packages |
| package | Within the class, subclasses in the same package |
The Java Class Library-A class library that includes hundreds of useful classes.
Build your own-You create the needed class, possibly using pre-existing classes in the process.
Off the shelf-Software components, such as classes or packages of classes, which are obtained from third party sources.
|
package |
description |
|---|---|
|
java.awt |
(Abstract Windowing Toolkit) Contains tools for creating user interfaces, graphics, and images. |
|
java.awt.event |
Provides interfaces and classes for handling the different types of events created by AWT components. |
|
java.io |
System input and output through data streams, serialization, and the file system |
|
java.lang |
Provides basic classes for use in creating Java programs. Wrappers are found here. |
|
java.math |
Provides basic classes for performing math operations. |
|
java.text |
Provides classes and interfaces for handling text, dates, numbers, and messages. |
|
java.util |
Contains the collections framework, legacy collections classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (string tokenizer, random number generations and bit array manipulation). |
|
java.util.jar |
Provides classes for reading and writing the JAR (Java ARchive file format, which is base on the standard ZIP file format. |
Here is the link to the official Sun API (application programming interface) documentation
The System Class-To obtain current system properties
The Random Class-to generate a list of random numbers
The DecimalFormat Class-to format numbers for output
Wrappers-to wrap a primitive valued variable in an object's structure
The Throwable and Exception Classes-An exception is associated with an unusual, often unpredictable event, detectable by software or hardware, that requires special processing.
So there is a physical size and a logical size in an object of ArrayList
| Method signature |
Operation |
|---|---|
| ArrayList() |
Construct an empty array list of 10 elements |
| ArrayList(int size) |
Construct an empty array list of size |
| void add(int p,Object t) |
Insert Object t at position p, shift all elements down to make room (expensive) |
| void add(Object t) |
Insert Object t at the end of the list (cheap) |
| int ensureCapacity(int c) |
Increases the capacity of the array list to at least the capacity c, if less than c |
| Object get(int p) |
Return element at position p |
| boolean isEmpty() |
True if array list is empty, false otherwise |
| Object remove(int p) |
Remove element at position p and return the removed element (expensive) |
| int size() |
Return the current logical size of the array |
| void trimToSize() |
Set physical size of array to logical size |
Go to Sun documentation for more description and full set of methods. It will not tell you what operations may be expensive to use.
Shallow Copy -- An operation that copies a source class instance to a destination class instance, simply copying all references so that the destination instance contains duplicate references to values that are also referred to by the source. Aliasing is being used.
Deep Copy -- An operation that copies one class instance to another, using observer methods as necessary to eliminate nested references and copy only the primitive types that they refer to. The result is that the two instances do not contain any duplicate references.

One system unit raises or throws an exception, and another unit catches the exception which processes (handles) it.
Throw an exception-Interrupt the normal processing of a program to raise an exception that must be handled or rethrown by surrounding block or code. You throw an exception when you've detected a problem that the implementation shouldn't have to resolve or cannot easily resolve. A throw is usually found in a conditional statement, i.e., only throw an exception if a particular condition holds.
Catch an exception-Code that is executed to handle a thrown exception is said to "catch the exception". Catches are part of the code that may anticipate problems in another class object that the object may throw back.
Unchecked Exception This is an exception of the RunTimeException class, it does not have to be explicitly handled by the method within which it might be raised.
try
{
month = Integer.parseInt(dataFile.readLine());
day = Integer.parseInt(dataFile.readLine());
year = Integer.parseInt(dataFile.readLine());
}
catch (Exception readExcp)
{
outFile.println("There was trouble reading numeric form of date");
outFile.println("Exception: "+readExcp.getMessage());
System.exit();
}
We caught all possible exceptions above. We don't know if there was problems with the input file or if the data was malformed. We can catch different, specific exceptions as in:
try
{
month = Integer.parseInt(dataFile.readLine());
day = Integer.parseInt(dataFile.readLine());
year = Integer.parseInt(dataFile.readLine());
}
catch (IOException readExcp)
{
outFile.println("There was trouble reading from input file");
outFile.println("Exception: "+readExcp.getMessage());
System.exit();
}
catch (NumberFormatException fmtExcp)
{
outFile.println("There was trouble reading numeric form of date");
outFile.println("Exception: "+fmtExcp.getMessage());
System.exit();
}
catch (Exception genExcp) //this general catch must follow specific
{
outFile.println("There are unresolved problems retrieving date");
outFile.println("Exception: "+genExcp.getMessage());
System.exit();
}
Exceptions are simply objects. You throw and catch an object; it's best that object is from the Exception class hierarchy within which you can define your own, as in:
public class DateOutOfBoundsException extends Exception
{
public DateOutOfBoundsException()
{
super();
}
public DateOutOfBoundsException(String message)
{
super(message);
}
}
Typically you define a default constructor that simply invokes its parent default constructor, as well as, one that includes a user's message.
public Date(int newMonth, int newDay, int newYear) throws DateOutOFBoundsException
{
if((newMonth <=0) || (newMonth > 12))
throw new DateOutOfBoundsException("Month must be 1<= m <=12");
else
month = newMonth;
...
if (newYear < MINYEAR)
throw new DateOutOfBoundsException("Year "+newYear+" is too early");
else
year = newYear;
}
public class UseDates
{
public static void main (String[] args) throws DateOutOfBoundsException
{
...
Date aDate = new Date(28,2,2004);
public class Usedates
[
public static void main(String[] args)
{
Date theDate;
boolean DateOK = false;
while (!DateOK){
//M,D,Y are set...
try
{
theDate = new Date(M,D,Y);
DateIO = true;
}
catch(DateOutOFBoundsException OB)
{
System.out.println(OB.getMessage());
}
}
}
Determine the general purpose.
List the specific types of operations the application program performs.
Identify a set of public methods to be provided by the ADT class that allow the application program to perform the desired operations.
Identify other public methods which help make the ADT generally usable.
Identify potential error situations and classify into
Define the needed exception classes.
Decide how to structure the data.
Decide on a protection level for the identified data.
Identify private structures and methods.