Professional Documents
Culture Documents
Multiplication Table
Multiplication Table
/**
* 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
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);
}
}
Anagrams
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
System.out.println("Enter a word");
Scanner scanOne = new Scanner(System.in);//scanner to scan string entered
phraseOne = scanOne.nextLine();
System.out.println(phraseOne+" "+phraseTwo);
Binary Search
import java.util.Arrays;
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]);
}
}