Martrinex Learning Center
HOME CONTENTS
 
This website is underconstruction.
Author: Martin Sykes
Challenge: I have challenged myself to 1 article per day, and framework improvements! Enjoy
Challenge end: unknown

Classes

We have already seperated our code in to different functions, now its time to seperate our functions (and variables) in to different classes, you have already used classes I have asked you to create a 'main' class in every example and project throughout the tutorials, the 'main' classes are ofcourse where the program starts, they are named the same as the program and contain the function 'main'.

Not only do classes seperate code, they are objects, which can be re-used, inherited and created with the 'new' statement, they can be created in to Abstract Data Types (ADT) such as queue, stack, linked lists, tree's, etc....

Example Abstract Data Types Queue and Stack
These are two of the easyer ADT's for convenience we will use the Primitive Data Type Array of Strings for storage.
So we are storing information, we get one piece of information and push it to the array, randomly we may also get requests for information and we must pop that from the array.
Stack, like a stack of plates meens the first item in, the bottom plate is the last one to be used. Since the other plates get put on top and get in the way.
Getting items from a stack is known as push (add to), pop (remove from) these functions will be accessible from our class.

Obviosly we are going to make these ADT's now, starting with Stack (its easier)
Create a new project named ADTS with a main class named ADTS (same as always)
Right click on the (default) package and add a new class

Name it "Stack" DONT tick the "Public static main"
You should have a new class with just:
Code:
public class Stack {


}
We need to add:
  1. A String Array to store the information
  2. An integer pointing to the next read/write position in the array
  3. A function to check if stack is empty (BOF) beginning of file.
  4. A function to check if stack is full (EOF) end of file.
  5. A function to add items. (Push)
  6. A function to remove items. (Pop)
  7. Some code to the main class to test the program
1. String Array
At the top of the class, just under the class declaration you can add variables these are known as global and can be used throughout the class. Add:
Code: String[] data = new String[50];
2. Integer pointing to the next read/write
Again at the top add:
Code: int pointer=0;
3. Check if empty (BOF)
This requires a function again add it after:
Code:
// Begining of file, is there any data to read?
public boolean BOF(){

return (pointer==0);
}
Note im using shorthand in this example from this tutorial so return (pointer==0), is same as:
if(pointer==0) return true; else return false;
4. Check if full (EOF)
Again add this function after:
Code:
// End of file, is there any space for more data?
public boolean EOF(){

return (pointer == data.length);
}
This is also shorthand, it checks if the pointer (read/write) pos is pointing to the end of the array (data.length) same as:
if(pointer==data.length) return true; else return false;
5. Push (add) items function
Code:
// Pushes (adds) data into the array, returns false if array is full.
public boolean Push(String strData){

if(EOF()) return false;
data[++pointer] = strData;
return true;
}
boolean function helps us debug, by sending true if it worked and false if it didn't
if(EOF()) keeps the program from crashing stopping the addition of data past the end of the array
data[++pointer] = strData: Yes more shorthand! What im lazy... I showed you var++ before to add 1 to a variable named 'var', placing ++ before the variable name does the same thing BUT the variable is read before the addition instead of after, WTF? you say, ok... the code is the same as:
Code:
// Pushes (adds) data into the array, returns false if array is full.
public boolean Push(String strData){

if(EOF()) return false;
data[pointer] = strData;
pointer = pointer + 1;
return true;
}
6. Pop (remove) items function
Code:
// Pop (removes) data from the end of the array, returns "" if array is empty.
// Note: an array item might be "" so always call BOF before this function.
public String Pop(){

if(BOF()) return "";
return data[pointer--];
}
if(BOF()) return ""; Stops crashes but is NOT good enough on its own, their is no way for the user to tell if "" is a value or an empty array... theirfor call if(!BOF()) System.out.println("Value:"+Pop()); else System.out.println("Empty");
return data[pointer--]; simpler shorthand, subtracts 1 from pointer, reads data[pointer] and returns it, same as:
Code:
// Pop (removes) data from the end of the array, returns "" if array is empty.
// Note: an array item might be "" so always call BOF before this function.
public String Pop(){

if(BOF()) return "";
pointer = pointer - 1;
return data[pointer];
}
1-6:
Just incase here is the "Stack ADT class, download and paste in your workspaces (/ADTS/src/Stack.java)
Download

Stack.zip [0.5kb]
7: Test code
Code:
public class ADTS {

public static void main(String[] args) {

// TODO Auto-generated method stub
Stack stack = new Stack();
// yes a crude advertisement of my websites...
stack.Push("www.martrinex.net");
stack.Push("www.rapidvb.com");
stack.Push("learning.martrinex.net");
stack.Push("www.goodhostbadhost.com");
stack.Push("www.phpdataweb.com");
stack.Push("facebook.martrinex.net [construction]");
stack.Push("mydigipet.com [closed]");
// pop these items out
while(!stack.BOF())
System.out.println(stack.Pop());
// run and notice the reverse order! welcome to the stack.

// but what happens if we read/write randomly?
System.out.println("Random read tests:");
stack.Push("a");
stack.Push("b");
System.out.println(stack.Pop());
stack.Push("c");
stack.Push("d");
// pop these items out
while(!stack.BOF())
System.out.println(stack.Pop());

// items are deleted once read, always the last item added is the 1st read.
}

}

Tasks

Note the queue will be covered later in the tutorials (this one is long enough)
1. What order will these letters be printed? (don't run the code)
Code:
stack.Push("a");
stack.Push("b");
stack.Push("c");
stack.Push("d");
System.out.println(stack.Pop());
stack.Push("e");
stack.Push("f");
stack.Push("g");
System.out.println(stack.Pop());
stack.Push("h");
stack.Push("i");
System.out.println(stack.Pop());
System.out.println(stack.Pop());
System.out.println(stack.Pop());
stack.Push("j");
[click for answer]
2. What happens if the array is full and you try to add another?
[click for answer]
3. What happens if you try to pop an item from the empty array?
[click for answer]
4. What happens if you push "" and pop that?
[click for answer]
Enough with the dumb questions! Have a nice day.

Add Comment

Help the web master by commenting on the tutorials, thankyou.
Name:
Email: (this will not be shown)
Comment:
  Send email to admin, don't post.

 

Comments

Nobody has commented on this article.
Be the first, the webmaster needs comments good or bad to improve this site.



Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Martrinex Learning Center[X]
  Introduction Beginner Intermediate

Important useful well commented source codes
Please read the import tutorial to know how to use them.

Source