Hanoi
package lab7;
public class Hanoi {
public static void main(String[] args)
{
recursive(3,'A','C','B');
}
public static void recursive(int n, char src, char aux, char dst)
{
if(n==0)
{
return;
}
else
{
recursive(n-1,src, dst, aux);
System.out.println("Move "+n+" from "+src+" to "+dst);
recursive(n-1,aux,src,dst);
}
}
}
LacRecursivsSqureSum
package Lab7;
public class LacRecursivsSqureSum
{
public static void main(String[] args)
{
System.out.println("squreSum()"+squreSum(5));
}
public static int squreSum(int n)
{
if(n==1)
{
return 1;
}
else
{
return (squreSum(n-1)+n*n);
}
}
}
LabModularSort
package lab7;
import javax.swing.JOptionPane;
public class LabModularSort {
private int arry[];
private static int x,L;
public LabModularSort( )
{
x=Integer.parseInt(JOptionPane.showInputDialog("Please enter the number of arrary:"));
this.arry = new int [x];
for (int r = 0; r < arry.length; r++)
{
arry[r]=Integer.parseInt(JOptionPane.showInputDialog("Please enter number:"));
}
}
public LabModularSort (int[] a)
{
this.arry = new int [a.length];
for(int r=0; r
{
arry[r]=a[r];
}
}
public static LabModularSort sort( LabModularSort newAry)
{
LabModularSort s = new LabModularSort ( newAry.arry);
int x=0;
System.out.print("original :");
for(int i=0;i
{
System.out.print(s.arry[i]+",");
if (i==s.arry.length-1)
System.out.println();
}
for(int cont1=0;cont1< newAry.arry.length;cont1++)
{
for (int cont2=cont1+1;cont2< newAry.arry.length;cont2++)
{
if (s.arry[cont1] > s.arry[cont2])
{
x = s.arry[cont1];
s.arry[cont1] = s.arry[cont2];
s.arry[cont2] = x;
}
}
}
System.out.print("sorting : ");
for(int i=0;i
{
System.out.print(+s.arry[i]+",");
}
System.exit(0);
return s;
}
}
----------------
package lab7;
public class LabModularSortDemo {
public LabModularSortDemo() {
}
public static void main(String[] args) {
LabModularSort labmodularsortdemo = new LabModularSort();
LabModularSort.sort(labmodularsortdemo);
// labmodularsortdemo.toString(labmodularsortdemo);
}
}
---
HW Square
package lab7;
import javax.swing.*;
public class ArraySquare {
private static int ary[];
private static int x,L;
public ArraySquare()
{
this.ary = new int [Integer.parseInt(JOptionPane.showInputDialog("Please enter the numbers of arrary elements:"))];
for(int r=0; r
{
ary[r]=Integer.parseInt(JOptionPane.showInputDialog("Please enter number:"));
}
}
public ArraySquare(int[] a)
{
this.ary = new int [a.length];
for(int r=0; r
{
ary[r]=a[r];
}
}
public static ArraySquare Square( ArraySquare newArrary)
{
ArraySquare square = new ArraySquare(newArrary.ary );
for (int i = 0; i< newArrary.ary.length;i++)
{
square.ary[i] = newArrary.ary[i] * newArrary.ary[i];
// System.out.print(ary[i]+", ");
}
return square;
}
public void toString (ArraySquare square)
{
for(int i=0;i
{
System.out.print(square.ary[i]+",");
}
System.exit(0);
}
}
---------
package lab7;
public class ArrarySquareDemo {
public static void main(String[] args) {
ArraySquare squareDemo = new ArraySquare();
ArraySquare.Square(squareDemo);
squareDemo.toString(squareDemo);
}
}
----
LabSorb
package lab7;
public class LabSort
{
public static void main(String[] args)
{
int [] sort = {1,3,5,4,9};
int x=0;
for(int cont1=0;cont1
{
for (int cont2=cont1+1;cont2
{
if (sort[cont1] > sort[cont2])
{
x = sort[cont1];
sort[cont1] = sort[cont2]; //可知i
sort[cont2] = x; //可知j
}
}
}
for(int i=0;i
{
System.out.print(sort[i]+",");
}
}
}