Wednesday, October 13, 2010

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication1;

/**
*
* @author st_krizan
*/
// -----------------------------------------------------------------------------
// LinkedListExample.java
// -----------------------------------------------------------------------------

/*
* =============================================================================
* Copyright (c) 1998-2005 Jeffrey M. Hunter. All rights reserved.
*
* All source code and material located at the Internet address of
* http://www.idevelopment.info is the copyright of Jeffrey M. Hunter, 2005 and
* is protected under copyright laws of the United States. This source code may
* not be hosted on any other site without my express, prior, written
* permission. Application to host any of the material elsewhere can be made by
* contacting me at jhunter@idevelopment.info.
*
* I have made every effort and taken great care in making sure that the source
* code and other content included on my web site is technically accurate, but I
* disclaim any and all responsibility for any loss, damage or destruction of
* data or any other property which may arise from relying on it. I will in no
* case be liable for any monetary damages arising from such loss, damage or
* destruction.
*
* As with any code, ensure to test this code in a development environment
* before attempting to run it in production.
* =============================================================================
*/

import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Collections;
import java.util.Random;

/**
* -----------------------------------------------------------------------------
* The following class provides an example of storing and retrieving objects
* from a LinkedList.
*
* A List corresponds to an "ordered" group of elements where duplicates are
* allowed.
*
* A LinkedList is based on a double linked list where elements of the List are
* typically accessed through add() and remove() methods.
*
* LinkedList's give great performance on add() and remove() methods, but do not
* perform well on get() and set() methods when compared to an ArrayList.
*
* @version 1.0
* @author Jeffrey M. Hunter (jhunter@idevelopment.info)
* @author http://www.idevelopment.info
* -----------------------------------------------------------------------------
*/

public class LinkedListExample {

/**
* Provides an example of how to work with the LinkedList container.
*/
public void doLinkedListExample() {

final int MAX = 10;
int counter = 0;

System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Create/Store objects in an LinkedList container. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

List listA = new LinkedList();
List listB = new LinkedList();

for (int i = 0; i < MAX; i++) {
System.out.println(" - Storing Integer(" + i + ")");
listA.add(new Integer(i));
}

System.out.println(" - Storing String(Alex)");
listA.add("Alex");

System.out.println(" - Storing String(Melody)");
listA.add("Melody");

System.out.println(" - Storing String(Jeff)");
listA.add("Jeff");

System.out.println(" - Storing String(Alex)");
listA.add("Alex");

System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Retrieve objects in an LinkedList container using an Iterator. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

Iterator i = listA.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}


System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Retrieve objects in an LinkedList container using a ListIterator. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

counter = 0;
ListIterator li = listA.listIterator();
while (li.hasNext()) {
System.out.println("Element [" + counter + "] = " + li.next());
System.out.println(" - hasPrevious = " + li.hasPrevious());
System.out.println(" - hasNext = " + li.hasNext());
System.out.println(" - previousIndex = " + li.previousIndex());
System.out.println(" - nextIndex = " + li.nextIndex());
System.out.println();
counter++;
}


System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Retrieve objects in an LinkedList container using index. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

for (int j=0; j < listA.size(); j++) {
System.out.println("[" + j + "] - " + listA.get(j));
}


System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Search for a particular Object and return its index location. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

int locationIndex = listA.indexOf("Jeff");
System.out.println("Index location of the String \"Jeff\" is: " + locationIndex);


System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Search for an object and return the first and last (highest) index. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

System.out.println("First occurance search for String \"Alex\". Index = " + listA.indexOf("Alex"));
System.out.println("Last Index search for String \"Alex\". Index = " + listA.lastIndexOf("Alex"));


System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Extract a sublist from the main list, then print the new List. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

List listSub = listA.subList(10, listA.size());
System.out.println("New Sub-List from index 10 to " + listA.size() + ": " + listSub);


System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Sort the Sub-List created above. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

System.out.println("Original List : " + listSub);
Collections.sort(listSub);
System.out.println("New Sorted List : " + listSub);
System.out.println();


System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Reverse the Sub-List created above. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

System.out.println("Original List : " + listSub);
Collections.reverse(listSub);
System.out.println("New Reversed List : " + listSub);
System.out.println();


System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Check to see if the Lists are empty. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

System.out.println("Is List A empty? " + listA.isEmpty());
System.out.println("Is List B empty? " + listB.isEmpty());
System.out.println("Is Sub-List empty? " + listSub.isEmpty());


System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Clone the initial List. |");
System.out.println("| NOTE: The contents of the List are object references, so both |");
System.out.println("| of the List's contain the same exact object reference's. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

System.out.println("List A (before) : " + listA);
System.out.println("List B (before) : " + listB);
System.out.println("Sub-List (before) : " + listSub);
System.out.println();
System.out.println("Are List's A and B equal? " + listA.equals(listB));
System.out.println();
listB = new LinkedList(listA);
System.out.println("List A (after) : " + listA);
System.out.println("List B (after) : " + listB);
System.out.println("Sub-List (after) : " + listSub);
System.out.println();
System.out.println("Are List's A and B equal? " + listA.equals(listB));


System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Shuffle the elements around in some Random order for List A. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

System.out.println("List A (before) : " + listA);
System.out.println("List B (before) : " + listB);
System.out.println("Sub-List (before) : " + listSub);
System.out.println();
System.out.println("Are List's A and B equal? " + listA.equals(listB));
System.out.println();
Collections.shuffle(listA, new Random());
System.out.println("List A (after) : " + listA);
System.out.println("List B (after) : " + listB);
System.out.println("Sub-List (after) : " + listSub);
System.out.println();
System.out.println("Are List's A and B equal? " + listA.equals(listB));


System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Convert a List to an Array. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

Object[] objArray = listA.toArray();
for (int j=0; j < objArray.length; j++) {
System.out.println("Array Element [" + j + "] = " + objArray[j]);
}


System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Remove (clear) Elements from List A. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();

System.out.println("List A (before) : " + listA);
System.out.println("List B (before) : " + listB);
System.out.println();
listA.clear();
System.out.println("List A (after) : " + listA);
System.out.println("List B (after) : " + listB);
System.out.println();

}


/**
* Sole entry point to the class and application.
* @param args Array of String arguments.
*/
public static void main(String[] args) {
// LinkedListExample listExample = new LinkedListExample();
// listExample.doLinkedListExample();
}

}

Sunday, October 3, 2010

Java Code

View in edit mode to see the correct Spacing and Tabs
-----------------------------------------------------

http://www.javafaq.nu/java-example-code-90.html
//break;
//System.err ?
//"\n"



-------------------------------------------------------
How to call a method
------------------------------------------------------
// Zach Krizan

public class Main
{
// How to call a method

public static void main(String[] args)
{
int A [] = new int[10];
A[0] = 1000000;
//creates an array

methodA(A); //This runs methodA
//If there is something in the (), that means it will be used
// in the method
//This method is using an array - int[] B is an array
//All B's will be replaced with A's win this method runs
}


public static void methodA(int [] B) //creates methodA
//inside the () is what you need in order to run this method
//the "(int [] B)" means you must use an array for this method

// The B is just for this method
// All B's will be replaced with whatever you put in the () earlier
// So again... All B's will be replaced with A's

{
System.out.println("You have ran method A");
System.out.println("Your int is: "+B[0]);
}

}



-----------------------------------------------------
How to create a Class
-----------------------------------------------------
// Zach Krizan

//How to create a Class

public class Main
{


public static class ExampleClass
{
//creates class

public void methodA(int [] B)
{
//in methods all things in () will be replaced by whatever you put
// inside the () when you ran the method
//So all B's will be replaced by A's
System.out.println("You have ran the class 'Example Class'");
System.out.println("You have ran method A");
System.out.println("Your int is: "+B[0]);
}
}


public static void main (String[] args)
{
int[] A = new int[10];
A[0] = 1;

ExampleClass example = new ExampleClass();
example.methodA(A);
//creates new object in the class called 'example'
}
}



--------------------------------------------------------
How to run a class from a different page other than Main
--------------------------------------------------------

package javaapplication1;

// Zach Krizan

public class Main
{

// This program will run a class that is on a different page than Main
// The class is named page2 and must be created seperatley

public static void main(String[] args)
{
// This is the format to create a new object to run the class page2
page2 p2 = new page2();
p2.methodA();
// This runs methodA from page2

}

}

**************************************************
page 2, named: page2.java
**************************************************


package javaapplication1;


// Zach Krizan

public class page2
{
// ^^^ This is the correct format when you create a new class that is not
// on the Main page

public static void methodA()
{
System.out.println("You ran methodA() from page 2!");
}


}




-----------------------------------------------------
Sort using Array
-----------------------------------------------------
package javaapplication10;

/* Zach Krizan
* Data Structures
* September 20, 2010
*/


public class Main
{

// This program will generate 10 random numbers and show them before they are sorted
// Then it will show them sorted


public static void main(String[] args)
{
int A [] = new int[10];
populateA(A); //runs method populatA
System.out.println("Before Sorting:"); //display line
printA(A); // runs method printA

int key;
int i;



for (int j=1; j -1) && (A[i] > key))
{
A[i+1] = A[i];
i = i-1;
}
A[i+1] = key;

}

System.out.println("\n" +"After Sorting"); //displays line
printA(A); //runs method printA again and it will be sorted now
}


public static void printA (int [] B) //creates method printA
{
for (int i=0; i < i="0;">





-------------------------------------------
Stack Excercise, changing Infix to Postfix
-------------------------------------------


package program3stackexercise;


import java.util.Scanner;
import java.util.Stack;
import java.io.IOException;

/*Zach Krizan
*Data Structures
*Stack Exercise Program 3
*/

public class Main
{

/* This program will have the user enter an expression in infix form, which
* should just be letters of the alphabet a-z or A-Z and/or "/*-+",
* and it will return them in Postfix form.
* If the stack is empty it will present an error message.
* If the stack is over 5, it will present an error message and end
* the program.
*/

public static void main(String[] args)
{

System.out.println("Enter an expression in the Infix form:");
Scanner scan = new Scanner(System.in);

// Whatever expression the user enters will be equal to the string.
// called expression. It uses a scanner to find the string.
String expression = scan.nextLine();
new Infix2Postfix(expression);
// This runs the Infix2Postfix method with the string the user entered

}
}


***************************************
page 2 class named: Infix2Postfix.java
***************************************
package program3stackexercise;

import java.util.Scanner;
import java.util.Stack;
import java.io.IOException;
//Zach Krizan

public class Infix2Postfix
{
Stack stack;
String infixExp;
String postfixExp = "";

// creates a stack and to empty strings

public Infix2Postfix (String exp)
{
String str = "";
infixExp = exp;
stack = new Stack();

// creates another empty string
// changes exp to InfixExp
// creates the new stack


// this if statement checks to see if the stack is empty
// and will present an error message if it is
if (infixExp.length() == 0)
{
System.out.println("Error: Stack Empty");
System.out.println("Please enter an expression in Infix form");

}

// this if statement checks to see if the stack is above 5
// and will present an error message if it is.
// it will also end the program
if (infixExp.length() >= 6)
{
System.out.println("Error: Stack is Full");
System.out.println("5 is the maximum for the stack");
System.out.println("Program will now end");
System.exit(1);
}

// This for loop will run for how long the entered expression is.
// It will check all the inputs are a-z or A-Z.
// The string postfixExp will and the str to itself each time the
// loop runs.
for (int i=0;i {
str = infixExp.substring(i, i+1);
if(str.matches("[a-zA-Z]|\\d"))
postfixExp += str;
else if (isOperator(str))
{
//this if statement checks to see if the stack is empty
// then pushes the stack if it is
if (stack.isEmpty())
{
stack.push(str);
}
else
{
// this sets stackTop to whatever is on top of the stack
// by using the peek function
String stackTop= stack.peek();

// while getPrecedence(stackTop) = stackTop
// and the stack is not empty run this while loop
while (getPrecedence(stackTop, str).equals(stackTop) &&!
(stack.isEmpty()))
{
// postfixExp will be equal to itself + stack.pop
postfixExp += stack.pop();

//if stack is empty make stackTop = to whatever is
// on top of the stack by using the peek function
if (!(stack.isEmpty()))
stackTop = stack.peek();
}
//Push the stack
stack.push(str);
}
}
}
// presents the final postfix form
System.out.println("The postfix form is: "+postfixExp);
}


public boolean isOperator(String ch)
{
//Checks to see what mathmatical signs are used.
String operators = "*/%+-";

//if it is not = to -1 return true
if (operators.indexOf(ch) != -1)
return true;
else
return false;
}

private String getPrecedence(String op1, String op2)
{
//The "*/%" have a higher priority than the "+-"
String multiplicativeOps = "*/%";
String additiveOps = "+-";

if ((multiplicativeOps.indexOf(op1) != -1) &&
(additiveOps.indexOf(op2)!= -1))
return op1;
else if ((multiplicativeOps.indexOf(op2) != -1) &&
(additiveOps.indexOf(op1) != -1))
return op2;
else if((multiplicativeOps.indexOf(op1) != -1) &&
(multiplicativeOps.indexOf(op2) != -1))
return op1;
else
return op1;
}
}




==================================================================
Linked list exercise
=================================================================


package javaprogram4;

import java.util.Scanner;
import java.util.*;
import java.io.*;

public class Main
{

public static void main(String[] args)
{


String str = "yes";
while (str.equals("yes"))
{


List listA = new LinkedList();
//List listB = new LinkedList();
int num [] = new int[10];

for (int i = 0; i < 10; i++)
{

System.out.println("\n" + "Enter a number: ");
Scanner scan = new Scanner(System.in);

num[i] = scan.nextInt();
listA.add(new Integer(num[i]));

System.out.println("\n"+"The list is : "+listA);

System.out.println("Would you like to enter another number?");
System.out.println("Please enter 'yes'or 'no':");
System.out.println("");
Scanner scan2 = new Scanner(System.in);
String a = scan2.nextLine();

str = a;
if (str.equals("no"))
{
System.out.println("\n"+"The program will now end");
//You will know run Program 4b
System.exit(0);
}
}

}


}


}

==============================================
working on

package javaprogram4;

import java.util.Scanner;
import java.util.*;
import java.io.*;


public class Main
{
static void printList(ListNode start, int A) {
// Prints the items in the list to which start points.
// They are printed on one line, separated by spaces.

ListNode runner; // For running along the list.
runner = start;
while (runner != null) {
System.out.print(" " + runner.item);
runner = runner.next;
}
} // end printList()

public static void main(String[] args)
{




//System.out.println("I will print out a list");

ListNode list = null; // A list, initially empty.

int ct = 0; // How many lists have we processed?
int num = 0;
String str = "yes";

while (str.equals("yes"))
//for (int i =0; i<100;i++)
{
// Print the current list and its reverse. Then
// add a new node onto the head of the list before
// repeating.
ct++;

System.out.println("Enter a number: ");
Scanner scan = new Scanner(System.in);
num = scan.nextInt();



ListNode head = new ListNode(); // A new node to add to the list.
head.item = num;
head.next = list;
list = head;

System.out.print("The list is: ");
printList(list, num);
System.out.println();


System.out.println("Would you like to enter another number?");
System.out.println("Please enter 'yes'or 'no':");
System.out.println("");
Scanner scan2 = new Scanner(System.in);
String a = scan2.nextLine();
str=a;
if (str.equals("no"))
{
System.out.println("\n"+"The program will now end");
//You will know run Program 4b
//System.exit(0);
}
}

//program 4-b
/*
int part2=0;

System.out.println("Enter a number: ");
Scanner scanPart2 = new Scanner(System.in);
part2 = scanPart2.nextInt();

//search for the number
Iterator i = list.iterator();
while (i.hasNext())
{
System.out.println(i.next());
}

String wishDelete ="";

System.out.println("Would you like to delete it?");
System.out.println("Please enter 'yes' or 'no'");
Scanner scanDelete = new Scanner(System.in);
wishDelete = scanDelete.nextLine();
if (wishDelete.equals("yes"))
{
System.out.println("Deleting");
}
if (wishDelete.equals("no"))
{
System.out.println("Will not delete");
}
*/
}
}



================
page 2
=======================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaprogram4;

/**
*
* @author st_krizan
*/
public class ListNode
{
int item; // An item in the list.
ListNode next; // Pointer to the next node in the list.
}

Code Tutorial

HTML tutorial

>html<
>head<
>title< >/title<
>/head<

>body< >/body<

>/html<

//How you do a style tag
//style=""

//padding: 1px;
//background-image:url(yourImage.jpg);
//height: 50px;
//font-size: 40;
//margin:0px;
//text-align: right;
//background-position: repeat-y;
//background-color: white;
//border-bottom: 1px solid blue;
//font-size: 20;
//float: left;


>!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"<

>meta http-equiv="Content-type" content="text/html; charset=UTF-8"<
//Put this in the head tags, after the title


// Ordered list
// div tags use
// blockquote use