Prof. Janet Brown-Sederberg
Computer Technology &
Information Management
Technology Building Room 105
508.588.9100 x 1630

jbrown-sederberg@massasoit.mass.edu

Advanced Java: Spring 2008

  Class Notes

On this page
Links ||Common DOS Commands || Sorting Algorithms || Loading Compiler
||
How to Compile/Run Programs

Links
java.sun.com (Sun website)
www.cs.armstrong.edu/liang/intro6e (text author web site)
mazeworks.com (games in Java)
leepoint.net (Java Tutorial)
Common DOS Commands
Command Action
cd directory name changes to named directory (must be a directory within the current directory
cd\ changes directory to root directory
cd.. changes directory to directory one step up in hierarchy
dir lists contents of the current directory
dir/p lists contents of the current directory, stops scroll off screen
edit turns on dos editor
exit exits DOS
java filename runs file of filename
javac filename.java compiles file of filename.java
mkdir directoryname creates a new directory of directory name
rmdir directoryname remove directory of directory name
set displays the environmental variables
path displays the path
classpath displays the class path

NB: The following would all be on one line

set CLASSPATH=
%CLASSPATH%;
C:\directoryname;

Explanation of the above:

set --> command to establish an environmental variable

CLASSPATH=%CLASSPATH%; --> retains the old class path

C:\directoryname; --> adds a new class path, if there are additional path they are to delimited by a ";"
eg. C:\directoryname; C:\directoryname; C:\directoryname;

to set the class path where directoryname is the name of the directory where the classes will be saved (this is the path to the directory where the classes will be stored - therefore the path can be any number of levels deep - each level to be indicated by a "\" eg. C:\level1\level2\level3)

This presumes that you have or will create the directories noted in the class path. Creating directories is done through the mkdir command above.

Return to Top of Page

Sorting Algorithms
Algorithm Efficiency Efficiency of sorting algorithms
Sorting Algorithms Java Applets Demonstrations of several sorting algorithms
Selection Sort The selection sort algorithm is in many ways similar to the Simple Sort and Bubble Sort algorithms. Rather than swapping neighbors continously as the algorithm traverses the (sub)array to be sorted, as done in the Bubble Sort case, this algorithm finds the MINIMUM element of the (sub)array and swaps it with the pivot (or "anchor") element. This algorithm sorts in ascending order - i.e., smaller elements move to the front of the list.
Quick Sort Quicksort is a very efficient sorting algorithm invented by C.A.R. Hoare. It has two phases: the partition phase and the sort phase. As we will see, most of the work is done in the partition phase - it works out where to divide the work. The sort phase simply sorts the two smaller problems that are generated in the partition phase. This makes Quicksort a good example of the divide and conquer strategy for solving problems. (You've already seen an example of this approach in the binary search procedure.) In quicksort, we divide the array of items to be sorted into two partitions and then call the quicksort procedure recursively to sort the two partitions, ie we divide the problem into two smaller ones and conquer by solving the smaller ones.
Bubble Sort

This algorithm sorts in ascending order - i.e., smaller elements move to the front of the list.

Pseudocode:

function bubble_sort(list L, number listsize)
loop
has_swapped := 0 //reset flag
for number i from 1 to (listsize - 1)
if L[i] > L[i + 1] //if they are in the wrong order
swap(L[i], L[i + 1]) //exchange them
has_swapped := 1 //we have swapped at least once, list may not be sorted yet
endif
endfor
//if no swaps were made during this pass, the list has been sorted
if has_swapped = 0
exit
endif
endloop
endfunction

Insertion Sort This is a sorting algorithm that in the "worst case" behaves like the more inefficient Bubble Sort and Selection Sort Algorithms, but in many "average cases" performs better. This algorithm sorts in ascending order - i.e., smaller elements move to the front of the list.
Merge Sort This algorithm sorts in ascending order - i.e., smaller elements move to the front of the list.

Return to Top of Page

Loading Compiler

I recommend that you download the Java SE compiler from the Sun web site as demonstrated in class or select one of the IDE linked through the text authors website.

Return to Top of Page

How to Compile/Run Programs


General Compile/Run Instructions

It is easiest to work from the C drive at Home and the G drive on Campus. If you work from the C Drive on campus when you are done working save your files on the A or other secure drive for later use (the C drives are clened each night). Remember if you are using any of the methods of a class you must have a copy of that class file in the same directory as is the file you want to compile).

To Compile - when both the compiler and the file to be compiled on the C drive:

1. Get to the C prompt:

C:\>

2. Enter the path to the instruction to compile the file (with the .java extension) and the name of the file to compile:

C:\>pathToThe Instruction\nameOfFileToCompile.java

For example:

If the path the instruction to compile is located on the C drive at the following location:

Java5\bin\javac

And the name of the file you want to compile is:

nameOfFileToCompile.java

Then you would enter the following after the C prompt (note - there is a space between the pathToTheInstruction and the nameOfFileToCompile):

Java5\bin\javac nameOfFileToCompile.java

So the line on the screen would look like this:

C:\>Java5\bin\javac nameOfFileToCompile.java

To Run - when both the compiler and the file to be run on the C drive:

1. Get to the C prompt:

C:\>

2. Enter the path to the instruction to run the file and the name (with no extension) of the file to run:

C:\>pathToTheInstruction nameOfFileToRun

For example:

If the path the instruction to run is located on the C drive at the following location:

Java5\bin\java

And the name of the file you want to run is:

nameOfFileToRun

Then you would enter the following after the C prompt (note - there is a space between the pathToTheInstruction and the nameOfFileToRun):

Java5\bin\java nameOfFileToRun

So the line on the screen would look like this:

C:\>Java5\bin\javac nameOfFileToRun


 

Any fool can know. The point is to understand. - Albert Einstein