Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

Introduction to Programming 2 v2.0 - JEDI

JEDI is a collaborative project that aims to make high-quality, industry-endorsed IT and Computer Science course material available for free. The course materials are developed with inputs from industry and conforms to international education standards. JEDI materials and resources are developed, used and enhanced in a collaborative environment using java.net.

JEDI is a project of Sun Microsystems, Inc. through the University of the Philippines Java Research and Development Center and in partnership with various groups from the Education and Industry Sectors.

Object-Oriented Analysis and Design Understanding System Development with UML 2.0 - Mike O’Docherty

The aim of this book is to give you a broad understanding of the processes and techniques
used in object-oriented software development, the production of computer programs using
object-oriented techniques. The Unified Modeling Language (UML) comes into play as the
standard notation used in industry for software documentation.

You may be a student at a university or on a commercial training course. Or you may
be an experienced software developer, moving into object orientation for the sake of your
career. In either case, this book is for you. Little prior knowledge is required on your
part and there is no attempt to teach you everything there is to know. Instead, you will
be guided through the essential parts of the process, so that you can do your job more
effectively.

Although this book is broad, it only goes as deep as the point where you would normally
start writing lines of code. Describing how to write code would mean choosing a particular
programming language; the decision about the most appropriate language is for you to
make. You should consider this book as a generic front-end that will work for any pure,
object-oriented programming language.



*If the link is broken, don't hesitate to contact me.

GUI - How to change the background Color of the Panel

So in this program, let's try to change the background color of a panel.

Now in this program, we utilize the AWT. It is possible to use the Swing but let's do that on anot
her problem.

Ok so now, we use a Scrollbar for this in order to freely adjust the values of our color using the RGB format(0-255).

Since the there is no ActionListener of Scrollbar, unlike Button, but the Scrollbar is a AdjustmetListener found at java.awt.event package with only one method signature and that is adjustmentValueChanged(AdjustmentEvent e).

We are to use the Scrollbar objects to accommodate the three colors(Red, Green, and Blue. The event for each color sin
ce each change would just read the current value for each Scrollbar and use it as basis for the new color of the Panel.

The Scrollbars are namely, sbRed, sbGreen, and sbBlue. We set the minimum value of the scrollbar to 0 and maximim to 255 since the RGB only accepts 0-255. To set the Scrollbar with the values given, we use its constructor Scrollbar(int orientation, int value, int visible, int minimum, int maximum).

For the orientation, 0 is horizontal and 1 is vertical.

For the value, it is for the initial value the scrollbar.

For the visible, it is for how should the scroller for the Scrollbar should appear on the Scrollbar. If set to 0, it would adjust automatically. It is actually based on the range of the value for minimum to maximum. If it is 255, then it would match the maximum and would cover the all of the Scrollbar. Just be careful since if the value is greater than the maximum range, then it would generate an Error.

For minimum, it is the minimum value and the maximum is for the maximum value.

To get the value of each Scrollbar, which is an integer value, is the getValue() method. Example, sbRed.getValue().

I created one class for the AdjustmentListener to make the event listeners uniform for each scrollbar since named AdjL. But in this implementation, I need to make it an inner class so that I could access the objects. Though it is possible to use an outside class, at least I'll be able to only use only one file.

The layout for the Scrollbars, the top is for the Red, the middle is for the Green, then the bottom bar is for the Blue.


Sample output.


Doubly Linked List Implementation


So here is another implementation of another linked list, Doubly-Linked List.
For a doubly-linked list, each node is divided into
 three parts: the left pointer field, the data field, and t
he right pointer field.
The left pointer field is used to contain the 
address of the preceding node in the list or what is known as the Predecessor
Unlike the sing
ly-linked list counterpart, the doubly-linked list can traverse backwards since there is a pointer to poin
t at the previous node.

The algorithms:

-       Inserting a Node into a Doubly-Linked List

o    General procedure

1.     Create a new node for the element

2.     Set the data field of the new node to the value to be inse

rted.

3.     Determine the position of the node in the list based on its valu

e

4.     Insert the node

-       Inserting a node into the Head of the List

o    The algorithm

1.     Set the left pointer field of the new node to null

2.     Set the right pointer field of the new node to the address contained in the head.

3.     Set the left pointer filed of the current head node in the list to the address of the new node.

-       Insert a Node at the End of a List

o    The algorithm:

1.     Set the right pointer field  of the new node to null

2.     Set the left pointer field of the new node to tail

3.     Set the right pointer field of the current tail node in the list to the address of the new node.

4.     Set the variable tail to the address of the new node.

-       Inserting a node within the list

o    The algorithm:

1.     Determine the position of the node in the list

2.     Set the left pointer field of the new node to the address of the current node.

3.     Set the right pointer field of the new node to the address of the current.next node.

4.     Set the right pointer field of the current node to the address of the new node

5.     Set the left pointer field of the current.next node to the address of the new node.

-       Deleting a node from a doubly-linked list

o    General procedure

1.     Locate the node

2.     Delete the node

3.     Release the node form memory

-       Deleting the node at the Head of the list

o    The Algorithm

1.     Set the variable head to the address of the second node in the list.

2.     Set the left pointer field of the new head node to null

-       Deleting the node from within the list

o    The Algorithm

1.     Set the right pointer field of current.previous node to the address of the current.next node

Set the left pointer field of the current.next node to the address of the current.previous node

The code and the output:

JPanel - Drawing a Star

To draw a star inside a panel, we put the code inside the paint method of the Panel class and use the fillPolygon() to draw our star since this method utilizes multiple points to draw a shape. Specifically, based on an array of X's and Y's.

So here's the code and the corresponding output.

Singly Linked List Implementation

For this implementation of a linked list in Java, I created my own class for a node called SLNode since a different node is needed for a singly and a doubly.

Here is the SLNode class:
class SLNode
{
public Object data;
public SLNode next;
public SLNode(Object d)
{
data=d;
next=null;
}
}

The next code is the class for the singly linked list called the SLinkedList

The attributes:
SLNode head;
SLNode tail;
We only need two attribute since a singlylinked list only has a head and a tail

The constructor:
public SLinkedList()
{
head=null;
tail=null;
}

It all attributes are set to null since from the beggining, the list is empty.

3 methods for adding data. Why 3? It is because there are three situations to take note when adding data within a singly linked list namely: when adding at the beginning, at the middle (1<1

public void addFirst(SLNode newNode) //add at the beginning
{
newNode.next=head;
head=newNode;
}
public void addLast(SLNode newNode) //add at the end of the list
{
tail.next=newNode;
tail=newNode;
}
public void add(SLNode newNode, int pos) //add at a specified position
{
SLNode current = head;
SLNode previous = head;
int ctr=0;
if(head!=null && tail!=null) //if list is not empty
{
while(current != null)
{
ctr++;
if(ctr == pos)
{
if(ctr==1)
addFirst(newNode);
else
{
newNode.next=current;
previous.next=newNode;
}
return;
}
previous = current;
current = current.next;
}
}
else //if list is empty
{
System.out.println("The list is empty. The node was added as the first and the last node.");
addFirst(newNode);
tail=head;
return;
}
addLast(newNode);
if(pos > ctr+1)
System.out.println("The desired position is still unavailable."
+ "\nThe node was inserted at position " + ctr
+ " which is considered as the last position.");
}

When deleting an item within the list, you only need to consider 2 things: if deleting the beginning of deleting the end. In this implementation, I incoporate the two situations into one function. But the 2 situations are still noted. Also, in the codes presented, two functions are presented, first if deleting based on the object you want to delete and the other is based on the what node you want to delete (eg. Node #2 or the 2nd Node)

public SLNode delete(Object data) //delete based on the item you want to delete
{
SLNode current=head;
SLNode previous=head;
int ctr=0;
while(current!=null)
{
ctr++;
if(current.data==data)
{
if(ctr==1)
head=head.next;
else if(ctr==length())
{
previous.next=null;
tail=previous;
}
else
previous.next=current.next;
return current;
}
previous=current;
current=current.next;
}
return null;
}
public SLNode delete(int i) //delete based on what node you want to delete starting at 1
{
SLNode current=head;
SLNode previous=head;
int ctr=0;
while(current!=null)
{
ctr++;
if(ctr==i)
{
if(ctr==1)
head=head.next;
else if(ctr==length())
{
previous.next=null;
tail=previous;
}
else
previous.next=current.next;
return current;
}
previous=current;
current=current.next;
}
return null;
}

The other methods are just accessory for extra functions for the SLinkedList class
public boolean search(Object node) //search a data based on a given key
{ //if found return true, else false
SLNode current=head;
while(current!=null)
if(current==node)
return true;
return false;
}
public SLNode getAt(int i) //this is similary to peek 
//returning a value at a given Node number
{
SLNode current=head;
int ctr=0;
while(current!=null)
ctr++;
if(ctr==i)
return current;
current=current.next;
}
return null;
}
public int length() //returns the current length/size of the list
{
int ctr=0;
SLNode current=head;
while(current!=null)
{
ctr++;
current=current.next;
}
return ctr;
}

Here are some codes on the implementation of the Singly-Linked List
public static void main(String args[])  
{  
SLinkedList list=new SLinkedList();  
list.add(new SLNode((Object)"Abcd"),2);  
list.add(new SLNode((Object)"CDeFG"),2);  
list.add(new SLNode("HigjDH"),2);
System.out.println("\nThe list size is now " + list.length());
displayList(list); System.out.println(list.delete(2));
displayList(list);
}

public static void displayNode(SLNode node) // to display the data of a single node
{
System.out.print(node.data);
}

public static void displayList(SLinkedList list) // to display the an entire list
{
SLNode current=list.head;
int ctr=0;
while(current!=null)
{
ctr++;
System.out.print("Node " + ctr + " ");
displayNode(current);
System.out.println();
current = current.next;
}
}