CHAPTER 4
Structures in Java TOC

Object Oriented ProgrammingTOC


Much is heard these days about object oriented programming. There seems to a mysterious aura sourronding the concept which is rarely defined. The basic idea of object oriented programming in Java is to create a single defining structure. Most generally these structures are called classes. In Java the entire universe is made up of classes. The structure of each class is then determined by the programmer.

Each class then, is like an object which the programmer manipulates. These objects all have properties which are defined within the original class definition. The properties of the object makes it easier for the programmer to save and recall the properties of each individual object and to make use of these propeties in the programming environment. In Pascal these function were assummed under many different guises: records (data structures); functions; procedures and units.

In more recent languages there has been an attempt to combine all functions procedures and data structures into one common structure type - a class.

Before we cover some of the delicate matters of object oriented programming we will discuss the basic predefined structures in Java.

Data TypesTOC


TypeStorageRange
int4 bytes-2,147483,648 to
2,147,483,647
short2 bytes-32,768 to
32,767
long8 bytes|9x1018|
byte1 byte-128 to
127
float4 bytes|3x1038|
6 sig digits
double8 bytes|10308|
15 sig digits
char2 bytesUnicode
a denoted by 'a'
boolean.true
false

Operators TOC


TypeNameExample
+addition2+3 is 5
-subtration3-2 is 1
*multiplication3*4 is 12
/division12/3 is 4
%modular
arithmetic
13 % 5 is 3
+=increment by x += 4 sets x
to x + 4
Math.pow(,)import java.lang.Math
must be used
Math.pow(x,3) computes
x cubed

Relational OperationsTOC


TypeNameExample
= =is equal to(2= =3) is false
(2= =1+1) is true
!=not equal(2!=3) is true
(2!=1+1) is false
<less than(3<4) is true
(3<3) is false
>greater than(3>4) is false
(4>3) is true

String Data TypeTOC


The string data type is not built into Java but is a class and thus an object. For most applications this fact is not important. Generally, strings can be used like any data type in the typical implementation of Java to the web. Note that the string is treated as an array each component being a letter. Since string is a class it has characteristics. If s represents a string, then we may test for equality with the following bollean function:

s.equals("some_string")

this returns a true value if the string s and the function argument agree.

Colors TOC


Note that -import java.awt.Color- should appear at the top of the program using colors. It is common to simply -import java.awt.*- which makes the entire jav.awt library available. The standard colors are:

Color.black
Color.blue
Color.cyan
Color.darkGray
Color.gray
Color.lightGray
Color.magenta
Color.orange
Color.pink
Color.red
Color.yellow


Other colors may be defined using advanced techniques.

Loop Control StructuresTOC


DO/WHILE LOOP

The do/while loop executes a set of instructions repeatedly until some condition is satisfied. The condition is checked after the instruction set is executed. For example:

int i=1;
do{ // begin do loop
i++;
i=2*i;
}while(i<100); //end do loop


This loop will change i to 2 then multiply by 2 to get 4. The second execution will change i to 5 then multiply by 2 to get 10 and so forth until i is 100 or greater.

WHILE LOOP

The while loop first checks the condition and if it is true then it executes the instruction set. For example:

int i=1;
while(i<100){ // begin while i is less than 100 is true
i++;
i=2*i;
} // end while


FOR LOOP

The for loop allows the programmer to initialize variables, set values, increment and test conditions for exiting from the loop. The for loop executes a set of instructions repeatedly until the condition is satisfied. For example:

j=1;
for(int i=1; i<100; i++){ // begin for i
j=j+2;
} //end for i


BREAK/CONTINUE

These commands allow for advanced controls within loops.

Decision Structures TOC



IF/ELSE

The if/else if allows the programmer to test two or more coditions and to execute different instruction sets for each condition.

if(j==1){j=2;} //simple if
if(j==2){ // begin if j is 2
    j=j+2;
  } //end if j is 2
  else if(j==3){ //begin if j is 3
      j=j+3;
    } //end else if
else { j=100; i=200;} //end else - end block


SWITCH

The switch statement allows the programmer to simply test several conditions and to execute different instruction sets for each condition. You may only test byte, char, short, int, long types.

switch(count){ \\begin case checks
      case 1: note="one"; break;
      case 2: note="two"; break;
      case 3: note="three";break;
      default: note="na";break;
             } //end case checks

Objects as Data TypesTOC


One important application of classes is to constructing data types. Let us say that we wish to construct a type which represnts a square. First we must decide which properties we wish to give to our square. Recall that a square can be drawn in an applet using a "square" valued button someting like this:

if(event.target==square_button){ //begin square draw
        Graphics g=this.getGraphics();
        g.setColor(current_color);
        g.drawRect(10,10,40,40);
        return true;
        } // end square draw

One might say that the minmal characteristics of the square are the upper left hand corner. and the length of a side. We could the define the class

public class Square{

  public int x;
  public int y;
  public int side;
    }

We could then obtain an instance of this class object with the declaration.

    public Square sq=new Square();

We can now store data and retrieve data from our Square class.

sq.x=10;
sq.y=10;
sq.side=30;
if(event.target==square_button){ //begin square draw
        Graphics g=this.getGraphics();
        g.setColor(current_color);
        g.drawRect(sq.x,sq.y,sq.x+sq.side,sq.y+sq.side);
        return true;
        } // end square draw

The problem is that this will only allow us to use one set of data. That is any new instance of Square will point to the same data. To implement more usable data classes we must instruct the compiler about the method of cloning or replicating the data structure.

public class Square implements Cloneable{

  public int x;
  public int y;
  public int side;
        public Object clone(){ //begin clone
        try{ //begin try
        return super.clone();
        } //end try

        catch(CloneNotSupportedException e){ // begin catch
        return null; //if there is a catch
        } //end catch
        } //end cloning structure for point
  } // end def of Square

Note that the definition must say -implements Cloneable-. This allows java to invoke the super.clone method. Now we can define two different instances of Squares.

    public Square sq1=new Square();
    public Square sq2=new Square();

If the data structure is more complicated the super.clone method will not suffice. Consider the following example:

public class point implements Cloneable{
public int x;
public int y;

public Object clone(){ //begin clone
try{ //begin try
return super.clone();
} //end try

catch(CloneNotSupportedException e){ // begin catch
return null; //if there is a catch
} //end catch
} //end cloning structure for point

} //end point def


public class Vcirc implements Cloneable{

public point c=new point();
public point v=new point();
public int s;
public boolean sel=false;
public Color cl=Color.black;

public Object clone(){ //begin clone
try{ //begin try
Vcirc vc=(Vcirc)super.clone();
vc.c=(point)c.clone();
vc.v=(point)v.clone();
return vc;
} catch(CloneNotSupportedException e){
return null;
}//end catch

} //end cloning structure for virtual circle c is center, v is vector

} //end virtual circle def

Project 1

Write a program that will draw a specified chord on the circle.

Project 2

Extend the program to draw line art on the circle. That is iterate the points on the circle drawing a chord for each arc at a fixed angle.

Project 3

Add buttons to the program to allow the user to change colors and select the chord length and circle size.

Project 4

Time permitting: study the Geom.java program and extend its capabilities.


References TOC