Second Homework Answers - Interfaces, Exceptions, Stream I/O
public interface Collection { public boolean isFull(); public boolean add(Object obj); public boolean isIn(Object obj); }
public class myCollect implements Collection { Object[] elements; private int numElements; public myCollect() { elements = new Object[100]; numElements = 0; } public boolean isFull() { if (numElements == elements.length) return true; return false; } public boolean add(Object newObj) { if (this.isFull()) return false; elements[numElements] = newObj; numElements++; return true; } public boolean isIn(Object obj) { for (int i = 0 ; i < numElements ; i++) if (elements[i].equals(obj)) return true; return false; } }
public static boolean addSomething(Collection col, Object addit) { PrintWriter stdout = new PrintWriter(System.out,true); boolean ok; ok = col.add(addit); if (!ok) stdout.println("Error, collection is full"); return ok; }
Arithmetic Error
string error Error in number format Done with A
Error in number format Done with A
Error occurred: java.lang.ArrayIndexOutOfBoundsException
array error Error in number format Done with A
public static void split(String infile,String outfile) { PrintWriter stdout = new PrintWriter(System.out,true); try { PrintWriter fileout = new PrintWriter(new FileWriter(outfile),true); BufferedReader filein = new BufferedReader(new FileReader(infile)); String instr; int num; StringTokenizer stok; while ((instr = filein.readLine()) != null) { stok = new StringTokenizer(instr); while (stok.hasMoreTokens()) { num = Integer.parseInt(stok.nextToken()); fileout.println(num); } } fileout.close(); } catch (IOException ioe) { stdout.println("Exception occurred in split: " + ioe); System.exit(-1); } }
public class InOutDouble { public static void main(String[] args) { PrintWriter stdout = new PrintWriter(System.out,true); String instr; try { BufferedReader filein = new BufferedReader(new FileReader(args[0])); instr = filein.readLine(); // loop till EOF (readLine returns null on EOF) while (instr != null) { try { double dnum = new Double(instr).doubleValue(); stdout.println(dnum); } catch(NumberFormatException e) { } instr = filein.readLine(); } } catch(FileNotFoundException e) { stdout.println("File " + args[0] + " not found"); } catch(IOException e) { stdout.println("I/O Error"); } } }
import java.io.*; import java.util.Vector; import java.util.StringTokenizer; public class WordsPrint { public static void main(String[] args) { String word; String line; Vector allWords = new Vector(); StringTokenizer lineWords; PrintWriter stdout = new PrintWriter(System.out,true); try { BufferedReader filein = new BufferedReader( new FileReader("words.in")); line = filein.readLine(); while (line != null) { lineWords = new StringTokenizer(line); while (lineWords.hasMoreTokens()) { word = lineWords.nextToken(); allWords.addElement(word); } line = filein.readLine(); } } catch(IOException e) { stdout.println("IO Error occurred"); System.exit(-1); } stdout.println("Words that contain b or B:"); for (int i = 0 ; i < allWords.size() ; i++) { word = (String) allWords.elementAt(i); if (word.indexOf('b') != -1 || word.indexOf('B') != -1) stdout.println(word); } stdout.println("Words that end in s:"); for (int i = 0 ; i < allWords.size() ; i++) { word = (String) allWords.elementAt(i); if (word.endsWith("s")) stdout.println(word.substring(0,word.length()-1)); } } }
Email Me | Office Hours | My Home Page | Department Home | MCC Home Page