Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Multiplication table

public class FactorialDisplay


{

/**
* Constructor for objects of class FactorialDisplay
*/
public FactorialDisplay()
{
calculator = new FactorialCalculator();
}

/**
* Displays the factorial of a positive number less than 10 calculated by three different
methods.
* Should print three identical lines
* @param the integer whose factorial is to be calculated
*/
public void display(int n)
{
if(n > 0 && n < 10)
{
System.out.println("The factorial of " + n + " using a for loop is: " +
calculator.factorialByForLoop(n));
System.out.println("The factorial of " + n + " using a while loop is: " +
calculator.factorialByWhileLoop(n));
System.out.println("The factorial of " + n + " using a do..while loop is: " +
calculator.factorialByDoWhileLoop(n));
}
else
{
System.out.println("" + n + " is out of scope");
}
}
}
Magic square

public class MagicSquare { "); // for alignment


if (magic[i][j] < 100) System.out.print("
public static void main(String[] args) {  "); // for alignment
int N = Integer.parseInt(args[0]); System.out.print(magic[i][j] + " ");
if (N % 2 == 0) throw new }
RuntimeException("N must be odd"); System.out.println();
}
int[][] magic = new int[N][N];
}
int row = N-1; }
int col = N/2;
magic[row][col] = 1; output

for (int i = 2; i <= N*N; i++) { % java MagicSquare 3


if (magic[(row + 1) % N][(col + 1) % * 4 9 2 
N] == 0) { * 3 5 7 
row = (row + 1) % N; * 8 1 6 
col = (col + 1) % N; *
} * % java MagicSquare 5
else { * 11 18 25 2 9 
row = (row - 1 + N) % N; * 10 12 19 21 3 
// don't change col * 4 6 13 20 22 
} * 23 5 7 14 16 
magic[row][col] = i; * 17 24 1 8 15 
} *
* Limitations
// print results * -----------
for (int i = 0; i < N; i++) { * - N must be odd
for (int j = 0; j < N; j++) { *
if (magic[i][j] < 10) System.out.print("
Towers of Hanoi

package de.vogella.algorithms.towersofhanoi;

/**
* Towers of Hanoi
* Pole are labeled 1, 2,3
* Each disk is also labeled
* @author Lars Vogel
*
*/
public class TowersOfHanoi {
public static void move(int n, int startPole, int endPole) {
if (n== 0){
return; 
}
int intermediatePole = 6 - startPole - endPole;
move(n-1, startPole, intermediatePole);
System.out.println("Move " +n + " from " + startPole + " to " +endPole);
move(n-1, intermediatePole, endPole);
}

public static void main(String[] args) {


move(5, 1, 3);
}

}
Anagrams

import java.util.*; 
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class AnagramDriver {


public static void main(String args[]) {
String phraseOne;//first phrase
String phraseTwo;//second phrase

System.out.println("Enter a word");
Scanner scanOne = new Scanner(System.in);//scanner to scan string entered
phraseOne = scanOne.nextLine();

Scanner scanTwo = new Scanner(System.in);


phraseTwo = scanTwo.nextLine();

System.out.println(phraseOne+" "+phraseTwo);
Binary Search

import java.util.Arrays;

public class BinarySearch {

// a working recursive version


public static int search(String key, String[] a) {
return search(key, a, 0, a.length);
}
public static int search(String key, String[] a, int lo, int hi) {
// possible key indices in [lo, hi)
if (hi <= lo) return -1;
int mid = lo + (hi - lo) / 2;
int cmp = a[mid].compareTo(key);
if (cmp > 0) return search(key, a, lo, mid);
else if (cmp < 0) return search(key, a, mid+1, hi);
else return mid;
}

// whitelist, exception filter


public static void main(String[] args) {
In in = new In(args[0]);
String s = in.readAll();
String[] words = s.split("\\s+");
System.err.println("Done reading words");

// sort the words (if needed)


Arrays.sort(words);
System.err.println("Done sorting words");

// prompt user to enter a word and check if it's there


while (!StdIn.isEmpty()) {
String key = StdIn.readString();
if (search(key, words) < 0) StdOut.println(key);
}
}
}
Activities in Data Structure

Present by:
Padilla Jovelle G.
BT-202P
Sorting programs

import java.io.*;
class A
{
public static void main(String args[])throws Exception
{
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter How many numbers to sort ");
String s=dis.readLine();
int l=Integer.valueOf(s).intValue();
System.out.println("Please Enter "+l+" integers to sort");
int a[]=new int[l];
for (int i=0;i {
s=dis.readLine();
int m=Integer.valueOf(s).intValue();
a[i]=m;
}
System.out.println("Here is the sorted numbers ");
for(int j=0;j for(int k=j+1;k {
if(a[j] {
int t;
t=a[j];
a[j]=a[k];
a[k]=t;
}
}
for(int x=0;x System.out.println(a[x]);

}
}

You might also like