Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

How to Build a Chrome Extension (Lifehacker.com)


How to Build a Chrome ExtensionGoogle Chrome is the best web browser around right now, and part of Chrome's appeal is owed to its excellent extensions. The good news: It's not that hard to get started making your own Chrome extensions. In this guide, we'll take you from the most simple Hello World extension (no HTML or JavaScript knowledge required) to a more complex RSS-fetching extension to get you started down your path as a Chrome-extension-making guru.



Click here to continue reading

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.

Intorduction to Programming 1 v1.3 - 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.

Data Structures 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.

Developing Games in Java by David Brackeen

If you already have experience programming games with Java, this book is for you. David Brackeen, along with co-authors Bret Barker and Lawrence Vanhelsuwe, show you how to make fast, full-screen action games such as side scrollers and 3D shooters. Key features covered in this book include Java 2 game programming techniques, including latest 2D graphics and sound technologies, 3D graphics and scene management, path-finding and artificial intelligence, collision detection, game scripting using BeanShell, and multi-player game engine creation.

* Please notify me if the lick is broken.

Java EE Tutorial

This tutorial is a guide to developing enterprise applications for the JavaTM Platform, Enterprise
Edition 5 (Java EE 5).



*If the download link has been broken. Feel free 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.

Strings - Repeated Word

So here is the code requested. 

For this function, a word is recognized if the length of its characters is greater than one. So basically, to look for repeated words, first thing to do is know all combination of these words.

To look for these words, what I did was to get the substring of the entered string with 3 in length up to its maximum length. Since in java, the String class doesn't have any methods for substring, we I converted the String class object to a StringBuffer object. The stack is just used to determine if there is a repeated word(if stack is empty, no repeatitions).

After determining the word, we then traverse the string starting from start to the end comparing the newly found word and the substrings of specified index retrieved during the traversing.

The code:


The problem for this code though is that it recognizes spaces as part of the word.
A solution would be to separate all words then put it on a stack then base the searches on these grouped words.

String - Partial Reverse (First Characters - Alphabets)

So here is another problem that i just thought of. This problem is best done with stacks. So the problem is basically to let the user enter a string and then the first series of alphabets should be reversed. Here are some examples.

eg1:

Original: Hello World

Result: olleH World

eg2:

Original: text123

Result: txet123

eg3:

Original: -hello

Result: -hello


Meaning, the reverse will only proceed if the first series of characters are alphabets

So the concept for the solution would be,

  1. If recognize a character from the start, push the token to the stack

  2. If not, pop and display everything from the stack and concatinate with the remaining
    tokens.

These are the only steps needed.

Introducing, THE CODE:

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;
}
}

String - Inverse Case

The concept of doing the Inverse Case on a given string is simply to determine if a found letter is in what case and converting it to its inverse case, leaving the none Alpha characters alone. 

In this program, each letter is converted to its ASCII value, determine where it is located, if capital, add 32, if not, subtract 32. It can be considered capital if it is within 65 to 90, since the ascii values of capital letters are within this range. For small letters, 97 to 122 will be the range. So basically, the range between a letter and its opposite case is 32.

Here is the code.

import java.io.*;
public class InverseCase
{
public static void main(String args[]) throws IOException
{
BufferedReader k=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a string : ");
String str=k.readLine();
System.out.print("\nResult : ");
for(int i=0; i
{
int c=str.charAt(i);
if(c>=65 && c<=90)
c+=32;
else if(c>=97 && c<=122)
c-=32;
System.out.print((char)c);
}
System.out.println("");
}
}

Stack - Converting Infix Notation to Postfix Notation and Vice Versa


This code uses its own stack of Strings to proceed with the convertion from Infix/Postfix to Postfix/Infix.








Converting Infix to Postfix Notation Algorithm

1.     If recognize an operand, display

2.     If recognize a ‘(‘, push it on the stack

3.     If recognize a ‘)’

a.     Pop and display until encountering the first ‘(’ inside the stack

b.     Pop the ‘(’ from the stack

4.     If recognize an operator

a.     Peek from the stack and compare to the operator

b.     If stack is empty, push it on the stack

c.     If stack is not empty

                                          i.    If top of the stack is ‘(‘, push it on the stack

                                         ii.    If top of the stack is operator

1.     If top of the stack is of higher precedence, pop and display until encountering the first ‘(’ or if stack is empty

2.     If top of the stack is of lower precedence, push it on the stack

3.     If top of the stack is of equal precedence, pop and display then push it on the stack.

5.     If done reading, pop and display till stack is empty.

Converting Postfix to Infix Notation Algorithm

1.     If recognize an operand, push it on the stack

2.     If recognize an operator, pop its operands (pop 2 operands), and apply the operator and push the value on the stack.

o    If an item popped is already an expression, enclose the expression with ‘(‘ and ‘)’

3.     Upon conclusion, the value of the postfix expression is on the top of the stack.

 

-       The algorithm is based on the following assumptions:

1.     Each operand is denoted as a single alphabetic character.

2.     The expression may only 


Click here for the code