Saturday, June 1, 2013

Java linked node demo

LinkedList class:

/*  Program written by Luis Gallego

*
*/

public class LinkedList {
Node head;

public void insert(String item) {

Node curr = head;
Node prev = head;

// If the list is empty, then execute this if statement
if (head == null)
head = new Node(item);
else {

while (curr != null && curr.data.compareTo(item) < 0) {
prev = curr;
curr = curr.next;
}

// add beginning
if (head == curr) {
Node newNode = new Node(item);
newNode.next = head;
head = newNode;
}

else if (curr == null) {
Node newNode = new Node(item);
prev.next = newNode;
}

else {
Node newNode = new Node(item);
prev.next = newNode;
newNode.next = curr;
}
}
}

public void delete(String item) {

Node curr = head;
Node prev = head;

// if the list is empty
if (curr == null) {
System.out.println("The list is empty!");
}
// if the head node is the item we want to delete
else if (curr.data.compareTo(item) == 0) {
head = head.next;
}
// general case if node is in the middle
else {

while (curr != null && curr.data.compareTo(item) != 0) {
prev = curr;
curr = curr.next;
}
if (curr == null) {
System.out.println("Item not found!");
} else {
prev.next = curr.next;
}
}
}

// method to print the list
public void print() {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println(); // move cursor
}

public static void main(String[] args) {

LinkedList myList = new LinkedList();
myList.print();
myList.insert("apple");
myList.print();
myList.insert("orange");
myList.print();
myList.insert("kiwi");
myList.print();
myList.insert("tangerine");
myList.print();
myList.insert("strawberry");
myList.print();
myList.delete("apple");
myList.print();
myList.delete("strawberry");
myList.print();
myList.delete("tangerine");
myList.print();
myList.insert("apple");
myList.print();
myList.delete("tangerine");
myList.print();
myList.delete("apple");
myList.print();
myList.delete("tangerine");
myList.print();
myList.delete("apple");
myList.print();
myList.delete("kiwi");
myList.print();
myList.delete("orange");
myList.print();
myList.delete("strawberry");
myList.print();
myList.insert("job-well-done");
myList.print();
}
}

syntax highlighting by pygments via the monkey pygment web app

Node Class:
/* Program written by Luis Gallego

*/
public class Node {
String data;
Node next;

public Node(String data){
this.data = data;
next = null;
}

// set and get methods

public Node(String data, Node nextNode){
this.data = data;
next = nextNode;
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}

public Node getNext() {
return next;
}

public void setNext(Node next) {
this.next = next;
}

// Overloaded toString method
public String toString(){
return data;
}

}

syntax highlighting by pygments via the monkey pygment web app

Monday, May 27, 2013

Dual screen settings don't save after logging out/reboot, Arch Linux & KDE

After setting up up a dual screen setup on Arch Linux inside of KDE, rebooting or logging off discards any changes made to the set up, and you go back to the default setting.  In my case it went to both screens being clones.  Even changing the setting inside of KDE's monitor settings , and saving it as the default setup didn't work.

To fix this, you have force it using the xorg.conf file to hold the dual screen set up.

To do this run the nvidia settings , by going to K-Launcher > type in nvidia , and then click on the nvidia settings shortcut.

Inside nvidia settings config the display to exactly what you want, then hit the "Save Configuration" button.  At this point just save it to a location where you know it's location.

Simply replace your old xorg.conf with the new one that you just generated.  This should fix this problem, and also take care of any resolution issues that may arise.


Factorial in Java, Recursively and Non-Recursively

FactorialDemo demonstrates how to implement Factorial problem both recursively and non-recursively in Java.

1:  // Program by Luis Gallego Cardona.   

2:  package com.factorialdemo;
3:  import javax.swing.*;
4:  public class FactorialDemo {
5:       public static void main(String[] args) {
6:            String input; // hold the input
7:            int number; // hold a number
8:            // Get the number from the user
9:            input = JOptionPane.showInputDialog("Enter a non-negative number");
10:            number = Integer.parseInt(input);
11:            // Display the users input
12:            JOptionPane.showMessageDialog(null, number + "! is " + factorial(number) + ", using recursion"); // This shows the factorial using recursion
13:            JOptionPane.showMessageDialog(null, number + "! is " + nonrecursivefactorial(number) + ", not using recursion"); // This shows the factorial NOT using recursion
14:            System.exit(0);  
15:            // NOTE: What is the purpose of System.exit(0)? I know I have to call this whenever I use JOptionPane ... but that's about it.
16:       }
17:       private static int nonrecursivefactorial(int n) {
18:            int x; // this holds the number
19:            int fact = 1; // this is a place holder for the result of the factorial
20:            /** The for loops takes in a number as x, then assigns the value as n, which is the counter in the for loop.  
21:             * it then multiplies the number we passed in by 1 (base case), then it keeps multiplying the new number all the way until n is < 1. Thus breaks the loop
22:             */
23:            for (x = n; x > 1; x--)
24:                 fact *= x;
25:            return fact;
26:       }
27:       private static int factorial(int n) {
28:            if(n == 0)
29:                 return 1; // this is the base case , because 0! = 1
30:            else  
31:                 return n * factorial(n - 1);
32:       }
33:  }

Fibonacci sequence in Java, Recursive and Non-Recursive

This is an example in Java of how to implement the Fibonacci Sequence both recursively and Non-Recursively (or using loops).  Any questions please ask, if you don't understand how a method works, make a table!

1:  // This program is written by Luis Gallego Cardona.  

2:  package com.fibonaccidemo; // Comment out if it gives you issues
3:  import java.util.Scanner;
4:  public class FibonacciDemo {
5:       public static void main(String[] args) {
6:            Scanner keyboard = new Scanner(System.in);
7:            int number = 0;      // hold the number
8:            System.out.print("Please enter the nth term of the Fibonacci: ");
9:            number = keyboard.nextInt();
10:            // Recursive call that prints out each number in the sequence up to the desired nth term.
11:            for (int i = 0; i < number; i++)
12:                 System.out.print(fibonacci(i) + " ");
13:            System.out.println();
14:            // Non-Recursive call that simply prints out the nth term in the sequence.
15:            System.out.print(fibonacciNonRecursive(number) + " ");
16:            keyboard.close();
17:       }
18:       /** fibonacciNonRecursive method
19:        * @param n The nth number to calculate
20:        * @return The nth number.
21:        */
22:       private static int fibonacciNonRecursive(int n) {
23:            if(n == 0)
24:                 return 0;
25:            int x = -1, y = 1, z =0;    
26:            for (int i = 1; i <= n; i++){
27:                 z = x + y ;
28:                 x = y;
29:                 y = z;
30:            }
31:            return y;
32:       }
33:       /**
34:        * The fibonnaci method calculates the nth term in the Fibonnaci series
35:        * @param n The nth number to calculate
36:        * @return The nth number.
37:        */
38:       private static int fibonacci(int n) {
39:                 if (n == 0)
40:                      return 0;
41:                 if (n == 1)
42:                      return 1;
43:                 else
44:                      return fibonacci(n-1) + fibonacci(n-2);
45:       }
46:  }

Sunday, April 28, 2013

Dual booting Arch Linux + Windows 7

This is assuming that you have Arch installed on its own hdd, and same with windows.  To install windows, just plug in the hard drive to which you intend to install windows 7.   After installation, plug in both hard drives again, making sure you make Arch linux primary and the windows 7 slave.

Boot up arch linux, and install os-probe:

# pacman -S os-probe


Then run the mkconfig command, to rebuild your grub config file:

# grub-mkconfig -o /boot/grub/grub.cfg


WARNING:

If you end up having duplicate entries in your grub menu for Windows 7, check the files in which you can add  custom menu entries. For my computer, I had a manual entry under the file /etc/grub.d/40_custom

All you have to do is open up the file and take out whatever entry is already in there.


# nano /etc/grub.d/40_custom 


The re-reun the mkconfig command above, and you should be good 2 go.

To me it seems like the arch wiki makes this a more complicated problem than it should be.