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

No comments:

Post a Comment