I gave out handouts of some programs that we used as examples to calculate Big-O. If you didn't get a handout, please print it from this page. Thanks.
ReadInfo.java
// Examples by Blanca.
// Spring 2000
import java.io.*;
class ReadInfo {
private BufferedReader br;
public ReadInfo() {
//InputStreamReader isr = new InputStreamReader(System.in);
//BufferedReader br = new BufferedReader(isr);
br = new BufferedReader(new InputStreamReader(System.in));
}
public String promptAndRead(String s) throws Exception {
System.out.print(s);
System.out.flush();
return br.readLine();
}
}
Vectors.java
// Examples by Blanca.
// Spring 2000
import java.io.*;
import java.util.*; // to get vectors to work
public class vectors {
public static void main()throws Exception {
String s;
File f = new File("algo.txt");
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
Vector v = new Vector();
Enumeration enum = v.elements();
s=br.readLine();
while(s != null){
v.addElement(s);
System.out.println(s);
s=br.readLine();
}// while -reading from file
// putting into vector
while (enum.hasMoreElements()){
s = (String) enum.nextElement();
System.out.println(s);
} // while -accessing the vector
// printing in the screen
System.in.read();
}// main method
} // class
Arrays.java
// Examples by Blanca.
// Spring 2000
import java.io.*;
class Arrays{
public static final int size = 10; //constant
public static void main(String[] arg)throws Exception{
String[] spell = new String[size];
int i = 0;
ReadInfo ri = new ReadInfo();
for(i=0; i< size; i++){
spell[i] = ri.promptAndRead("Type line " + i + "... ");
System.out.flush();
} //for
for(i=0; i< size; i++){
System.out.println(spell[i]);
} //for
System.in.read();
}
}
While5.java
// Examples by Blanca.
// Spring 2000
import java.io.*;
class While5 {
public static void main(String[] arg)throws Exception{
File f = new File("file.txt");
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String oneLine = "";
oneLine = br.readLine();
while(oneLine != null){
System.out.println(oneLine);
oneLine = br.readLine();
} // while
System.out.println("Done!!!");
System.in.read();
}
}
While2.java
// Examples by Blanca.
// Spring 2000
class While2 {
public static void main(String[] arg)throws Exception{
String s = "";
ReadInfo ri = new ReadInfo();
System.out.println("Please follow instructions carefully");
s = ri.promptAndRead("Type number 10... ");
while(!(s.equals("10"))){
s = ri.promptAndRead("I said type number ten... ");
}
System.out.println("Thanks!!");
System.in.read();
}
}
Pattern.java
import java.io.*;
class Pattern{
public Pattern(){
}
public void printRight(int spaces) throws Exception{
for(int j = 0; j< spaces; j++){
for(int i = 0; i< j; i++){
System.out.print(" ");
}
System.out.println("------");
}
}
public void printLeft(int spaces) throws Exception{
for(int j=0; j< spaces; j++){
for (int i=spaces; i >j; i--){
System.out.print(" ");
}
System.out.println("------");
}
}
} //main bracket closes
DrawPattern.java
// Examples by Blanca.
// Spring 2000
import java.io.*;
class DrawPattern{
public static final int amount = 5;
public static final int howManyTimes = 3;
public static void main(String arg[]) throws Exception{
Pattern print = new Pattern();
int i = 0;
for(i=0; i< howManyTimes; i++){
print.printRight(5);
print.printLeft(5);
}
System.in.read();
} //main method
} //class