Sieve public class sieve public static void mainString
Sieve public class sieve { public static void main(String args[]) { int max = 100; boolean prime[] = new boolean[max+1]; int n, m; // initialize the array (entries 0, 1 unused) for (n = 2; n <= max; n++) { prime[n] = true; } // cross out multiples of uncrossed numbers for (n = 2; n <= max; n++) { if (prime[n]) { for (m = 2*n; m <= max; m += n) { prime[m] = false; } } } // print out primes: uncrossed numbers for (n = 2; n <= max; n++) { if (prime[n]) { System. out. print(n+" "); } } System. out. println(); } }
Queue class Queue { char q[]; // this array holds the queue int putloc, getloc; // the put and get indices Queue(int size) { q = new char[size+1]; // allocate memory for queue putloc = getloc = 0; } boolean is. Empty(){ if (getloc == putloc) return true; else return false; } boolean is. Full(){ if (putloc == q. length-1) return true; else return false; }
// put a characer into the queue void put(char ch) { if (is. Full()) { System. out. println(" -- Queue is full. "); return; } putloc++; q[putloc] = ch; } // get a character from the queue char get() { if (is. Empty()){ System. out. println(" -- Queue is empty. "); return (char) 0; } getloc++; return q[getloc]; } }
// Demonstrate the Queue class Queue. Demo { public static void main(String args[]) { Queue big. Q = new Queue(100); Queue small. Q = new Queue(4); char ch; int i; System. out. println("Using big. Q to store the alphabet. "); // put some numbers into big. Q for(i=0; i < 26; i++) big. Q. put((char) ('A' + i)); // retrieve and display elements from big. Q System. out. print("Contents of big. Q: "); for(i=0; i < 26; i++) { ch = big. Q. get(); if(ch != (char) 0) System. out. print(ch); } System. out. println("n");
System. out. println("Using small. Q to generate erros. "); // Now, use small. Q to generate some errors for(i=0; i < 5; i++) { System. out. print("Attempting to store " + (char) ('Z' - i)); small. Q. put((char) ('Z' - i)); System. out. println(); } System. out. println(); // more errors on small. Q System. out. print("Contents of small. Q: "); for(i=0; i < 5; i++) { ch = small. Q. get(); if(ch != (char) 0) System. out. print(ch); } } }
Stack class Stack { int st[]; // this array holds the stack int sp; // the stack pointer index Stack(int size) { st = new int[size+1]; // allocate memory for stack sp = 0; } boolean is. Full(){ if (sp == (st. length-1)) return true; else return false; } boolean is. Empty(){ if (sp == 0) return true; else return false; }
void push(int n) { if(is. Full()) { System. out. println(" -- Stack is full. "); return; } sp++; st[sp] = n; } int pop() { int top; if(is. Empty()) { System. out. println(" -- Stack is empty. "); return -1000; } top = st[sp]; sp--; return top; } }
// Demonstrate the Stack class Stack. Demo { public static void main(String args[]) { Stack st 1 = new Stack(5); int i, n; System. out. println("Using stack st 1 to store numbers. "); // put some numbers into st 1 for(i=1; i < 7; i++) st 1. push(i); // pop and display elements from st 1 System. out. println("Contents of st 1: "); for(i=1; i < 7; i++) { n = st 1. pop(); if (n != -1000) System. out. println(n); } } }
- Slides: 8