A2Q2.java - COMP 1020 Summer 2017(C Computer Science University of Manitoba Assignment 2 Question 2 A2Q2.java import java.io import java.util.Scanner

A2Q2.java - COMP 1020 Summer 2017(C Computer Science...

This preview shows page 1 - 2 out of 2 pages.

/* COMP 1020 Summer 2017* (C) Computer Science, University of Manitoba* Assignment 2 Question 2* A2Q2.java*/import java.io.*;import java.util.Scanner;public class A2Q2{// class constantprivate static final int SIZE = 20;private static final int NOT_FOUND = -1;// instance variablesprivate String [] words;private String infile;private String outfile;private int numWords;public A2Q2(String infile, String outfile){this.infile = infile;this.outfile = outfile;words = new String[SIZE];numWords = 0;}// constructorprivate void insert(String newWord){// see if we need to expand the array firstif(numWords == words.length){String [] newWords = new String[2 * words.length];for(int i = 0; i < words.length; i++)newWords[i] = words[i];words = newWords;}// use an loop to move strings to make room for the new itemint pos = numWords;while(pos > 0 && words[pos-1].compareTo(newWord) > 0){words[pos] = words[pos-1];pos--;}// whilewords[pos] = newWord;numWords++;}// insertprivate int indexOf(String newWord){int result = NOT_FOUND;int lo = 0;int hi = numWords - 1;int mid = 0;while(result == NOT_FOUND && hi >= lo){mid = lo + (hi - lo)/2;
Background image
Image of page 2

You've reached the end of your free preview.

Want to read both pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture