|
Java Applications
- 02: read text from a file and prints the lines on the screen
- 03: read text from a file and prints the lines into a new file
- 04: read text from a file and prints the lines into a new file, backwards
- 06: sort random numbers and store in array using bubble/selection sort
- tiktaktoe: tic tac toe game
- recursive n iteration: recursion vs iteration
- link list example: linked list example
- queue: queue example
- list: list example
- list2: another list example
- intlist: another list example
- node: basics of a linked-list, queues, trees, etc.
- queuenode: nodes for queues
- treenode: nodes for binary trees
- banksimulation: simulation of a real-time bankline
- bubblesort SORTS: Bubble Sort via arrays
- insertionsort SORTS: Insertion Sort via arrays
- mergesort SORTS: Merge Sort via arrays
- quicksort SORTS: Quick Sort via arrays
VALUABLE BEGINNER's NOTES:
+++INPUT & OUTPUT
//to input from the keyboard
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your word: ");
String word = br.readLine(); //this statement will prompt the user to input from keyboard
//buffer to input a file
BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream(
new File("file.txt"))));
String word = br.readLine(); //this statement will read the first line in file.txt
word = br.readLine(); //br.readLine() will read the next line in file.txt
//to write to a file
PrintWriter pw = new PrintWriter(new FileWriter(new File("file.txt")), true);
pw.println("text"); //this statement will print text into first line of file.txt
pw.println("more text"); //'' '' print text into second line '' ''
+++ARRAYS & VECTORS
//initializing an array of integers directly
int[] x = new int[size of array]
OR
int x[] = new int[size of array]
NOTE: the last element is int[size of array - 1]
//storing elements with integers
x[0] = 5;
x[1] = 2;
x[2] = 1;
...
//initializing an array of integers + storing
int[] x = {5, 4, 3, 2, 1};
~then:
x[0] = 5;
x[1] = 4;
x[2] = 3;
...
VISUALS: [5]
[4]
[3]
[2]
[1]
//returns the size of array x
x.length;
//you can store the length in an int
int size = x.length;
//initializing a 2d array of integers directly
int[][] x = new int[row size][column size]
x[row][column] = ...;
//initializing a 2d array of integers + storing
int[][] x = { {1,2,3} , {4,5,6} };
~then:
x[0][0] = 1;
x[0][1] = 2;
x[0][2] = 3;
x[1][0] = 4;
...
VISUALS: [1][2][3]
[4][5][6]
//to create vectors
Vector v = new Vector();
//this is the special way to use .addElement() to add integers
int i = 1;
v.addElement(new Integer(i));
v.addElement(new Integer(i++));
v.addElement(new Integer(i++));
//enumeration is used to read the vector
Enumeration enum = v.elements();
while (enum.hasMoreElements()) {
Integer integer = (Integer)enum.nextElement();
System.out.println(integer.intValue());
}
//another example of enumeration
...
Enumeration enums2 = v.elements();
while (nums.hasMoreElements()) {
entries++;
//.intValue() is used to change stuff to an int
total = total + ((Integer) enums2.nextElement()).intValue();
}
//another example of enumeration
...
int maxSoFar = Integer.MIN_VALUE;
Enumeration enums3 = v.elements();
while (enums3.hasMoreElements()) {
Integer number = (Integer) enums3.nextElement();
if (number.intValue() > maxSoFar) {
maxSoFar = numInt.intValue()
}
}
//another way to get around enumeration
...
int maxSoFar = Integer.MIN_VALUE;
// .size() [vectors] <=> .length [arrays]
for (int i = 0; i < v.size(); i++) {
// .elementAt(i) [vectors] <=> int[i] [arrays]
Integer number = (Integer) v.elementAt(i);
if (number.intValue() > maxSoFar) {
maxSoFar = numInt.intValue();
}
}
+++CASTING (changing String to int, String to double....etc)
//from int to double
int num1 = 1;
double num2 = (double) num1; //num2 = 1.0;
//from double to int
double num3 = 1.6;
int num4 = (int) num3; //num4 = 1; (doesn't do any rounding off, takes integer part of #)
//from String to int (note: String must contain numbers in order for this to work properly)
String s = "24";
int num5 = Integer.parseInt(s);
//from String to double
String s = "32";
double num6 = Double.parseDouble(s); //num6 = 32.0;
...there are much more here.
+++STRINGS
//creating a String
String word = "text";
//returns the length of String s
s.length();
//using .substring(int beginning, int end);
String word = "satchi";
String letter1 = word.substring(0, 1); //letter1 = "s";
String letter2 = word.substring(1, 2); //letter2 = "a";
String letter3 = word.substring(2, 3); //letter3 = "t";
..
String letter6 = word.substring(5, 6); //letter6 = "i";
String letter6 = word.substring(5, word.length()); //letter6 = "i";
//using .compareTo(String s);
String word = "oranges";
word.compareTo("pine"); //returns a negative int because "oranges" is before "pine"
word.compareTo("apples"); //returns a positive int because "oranges" is after "apples"
word.compareTo("oranges"); //returns 0 because they're equal
//using .indexOf(String s);
String word = "oranges";
word.indexOf("pine"); //returns -1 because none of the letters of "oranges" match "pine"
word.indexOf("oranges"); //returns some positive int
+++RANDOM INTs
int[] array = new int[100];
Random num = new Random();
//this loop will fill in array with random integers ranging from 0 to 10000
for (int i = 0; i < array.length; i++) {
int rnum = num.nextInt(10000);
array[i] = rnum;
}
+++RANDOM STRINGS
see here
+++MISC (UNCATEGORIZED):
//indexOf() example in arrays
public void search(String word, String file, String[] holdLines) {
int fileLineNum;
for (int num = 0; holdLines.length > num; num++) {
if (holdLines[num].indexOf(word) != -1) {
fileLineNum = num + 1;
System.out.println("Line #" + fileLineNum + " of " + file + " :");
}
}
}
//bubble sort via arrays
public void bubbleSort(int[] numArray) throws IOException {
int count=0;
int leftNum, rightNum, temp;
int last = numArray.length;
for ( ; count < numArray.length - 1; count++) {
//initial element place to start for each loop
leftNum= 0;
rightNum = 1;
for ( ; rightNum < last; rightNum++) {
if (numArray[leftNum] > numArray[rightNum]) {
temp = numArray[leftNum];
numArray[leftNum] = numArray[rightNum];
numArray[rightNum] = temp;
}
printArray(numArray);
leftNum++;
last = last ?1;
}
}
}
//binary search via Vectors
boolean bsearch (Vector v, String s) {
int left = 0;
int right = v.size();
if (v.size() == 0) {
return false;
} else {
while (left != right-1) {
int mid = (right + left) / 2;
String sm = (String) v.elementAt(mid);
if (sm.compareTo(s) < 0) {
left = mid;
} else {
if (sm.compareTo(s) > 0) {
right = mid;
} else{
left = mid;
right = mid + 1;
}
}
return s.equals((String)v.elementAt(left));
}
//OOP example
public class Person {
String name; //instance variables
int age;
public Person (String n, int a) {
name = n;
age = a;
}
void printPerson() {
System.out.print(“Hi, my name is ?+ name);
System.out.println(? I am ?+ age + ?years old.?;
}
public static void main (String[] args) {
Person p = new Person(“Luke? 50);
p.printPerson();
Person p = new Person(“Laura? 35);
p.printPerson();
}
}
OUTPUT: Hi, my name is Luke. I am 50 years old.
Hi, my name is Laura. I am 35 years old.
DEFINITIONS:
--steps in system development
-design phase: defining a collection of classes and their interactions to satisfy the specifications
-implementation: constructing the software modules that make up the system
-testing: ensuring that the modules conform to the specifications
--typical fundamental subsystems:
-interface
-model
-data management
--abstract class:
-a class that is not intended to be instantiated
-servers as a foundation to create subclasses
-non abstract classes should be called concrete
--accessibilty:
-public: accessible everywhere
-private: attribute of a feature that is accessible in that class ONLY
-protected: attribute of a feature that makes it accessible in subclasses
-restricted: attribute of a feature that makes it accessible to all class members of same package
--inheritance:
-superclass: the relationship of a class to class that extends it; more abstract
-subclass: the relationship of a class to class that it extends; less abstract, more concrete
--keyword “super?
-a superclass constructor name: super(arguments)
-if no constructor is called for a superclass/no const.
of same class is called, this is assumed to begin with
--overriding: providing an alternative iimplementation of an inherited method
|
|