Introduction To Object-Oriented Programming, Sample Programs, Set 1
Introduction To Object-Oriented Programming, Sample Programs, Set 1
Programs, Set 1
Published: November 29, 2001
Revised: June 1, 2005
By Richard G. Baldwin
• Preface
• Sample Programs
• About the Author
Preface
This is the first in a miniseries of lessons designed specifically to help the students in my
"Introduction to Object-Oriented Programming" course study for their exams. However,
others may find the lesson useful as well.
The lesson consists of a set of simple programs, each designed to illustrate one or more
important Java OOP concepts. The concepts involved are identified in the comments at
the beginning of each program.
The programs are designed to illustrate the code without providing a detailed discussion
of the code. You are referred to the other lessons in my online Java tutorials for detailed
discussions of the OOP concepts illustrated by these programs.
Sample Programs
Program Samp002.java
/*File Samp002
Copyright 2001, R.G.Baldwin
Rev 11/28/01
Illustrates:
Write Java application
The main method
Display String literal on console
**************************************/
class Samp002{
public static void main(
String[] args){
System.out.println("Hello World");
}//end main
}//end class Samp002
Program Samp004.java
/*File Samp004
Copyright 2001, R.G.Baldwin
Rev 11/28/01
Illustrates:
Instantiate object of existing class
Invoke method on object
**************************************/
import java.util.*;
class Samp004{
public static void main(
String[] args){
//Instantiate object of Date class
Date refVar = new Date();
System.out.println(refVar);
long primVar = refVar.getTime();
System.out.println(primVar);
}//end main
}//end class Samp004
Program Samp006.java
/*File Samp006
Copyright 2001, R.G.Baldwin
Rev 11/28/01
Illustrates:
Instantiate anonymous Date object
Invoke method on anonymous Date
object to get long value
Pass long value to constructor
for Random class
Invoke method on object of
Random class to get random
int value
Display random int value
472076870
**************************************/
import java.util.*;
class Samp006{
public static void main(
String[] args){
Random refVar2 =
new Random(new Date().getTime());
System.out.println(
refVar2.nextInt());
}//end main
}//end class Samp006
Program Samp008.java
/*File Samp008
Copyright 2001, R.G.Baldwin
Rev 06/01/05
Illustrates:
Instantiate anonymous Date object
Invoke method on anonymous Date
object to get long value
Pass long value to constructor
for Random class
Invoke method on object of
Random class to get random
int value
Use cast operator to convert random
int value to type byte
Display random byte value
The output consists of a single random
value similar to the following:
110
**************************************/
import java.util.*;
class Samp008{
public static void main(
String[] args){
Random refVar2 =
new Random(new Date().getTime());
System.out.println(
(byte)refVar2.nextInt());
}//end main
}//end class Samp008
Program Samp010.java
/*File Samp010
Copyright 2001, R.G.Baldwin
Rev 11/28/01
Illustrates:
Array objects
Conversion from int to double
during division of int value
by double value
0
1
2.0
3
**************************************/
class Samp010{
public static void main(
String[] args){
int[] arrayRef = new int[4];
for(int cnt=0;cnt<arrayRef.length;
cnt++){
arrayRef[cnt] = cnt;
}//end for loop
for(int cnt=0;cnt<arrayRef.length;
cnt++){
if(cnt == 2)
System.out.println(
arrayRef[cnt]/1.0 + " ");
else
System.out.println(
arrayRef[cnt] + " ");
}//end for loop
}//end main
}//end class Samp010
Program Samp012.java
/*File Samp012
Copyright 2001, R.G.Baldwin
Rev 06/01/05
Illustrates:
Reference variables
Defining new classes
Instantiating objects of new
classes
Overriding toString method in one
new class, but not the other
Invoking toString method on objects
of both new classes and displaying
the results
Richard Baldwin
Samp012ClassB@273d3c
**************************************/
import java.util.*;
class Samp012{
public static void main(
String[] args){
Samp012ClassA refVar1 =
new Samp012ClassA();
String stringVar =
refVar1.toString();
System.out.println(stringVar);
Samp012ClassB refVar2 =
new Samp012ClassB();
stringVar = refVar2.toString();
System.out.println(stringVar);
}//end main
}//end class Samp012
//===================================//
class Samp012ClassA{
//overridden toString method
public String toString(){
return "Richard Baldwin";
}//end overridden toString()
}//end class Samp012ClassA
//===================================//
class Samp012ClassB{
//no overridden toString method
}//end class Samp012ClassB
Program Samp014.java
/*File Samp014
Copyright 2001, R.G.Baldwin
Rev 11/28/01
Illustrates:
Defining a noarg constructor
Richard Baldwin
**************************************/
import java.util.*;
class Samp014{
public static void main(
String[] args){
new Samp014MyClass();
}//end main
}//end class Samp014
//===================================//
class Samp014MyClass{
Samp014MyClass(){//constructor
System.out.println(
"Richard Baldwin");
}//end constructor
Program Samp016.java
/*File Samp016
Copyright 2001, R.G.Baldwin
Rev 11/28/01
Illustrates:
Defining a parameterized constructor
Richard Baldwin
**************************************/
import java.util.*;
class Samp016{
public static void main(
String[] args){
new Samp016MyClass(
"Richard Baldwin");
}//end main
}//end class Samp016
//===================================//
class Samp016MyClass{
//parameterized constructor
Samp016MyClass(String dataIn){
System.out.println(dataIn);
}//end constructor
Program Samp018.java
/*File Samp018
Copyright 2001, R.G.Baldwin
Rev 11/28/01
Illustrates:
Class variables
Accessing class variable using
class name
Accessing class variable using
reference to object
Only one copy of class variable
exists regardless of number of
objects instantiated from the
class (including none)
The output consists of the following
six lines of text:
6
6
6
12
12
12
**************************************/
import java.util.*;
class Samp018{
public static void main(
String[] args){
Samp018MyClass.classVar = 6;
System.out.println(
Samp018MyClass.classVar);
Samp018MyClass refVar1 =
new Samp018MyClass();
System.out.println(
refVar1.classVar);
Samp018MyClass refVar2 =
new Samp018MyClass();
System.out.println(
refVar2.classVar);
refVar2.classVar = 12;
System.out.println(
Samp018MyClass.classVar);
System.out.println(
refVar1.classVar);
System.out.println(
refVar2.classVar);
}//end main
}//end class Samp018
//===================================//
class Samp018MyClass{
static int classVar;
}//end class Samp018MyClass
Program Samp020.java
/*File Samp020
Copyright 2001, R.G.Baldwin
Rev 11/28/01
Illustrates:
Instance variables
Cannot access instance variable using
class name
Accessing instance variable using
reference to object
Every object has its own copy of each
instance variable
6
12
10
20
**************************************/
import java.util.*;
class Samp020{
public static void main(
String[] args){
//Following statements produce
// compiler errors
// Samp020MyClass.instanceVar = 6;
// System.out.println(
// Samp020MyClass.instanceVar);
Samp020MyClass refVar1 =
new Samp020MyClass();
refVar1.instanceVar = 6;
System.out.println(
refVar1.instanceVar);
Samp020MyClass refVar2 =
new Samp020MyClass();
refVar2.instanceVar = 12;
System.out.println(
refVar2.instanceVar);
refVar1.instanceVar = 10;
refVar2.instanceVar = 20;
System.out.println(
refVar1.instanceVar);
System.out.println(
refVar2.instanceVar);
}//end main
}//end class Samp020
//===================================//
class Samp020MyClass{
int instanceVar;
}//end class Samp020MyClass
Program Samp022.java
/*File Samp022
Copyright 2001, R.G.Baldwin
Rev 11/28/01
Illustrates:
Overloaded class methods
6
2.727272727272727
6
2.727272727272727
**************************************/
import java.util.*;
class Samp022{
public static void main(
String[] args){
int intVar = 6;
double doubleVar = 6/2.2;
System.out.println(intVar);
System.out.println(doubleVar);
Samp022MyClass.store(intVar);
System.out.println(
Samp022MyClass.fetchIntData());
Samp022MyClass.store(doubleVar);
System.out.println(
Samp022MyClass.fetchDoubData());
}//end main
}//end class Samp022
//===================================//
class Samp022MyClass{
private static int classIntVar;
private static double classDoubleVar;
//class method
static int fetchIntData(){
return classIntVar;
}//end fetchIntData
//class method
static double fetchDoubData(){
return classDoubleVar;
}//end fetchDoubData
}//end class Samp022MyClass
Program Samp024.java
/*File Samp024
Copyright 2001, R.G.Baldwin
Rev 11/28/01
Illustrates:
Overloaded instance methods
3
8
**************************************/
import java.util.*;
class Samp024{
public static void main(
String[] args){
Samp024MyClass refVar1 =
new Samp024MyClass();
refVar1.setData(3);
System.out.println(
refVar1.getData());
refVar1.setData(new Integer(8));
System.out.println(
refVar1.getData());
}//end main
}//end class Samp024
//===================================//
class Samp024MyClass{
private int instanceVar;
//overloaded instance method
void setData(int dataIn){
instanceVar = dataIn;
}//end setData()
int getData(){
return instanceVar;
}//end getData()
}//end class Samp024MyClass
Program Samp030.java
/*File Samp030
Copyright 2001, R.G.Baldwin
Rev 11/28/01
Illustrates:
Reference variables
Class variables
Instance variables
Noarg constructor
Class methods
Instance methods
Overridden toString method
Overloaded class methods
Overloaded instance methods
Instantiating objects
Display on console
Invoking methods on anonymous objects
Array objects
Casting
Conversion from int to double
during division
Invoking class methods using class
name
Invoking instance methods using
reference to object
Samp030
Richard
Baldwin
-78 -81.0 -51 -61
-78 -81.0 -51 -61
**************************************/
import java.util.*;
class Samp030{
public static void main(
String[] args){
Samp030MyClass refVar1 =
new Samp030MyClass();
System.out.println(refVar1);
Random refVar2 =
new Random(new Date().getTime());
int[] arrayRef = new int[4];
for(int cnt=0;cnt<arrayRef.length;
cnt++){
arrayRef[cnt] =
(byte)refVar2.nextInt();
if(cnt==1)
System.out.print(
arrayRef[cnt]/1.0 + " ");
else
System.out.print(
arrayRef[cnt] + " ");
}//end for loop
System.out.println();
Samp030MyClass.store(arrayRef[0]);
System.out.print(
Samp030MyClass.fetchIntData()
+ " ");
Samp030MyClass.store(
arrayRef[1]/1.0);
System.out.print(
Samp030MyClass.fetchDoubleData()
+ " ");
refVar1.setData(arrayRef[2]);
System.out.print(
refVar1.getData() + " ");
refVar1.setData(
new Integer(arrayRef[3]));
System.out.println(
refVar1.getData());
}//end main
}//end class Samp030
//===================================//
class Samp030MyClass{
private static int classIntVar;
private static double classDoubleVar;
private int instanceVar;
Samp030MyClass(){//constructor
System.out.println("Samp030");
System.out.println("Richard");
}//end constructor
//overloaded method
static void store(int dataIn){
classIntVar = dataIn;
}//end store
//overloaded method
static void store(double dataIn){
classDoubleVar = dataIn;
}//end store
//overloaded method
void setData(int dataIn){
instanceVar = dataIn;
}//end setData()
//overloaded method
void setData(Integer dataIn){
instanceVar = dataIn.intValue();
}//end setData()
int getData(){
return instanceVar;
}//end getData()
}//end class Samp030MyClass
Program Samp032.java
/*File Samp032
Copyright 2001, R.G.Baldwin
Rev 11/29/01
Illustrates:
Use of absolute value method of Math
class
Use of modulus operator to
distinguish between even and odd
values
1728897982
Value is even
1728897983
Value is odd
**************************************/
import java.util.*;
class Samp032{
public static void main(
String[] args){
Random refToRNGen =
new Random(new Date().getTime());
int var = refToRNGen.nextInt();
System.out.println(Math.abs(var));
if(Math.abs(var)%2 == 0){
System.out.println(
"Value is even");
}else{
System.out.println(
"Value is odd");
}//end else
System.out.println(Math.abs(var));
if(Math.abs(var)%2 == 0){
System.out.println(
"Value is even");
}else{
System.out.println(
"Value is odd");
}//end else
}//end main
}//end class Samp032
Program Samp034.java
/*File Samp034
Copyright 2001, R.G.Baldwin
Rev 11/29/01
Illustrates:
Behavior of default equals method
The this keyword
true
false
**************************************/
import java.util.*;
class Samp034{
public static void main(
String[] args){
Samp034Class refVarA =
new Samp034Class(5);
Samp034Class refVarB =
new Samp034Class(5);
}//end main
}//end class Samp034
//===================================//
class Samp034Class{
private int data;
Samp034Class(int data){//constructor
this.data = data;
}//end constructor
Program Samp036.java
/*File Samp036
Copyright 2001, R.G.Baldwin
Rev 11/29/01
Illustrates:
Simple overridden equals method for
String data
The output consists of the following
three lines of text:
true
true
false
**************************************/
import java.util.*;
class Samp036{
public static void main(
String[] args){
Samp036Class refVarA =
new Samp036Class("Joe");
Samp036Class refVarB =
new Samp036Class("Joe");
Samp036Class refVarC =
new Samp036Class("Tom");
System.out.println(
refVarA.equals(refVarA) + " ");
System.out.println(
refVarA.equals(refVarB));
System.out.println(
refVarA.equals(refVarC));
}//end main
}//end class Samp036
//===================================//
class Samp036Class{
private String data;
//constructor
Samp036Class(String data){
this.data = data;
}//end constructor
Program Samp038.java
/*File Samp038
Copyright 2001, R.G.Baldwin
Rev 11/29/01
1 Joe
2 Sue
3 Tom
**************************************/
import java.util.*;
class Samp038{
public static void main(
String[] args){
Samp038Class refVarA =
new Samp038Class("Joe");
Samp038Class refVarB =
new Samp038Class("Sue");
Samp038Class refVarC =
new Samp038Class("Tom");
}//end main
}//end class Samp038
//===================================//
class Samp038Class{
private String data;
private static int count = 0;
//constructor
Samp038Class(String data){
this.data = data;
System.out.println(
++count + " " + data);
}//end constructor
Program Samp040.java
/*File Samp040
Copyright 2001, R.G.Baldwin
Rev 11/28/01
Illustrates:
Overridden equals() method
Class variable for counting object
instantiations
Samp040
Richard Baldwin
false true
**************************************/
import java.util.*;
class Samp040{
public static void main(
String[] args){
Samp040Class refVarA =
new Samp040Class();
Samp040Class refVarB =
new Samp040Class();
Random refToRNGen =
new Random(new Date().getTime());
if(Math.abs(var4)%2 != 0){
//value is odd
refVarA.setData(var3);
refVarB.setData(var3);
}else{
//value is even
refVarA.setData(var3);
refVarB.setData(var3 + var4);
}//end else
System.out.println(
refVarB.equals(refVarA));
}//end main
}//end class Samp040
//===================================//
class Samp040Class{
private static int objCnt = 0;
private int data;
Samp040Class(){//constructor
if(objCnt++ == 0){
//Display name only for first
// object instantiated
System.out.println("Samp040");
System.out.println(
"Richard Baldwin");}
}//end constructor
Program Samp048.java
/*File Samp048
Copyright 2001, R.G.Baldwin
Rev 11/29/01
Illustrates:
Instantiating an abstract class is
not allowed.
Extending an abstract class.
Parameterized constructor.
Storing a ref to a subclass object in
a ref var of a superclass type.
Requirement to downcast a superclass
ref variable to access a method in
a subclass object.
Overridden toString method.
The this keyword.
**************************************/
import java.util.*;
//constructor
Samp048Class(int data){
System.out.println("Samp048");
System.out.println("Richard");
this.data = data;
}//end constructor
Program Samp050.java
/*File Samp050
Copyright 2001, R.G.Baldwin
Rev 11/29/01
Illustrates:
Extending an abstract class.
Parameterized constructor.
Defining an abstract method in the
superclass and overriding it in a
subclass to make it accessible
without a requirement to downcast
to the subclass type.
Overridden toString method.
Samp050
Richard
Baldwin
23
23
**************************************/
import java.util.*;
//constructor
Samp050Class(int inData){
System.out.println("Samp050");
System.out.println("Richard");
data = inData;
}//end constructor
Program Samp056.java
/*File Samp056
Copyright 2001, R.G.Baldwin
Rev 11/29/01
Illustrates:
Downcasting object ref in order to
access a method belonging to the
object.
Samp056
Hello from Baldwin
**************************************/
import java.util.*;
class Samp056{
public static void main(
String[] args){
}//end main
}//end class Samp056
//===================================//
Samp056Class(String data){
System.out.println("Samp056");
this.data = data;
}//end constructor
Program Samp058.java
/*File Samp058
Copyright 2001, R.G.Baldwin
Rev 11/29/01
Illustrates:
Instantiating anonymous object, and
invoking method on that object.
**************************************/
import java.util.*;
class Samp058{
public static void main(
String[] args){
//Instantiate anonymous object and
// invoke method on that object.
new Samp058Class("Baldwin").
sayHello();
}//end main
}//end class Samp058
//===================================//
class Samp058Class{
private String data;
Samp058Class(String data){
System.out.println("Samp058");
this.data = data;
}//end constructor
Program Samp060.java
/*File Samp060
Copyright 2001, R.G.Baldwin
Rev 11/29/01
Illustrates:
Instantiating anonymous object, and
invoking method on that object.
Extending a class,
Passing subclass object reference as
superclass type.
Downcasting incoming object ref to
access method.
Samp060
Richard
Baldwin
78
78
**************************************/
import java.util.*;
class Samp060{
public static void main(
String[] args){
System.out.println(rNum);
}//end main
}//end class Samp060
//===================================//
Samp060ClassA(int data){
System.out.println("Samp060");
System.out.println("Richard");
this.data = data;
}//end constructor
Samp060ClassB(){
System.out.println("Baldwin");
}//end constructor
Program Samp066.java
/*File Samp066
Copyright 2001, R.G.Baldwin
Rev 11/29/01
Illustrates:
Create and populate an array object
without use of keyword new.
1 2 3
Richard Baldwin
**************************************/
class Samp066{
public static void main(
String[] args){
}//end main
}//end class Samp066
//===================================//
class Samp066Class{
private String data;
Samp066Class(String data){
this.data = data;
}//end constructor
Program Samp068.java
/*File Samp068
Copyright 2001, R.G.Baldwin
Rev 11/29/01
Illustrates:
Create and populate an array object
of type Object without use of
keyword new.
Downcast object references when
fetched from array to access
method.
**************************************/
class Samp068{
public static void main(
String[] args){
}//end main
}//end class Samp068
//===================================//
class Samp068Class{
private String data;
Samp068Class(String data){
this.data = data;
}//end constructor
Program Samp070.java
/*File Samp070
Copyright 2001, R.G.Baldwin
Rev 11/29/01
Illustrates:
Create one-element array of type
Object without use of keyword new.
Populate array element with reference
to object when array is created.
Passing object reference as type
Object.
Downcasting incoming object ref to
access method.
Samp070
Richard
Baldwin
78
78
**************************************/
import java.util.*;
class Samp070{
public static void main(
String[] args){
Random rGen =
new Random(new Date().getTime());
int rNum = (byte)rGen.nextInt();
System.out.println(rNum);
}//end main
}//end class Samp070
//===================================//
Samp070ClassA(int data){
System.out.println("Samp070");
System.out.println("Richard");
this.data = data;
}//end constructor
class Samp070ClassB{
Samp070ClassB(){
System.out.println("Baldwin");
}//end constructor
Richard Baldwin is a college professor (at Austin Community College in Austin, TX)
and private consultant whose primary focus is a combination of Java and XML. In
addition to the many platform-independent benefits of Java applications, he believes that
a combination of Java and XML will become the primary driving force in the delivery of
structured information on the Web.
Richard holds an MSEE degree from Southern Methodist University and has many years
of experience in the application of computer technology to real-world problems.
-end-
• Preface
• Sample Programs
• About the Author
Preface
This is the second in a miniseries of lessons designed specifically to help the students in
my "Introduction to Object-Oriented Programming" course study for their exams.
However, others may find the lesson useful as well.
The lesson consists of a set of simple programs, each designed to illustrate one or more
important Java OOP concepts. The concepts involved are identified in the comments at
the beginning of each program.
The programs are designed to illustrate the code without providing a detailed discussion
of the code. You are referred to the other lessons in my online Java tutorials for detailed
discussions of the OOP concepts illustrated by these programs.
Sample Programs
Program Samp076.java
/*File Samp076
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Interface definitions,
Implementing one or more interfaces
in class definition,
Defining interface methods in class
definition,
Casting from one interface type
to another.
Samp076
Richard
Baldwin
-64 -63 -62
-63 -63 -63
**************************************/
import java.util.*;
class Samp076{
public static void main(
String[] args){
Random rGen =
new Random(new Date().getTime());
int rNum = (byte)rGen.nextInt();
interface Samp076IntfcA{
public int getModifiedData();
}//end interface
//===================================//
interface Samp076IntfcB{
public int getData();
}//end interface
//===================================//
class Samp076ClassA
implements Samp076IntfcA,
Samp076IntfcB{
private int data;
Samp076ClassA(int data){
System.out.println("Samp076");
System.out.println("Richard");
this.data = data;
}//end constructor
class Samp076ClassB
implements Samp076IntfcA,
Samp076IntfcB{
private int data;
Samp076ClassB(int data){
System.out.println("Baldwin");
this.data = data;
}//end constructor
Program Samp078.java
/*File Samp078
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Use of the getClass method of the
Object class,
Use of the forName method of the
Class class,
Use of the getInterfaces method of
the Class class,
Use of the getSuperclass method of
the Class class,
Samp078
Richard Baldwin
74 74
Samp078ClassA
class java.lang.Object
interface Samp078IntfcA
interface Samp078IntfcB
java.awt.Button
class java.awt.Component
interface javax.accessibility.
Accessible
**************************************/
import java.util.*;
import java.awt.*;
class Samp078{
public static void main(
String[] args){
Random rGen =
new Random(new Date().getTime());
int rNum = (byte)rGen.nextInt();
}//end main
}//end class Samp078
//===================================//
interface Samp078IntfcA{
public void intfcMethod();
}//end interface
//===================================//
interface Samp078IntfcB{
public int getData();
}//end interface
//===================================//
class Samp078ClassA
implements Samp078IntfcA,
Samp078IntfcB{
private int data;
Samp078ClassA(int data){
System.out.println("Samp078");
System.out.println(
"Richard Baldwin");
this.data = data;
}//end constructor
Program Samp080.java
/*File Samp080
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Interface definitions,
Implementing one or more interfaces
in class definition,
Defining interface methods in class
definition,
Overridden toString method,
Use of the getClass method of the
Object class,
Use of the getInterfaces method of
the Class class
Samp080
Richard
Baldwin
-109 -108 -107
-108 -108 -108
-103 -103 -103
interface Samp080IntfcA
interface Samp080IntfcA
interface Samp080IntfcB
**************************************/
import java.util.*;
class Samp080{
public static void main(
String[] args){
Random rGen =
new Random(new Date().getTime());
int rNum = (byte)rGen.nextInt();
interface Samp080IntfcA{
public int getModifiedData();
public int getData();
}//end interface
//===================================//
interface Samp080IntfcB{
//this is a dummy interface
}//end interface
//===================================//
class Samp080ClassA
implements Samp080IntfcA{
private int data;
Samp080ClassA(int data){
System.out.println("Samp080");
System.out.println("Richard");
this.data = data;
}//end constructor
class Samp080ClassB
implements Samp080IntfcA,
Samp080IntfcB{
private int data;
Samp080ClassB(int data){
System.out.println("Baldwin");
this.data = data;
}//end constructor
Program Samp088.java
/*File Samp088
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Saving object ref as class type,
interface type, or type Object,
Casting ref stored as type Object
to class type or interface type
to access interface method,
Accessing overridden toString
method on references of all three
types without a requirement to
perform a cast.
intfcMethod output
intfcMethod output
Overridden toString output
Overridden toString output
Overridden toString output
**************************************/
import java.util.*;
class Samp088{
public static void main(
String[] args){
String strData = "";//for use later
Samp088Class var1 =
new Samp088Class();
Samp088Intfc var2 = var1;
Object var3 = var1;
var1.intfcMethod();
var2.intfcMethod();
//Following will not compile due
// to need for a cast
// strData = var3.intfcMethod();
strData = ((Samp088Intfc)var3).
intfcMethod();
System.out.println(strData);
interface Samp088Intfc{
public String intfcMethod();
}//end interface
//===================================//
class Samp088Class
implements Samp088Intfc{
Program Samp090.java
/*File Samp090
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Interface definitions,
Implementing an interface in class
definition,
Defining interface methods in class
definition,
Storing references to new objects in
elements of an array of type
Object,
Cast elements to interface type in
order to invoke methods,
Overridden toString method.
Samp090
Richard
Baldwin
-68 -67 -66
-67 -67 -67
-62 -62 -62
**************************************/
import java.util.*;
class Samp090{
public static void main(
String[] args){
Random rGen =
new Random(new Date().getTime());
int rNum = (byte)rGen.nextInt();
}//end main
}//end class Samp090
//===================================//
interface Samp090Intfc{
public int getModData();
public int getData();
}//end interface
//===================================//
class Samp090ClassA
implements Samp090Intfc{
private int data;
Samp090ClassA(int data){
System.out.println("Samp090");
System.out.println("Richard");
this.data = data;
}//end constructor
class Samp090ClassB
implements Samp090Intfc{
private int data;
Samp090ClassB(int data){
System.out.println("Baldwin");
this.data = data;
}//end constructor
Program Samp098.java
/*File Samp098
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Use of the Collection Framework, the
List interface, and the ArrayList
class. The ArrayList class implements
the List interface. The add method of
the List interface allows duplicates.
Samp098
Richard Baldwin
Harry is first
Harry Joe Bill Sue Tom Bill
Samp098
Richard Baldwin
Mike is first
Mike Bill Tom Mary Mike Alice
**************************************/
import java.util.*;
class Samp098{
public static void main(
String[] args){
Random rGen =
new Random(new Date().getTime());
int rNum = rGen.nextInt()%2;
Samp098Class(){//constructor
System.out.println("Samp098");
System.out.println(
"Richard Baldwin");
}//end constructor
}//end class Samp098Class
Program Samp100.java
/*File Samp100
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Use of the Collection Framework, the
Set interface, and the TreeSet class.
The TreeSet class implements the Set
interface. The add method of the Set
interface doesn't allow duplicates.
Samp100
Richard Baldwin
Bill is first
Bill Joe Sue Tom
Samp100
Richard Baldwin
Alice is first
Alice Bill Mary Mike
**************************************/
import java.util.*;
class Samp100{
public static void main(
String[] args){
Random rGen =
new Random(new Date().getTime());
int rNum = rGen.nextInt()%2;
Samp100Class(){//constructor
System.out.println("Samp100");
System.out.println(
"Richard Baldwin");
}//end constructor
}//end class Samp100Class
Program Samp106.java
/*File Samp106
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Use of the binarySearch method of the
Arrays class. This is a simple program
for students who dig deeply enough into
the Sun documentation to learn how to
use the binarySearch method of the
Arrays class. Otherwise, it will be a
fairly difficult problem.
Samp106
Richard Baldwin
Dick Harry Tom
Harry is at index 1
Bill is not in array
**************************************/
import java.util.*;
class Samp106{
public static void main(
String[] args){
System.out.println(
Samp106Class.displayName());
}//end main
class Samp106Class{
static String displayName(){
System.out.println("Samp106");
return "Richard Baldwin";
}//end displayName()
Program Samp108.java
/*File Samp108
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Use of the asList method of the Arrays
class. This is a simple program for
students who dig deeply enough into the
Sun documentation to learn how to use
the asList() method of the Arrays
class. Otherwise, it will be a very
difficult problem.
Samp108
Richard Baldwin
Tom Dick Harry
Tom TOM Dick DICK Harry HARRY
**************************************/
import java.util.*;
class Samp108{
public static void main(
String[] args){
System.out.println(
Samp108Class.displayName());
class Samp108Class{
static String displayName(){
System.out.println("Samp108");
return "Richard Baldwin";
}//end displayName()
Program Samp110.java
/*File Samp110
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Sorting strings in an array.
Bill is first
Samp110
Richard Baldwin
Bill Bill Joe Tom sue
Alice is first
Samp110
Richard Baldwin
Alice Bill Mike Mike mary
**************************************/
import java.util.*;
class Samp110{
public static void main(
String[] args){
Random rGen =
new Random(new Date().getTime());
int rNum = rGen.nextInt()%2;
Prob04Class.showYourName();
Prob04Class.sortArray(arrayRef);
for(int cnt=0;
cnt < arrayRef.length; cnt++){
System.out.print(
arrayRef[cnt] + " ");
}//end for loop
System.out.println();
}//end main
Program Samp116.java
/*File Samp116
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
The use of array objects containing
refs to other array objects,
The ability to copy data from one
array object to another,
Samp116
Richard Baldwin
45 4
-17 -119
-------
45 4
-17 -119
**************************************/
import java.util.*;
public class Samp116 {
public static void main(
String[] args){
Samp116Class.displayYourName();
Random rGen =
new Random(new Date().getTime());
class Samp116Class{
public static void displayYourName(){
System.out.println("Samp116");
System.out.println(
"Richard Baldwin");
}//end displayYourName()
Program Samp118.java
/*File Samp118
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
The use of the generic type Object to
store a reference to an array
object,
The requirement to cast from Object
to int[] to access the data in the
array object stored as type Object.
Samp118
Richard Baldwin
45 4
-17 -119
-------
45 4
-17 -119
**************************************/
import java.util.*;
public class Samp118 {
public static void main(
String[] args){
Samp118Class.displayYourName();
Random rGen =
new Random(new Date().getTime());
class Samp118Class{
public static void displayYourName(){
System.out.println("Samp118");
System.out.println(
"Richard Baldwin");
}//end displayYourName()
Program Samp120.java
/*File Samp120
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
The use of array objects containing
refs to other array objects,
The ability to copy data from one
array object to another,
The use of the generic type Object to
store a reference to an array
object,
The requirement to cast from Object
to int[] to access the data in the
array object stored as type Object.
Samp120
Richard Baldwin
45 4
-17 -119
-------
45 4
-17 -119
**************************************/
import java.util.*;
public class Samp120 {
public static void main(
String[] args){
Samp120Class.displayYourName();
Random rGen =
new Random(new Date().getTime());
class Samp120Class{
public static void displayYourName(){
System.out.println("Samp120");
System.out.println(
"Richard Baldwin");
}//end displayYourName()
-end-
Richard holds an MSEE degree from Southern Methodist University and has many years
of experience in the application of computer technology to real-world problems.
-end-
• Preface
• Sample Programs
• About the Author
Preface
This is the third and last in a miniseries of lessons designed specifically to help the
students in my "Introduction to Object-Oriented Programming" course study for their
exams. However, others may find the lesson useful as well.
The lesson consists of a set of simple programs, each designed to illustrate one or more
important Java OOP concepts. The concepts involved are identified in the comments at
the beginning of each program.
The programs are designed to illustrate the code without providing a detailed discussion
of the code. You are referred to the other lessons in my online Java tutorials for detailed
discussions of the OOP concepts illustrated by these programs.
Sample Programs
Program Samp122.java
/*File Samp122
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
The use of the Date class to get and
to manipulate the current time.
Samp122
The current time is:
8:49:57 PM CST
One hour will be:
9:49:57 PM CST
Richard Baldwin
**************************************/
import java.util.*;
import java.text.*;
public class Samp122{
public static void main(
String[] args){
Samp122Class.doDate();
}//end main
}//end class Samp122
//===================================//
class Samp122Class{
public static void doDate(){
System.out.println("Samp122");
Date now = new Date();
long millisNow = now.getTime();
long oneHourMillis =
millisNow + 60*60*1000;
Date oneHour = new Date(
oneHourMillis);
System.out.println(
"The current time is:\n"
+ DateFormat.getTimeInstance(
DateFormat.FULL).
format(now));
System.out.println(
"One hour will be:\n"
+ DateFormat.
getTimeInstance(
DateFormat.FULL).
format(oneHour));
System.out.println(
"Richard Baldwin");
}//end doDate()
}//end class
Program Samp124.java
/*File Samp124
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
The use of the Date class to get and
to manipulate the current time.
Samp124
The current time is:
8:39 PM
One-half hour from now will be:
9:09 PM
Richard Baldwin
**************************************/
import java.util.*;
import java.text.*;
public class Samp124{
public static void main(
String[] args){
Samp124Class.doDate();
}//end main
}//end class Samp124
//===================================//
class Samp124Class{
public static void doDate(){
System.out.println("Samp124");
Date now = new Date();
long millisNow = now.getTime();
long oneHalfHourMillis =
millisNow + 60*30*1000;
Date oneHalfHour = new Date(
oneHalfHourMillis);
System.out.println(
"The current time is:\n"
+ DateFormat.
getTimeInstance(
DateFormat.SHORT).
format(now));
System.out.println(
"One-half hour will be:\n"
+ DateFormat.
getTimeInstance(
DateFormat.SHORT).
format(oneHalfHour));
System.out.println(
"Richard Baldwin");
}//end doDate()
}//end class
Program Samp126.java
/*File Samp126
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
The use of the Date class to get and
to manipulate the current date.
Samp126
The current date is:
12/1/01
Tomorrow will be:
12/2/01
Richard Baldwin
**************************************/
import java.util.*;
import java.text.*;
public class Samp126{
public static void main(
String[] args){
Samp126Class.doDate();
}//end main
}//end class Samp126
//===================================//
class Samp126Class{
public static void doDate(){
System.out.println("Samp126");
Date now = new Date();
long millisNow = now.getTime();
long oneDayMillis =
millisNow + 60*60*1000*24;
Date oneDay = new Date(
oneDayMillis);
System.out.println(
"The current date is:\n"
+ DateFormat.
getDateInstance(
DateFormat.SHORT).
format(now));
System.out.println(
"Tomorrow will be:\n"
+ DateFormat.
getDateInstance(
DateFormat.SHORT).
format(oneDay));
System.out.println(
"Richard Baldwin");
}//end doDate()
}//end class
Program Samp128.java
/*File Samp128
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
The use of the Date class to get and
to manipulate the current date.
Samp128
The current date is:
Dec 1, 2001
Tomorrow will be:
Dec 2, 2001
Richard Baldwin
**************************************/
import java.util.*;
import java.text.*;
public class Samp128{
public static void main(
String[] args){
Samp128Class.doDate();
}//end main
}//end class Samp128
//===================================//
class Samp128Class{
public static void doDate(){
System.out.println("Samp128");
Date now = new Date();
long millisNow = now.getTime();
long oneDayMillis =
millisNow + 60*60*1000*24;
Date oneDay = new Date(
oneDayMillis);
System.out.println(
"The current date is:\n"
+ DateFormat.getDateInstance().
format(now));
System.out.println(
"Tomorrow will be:\n"
+ DateFormat.getDateInstance().
format(oneDay));
System.out.println(
"Richard Baldwin");
}//end doDate()
}//end class
Program Samp130.java
/*File Samp130
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
The use of the Date class to get and
to manipulate the current date and
time.
Samp130
The current date and time is:
Sat Dec 01 19:59:25 CST 2001
One hour from now will be:
Sat Dec 01 20:59:25 CST 2001
Richard Baldwin
**************************************/
import java.util.*;
public class Samp130{
public static void main(
String[] args){
Samp130Class.doDate();
}//end main
}//end class Samp130
//===================================//
class Samp130Class{
public static void doDate(){
System.out.println("Samp130");
Date now = new Date();
long millisNow = now.getTime();
long oneHourMillis =
millisNow + 60*60*1000;
Date oneHour = new Date(
oneHourMillis);
System.out.println(
"The current date and time is:\n"
+ now);
System.out.println(
"One hour from now will be:\n"
+ oneHour);
System.out.println(
"Richard Baldwin");
}//end doDate()
}//end class
Program Samp138.java
/*File Samp138
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
A method that returns a reference to an
object.
The output consists of the following
five lines of text.
Samp138
5
Incoming null ref
5
Terminating, Richard Baldwin
**************************************/
class Samp138 {
int instanceVar = 5;
Samp138 secondRefToObj =
Samp138Class.returnRefToObj(
firstRefToObj);
firstRefToObj = null;
showInstanceVar(firstRefToObj);
showInstanceVar(secondRefToObj);
System.out.println("Terminating,"
+ new Samp138Name());
}//end main
//---------------------------------//
class Samp138Class{
class Samp138Name{
public String toString(){
return " Richard Baldwin";
}//end toString()
}//end Samp138Name
Program Samp140.java
/*File Samp140
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
A method that returns a reference to an
object. Also illustrates the ability
to deal with references to objects
in a fairly complicated manner. This
is simply an exercise in handling
refs to objects.
Samp140
Enter a keyboard char -> A
70
Terminating, Richard Baldwin
Samp140
Enter a keyboard char -> 0
53
Terminating, Richard Baldwin
**************************************/
class Samp140 {
int instanceVar;
class Samp140Helper{
int instanceVar;
Samp140Helper(Samp140 obj){
instanceVar = obj.instanceVar;
}//end constructor
Samp140 returnRefToObj(){
Samp140 newObj = new Samp140();
newObj.instanceVar =
this.instanceVar + 5;
return newObj;
}//end returnRefToObj()
}//end Samp140Helper
class Samp140HelperA{
Samp140 returnRefToObj(){
return new Samp140();
}//end returnRefToObj()
}//end Samp140HelperA
class Samp140Name{
public String toString(){
return " Richard Baldwin";
}//end toString()
}//end Samp140Name
Program Samp150.java
/*File Samp150
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Use of the getClass method of the
Object class, and a couple of methods
of the Class class.
Samp150 String
Richard Baldwin
java.lang.String
class java.lang.Object
Samp150 Button
Richard Baldwin
java.awt.Button
class java.awt.Component
**************************************/
import java.awt.*;
import java.util.*;
class Samp150{
Class refToClassObj;
if( rNum == 0){
System.out.println(
"Samp150 String");
refToClassObj = Samp150Class.
getClassObj(new String(""));
}else{
System.out.println(
"Samp150 Button");
refToClassObj = Samp150Class.
getClassObj(new Button());
}//end else
System.out.println(refToClassObj.
getName());
System.out.println(refToClassObj.
getSuperclass());
}//end main
}//end class Samp150
//===================================//
class Samp150Class{
public static Class getClassObj(
Object objectIn){
System.out.println(
"Richard Baldwin");
Class refToClassObj = null;
try{
refToClassObj = objectIn.
getClass();
}catch(Exception e){
System.out.println(e);}
return refToClassObj;
}//end getClassObj();
}//end class Samp150Class
Program Samp160.java
/*File Samp160
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Use of the forName() method of the
class named Class.
Samp160 Button
Richard Baldwin
java.awt.Button
class java.awt.Component
Samp160 String
Richard Baldwin
java.lang.String
class java.lang.Object
**************************************/
import java.awt.*;
import java.util.*;
class Samp160{
Class refToClassObj;
if(rNum == 0){
System.out.println(
"Samp160 String");
refToClassObj = Samp160Class.
returnClassObj(
"java.lang.String");
}else{
System.out.println(
"Samp160 Button");
refToClassObj = Samp160Class.
returnClassObj(
"java.awt.Button");
}//end else
System.out.println(refToClassObj.
getName());
System.out.println(refToClassObj.
getSuperclass());
}//end main
}//end class Samp160
//===================================//
class Samp160Class{
public static Class returnClassObj(
String stringIn){
System.out.println(
"Richard Baldwin");
Class refToClassObj = null;
try{
refToClassObj = Class.forName(
stringIn);
}catch(Exception e){
System.out.println(e);}
return refToClassObj;
}//end returnClassObj();
}//end class Samp160Class
Program Samp170.java
/*File Samp170
Copyright 2001, R.G.Baldwin
Rev 11/30/01
Illustrates:
Use of the asList method of the Arrays
class to get a List view of an array.
Note that changes to the List view
write through to the underlying array.
Samp170
Richard Baldwin
A B C
Ax Bx Cx
**************************************/
import java.util.*;
class Samp170{
public static void main(
String[] args){
System.out.println(
Samp170Class.showYourName());
Object[] refToArray =
{"A","B","C"};
//Display array contents
for(int cnt=0;
cnt<refToArray.length;cnt++){
System.out.print(
refToArray[cnt] + " ");
}//end for loop
System.out.println();
class Samp170Class{
static String showYourName(){
System.out.println("Samp170");
return "Richard Baldwin";
}//end showYourName()
-end-
Richard Baldwin is a college professor (at Austin Community College in Austin, TX)
and private consultant whose primary focus is a combination of Java and XML. In
addition to the many platform-independent benefits of Java applications, he believes that
a combination of Java and XML will become the primary driving force in the delivery of
structured information on the Web.
Richard holds an MSEE degree from Southern Methodist University and has many years
of experience in the application of computer technology to real-world problems.
-end-