Chris Pollett >Old Classes >
CS151

( Print View )

Student Corner:
  [Grades Sec5]

  [Submit Sec5]

  [Email List Sec5]

  [
Lecture Notes]

Course Info:
  [Texts & Links]
  [Topics]
  [Grading]
  [HW Info]
  [Exam Info]
  [Regrades]
  [Honesty]
  [Additional Policies]
  [Announcements]

HW Assignments:
  [Hw1]  [Hw2]  [Hw3]
  [Hw4]  [Hw5]

Practice Exams:
  [Mid1]  [Mid2]  [Final]

                           












HW1 Solutions Page

Return to homework page.

To begin here are my solutions to 2.1, 2.4, as well as the state diagram I had for ByeStringChecker.

Solution to Problem 2.1 from the book Solution to Problem 2.4 from the book State diagram for ByeStringChecker

ByeStringChecker.java

//ByeStringChecker.java
import java.util.*;

/**
   Solution to first homework for CS151. This program is run from the
   command line with a line like:
   java ByeStringChecker string1 string2
   
   It outputs yes if string2 is a string consisting of
      bye followed by zero or more repetitions of the first string 
      followed by bye; otherwise it outputs no. 

   @author C. Pollett
   @version 7.9.2005
*/

public class ByeStringChecker
{
   /**
      Checks if the sequence of characters of from the firstString beginning at firstPosition
      to the end of the firstString match the characters of the secondString beginning at
      the second position.
      
      @param firstPosition -starting position in first string to match from
      @param firstString - a string to match from
      @param secondPosition - starting position in second string to match from
      @param secondString -  a string to match from
      
      @return true- if there is a match
   */
   public static boolean matchStringsFromPosition(int firstPosition, String firstString, int secondPosition, String secondString)
   {
         int secondLength = secondString.length();
         int firstLength = firstString.length();
         
         if(secondLength - secondPosition < firstLength - firstPosition) return false;
            //second string too short to match that portion of the first string.
         
         boolean passCheck = true;
         
         for(int i = firstPosition, j=secondPosition; i < firstLength; i++, j++)
         {
            if(firstString.charAt(i) != secondString.charAt(j))
            {
               passCheck=false;
               break;
            }
         }
         
         return passCheck;
   }
   
   /**
      Checks if the second supplied String is a string consisting of
      bye followed by zero or more repetitions of the first string followed by bye

      @param firstString - String to check if repeated in second string
      @param secondString - string to check if of form described above
      
      @return -whether or not the second string has the correct format
   */
   public static boolean checkStrings(String firstString, String secondString)
   {
      if(!matchStringsFromPosition(0, START_WORD, 0, secondString)) return false;
      
      int i = START_WORD.length();

      boolean stillChecking = true;
      boolean passCheck = true;
      
      while(stillChecking)
      {
         if(matchStringsFromPosition(0, END_WORD, i, secondString) &&
            i + END_WORD.length() == secondString.length())
         {
            stillChecking = false;
         }
         else if(!matchStringsFromPosition(0, firstString, i, secondString))
         {
            stillChecking = false;
            passCheck = false;            
         }
         
         i += firstString.length();
      }
      
      return passCheck;
   }

   /**
      prints error message if the user didn't supply enough command-line
      arguments to this program when it was run
   */
   protected static void tooFewArgumentsError()
   {
      String message = "ByeStringChecker.\n" +
         "Is run from the command line with a line like:\n" +
         "java ByeStringChecker string1 string2\n" +
         "It requires exactly two arguments. It outputs\n" +
         "Yes or No depending on if string2 is of the form\n" +
         "bye followed by zero or more repetitions of string1\n" +
         "followed by bye again.";
         
      System.err.println(message);
   }

   /**
      Driver method for this program. After check if
      args[1] is a string of the form bye followed by
      0 or more repetitions of args[0] followed by bye
      again, it outputs yes or no.
   
      @param args -- array of user supplied command line arguments.
         Should contain two strings which will be checked by this program 
   */      
   public static void main (String args[]) 
   {
      String a[] = new String[1000000];
      if(args.length != 2)
      {
         tooFewArgumentsError();
         System.exit(1);
      }
      if(checkStrings(args[0], args[1]))
      {
         System.out.println("Yes");
      }
      else
      {
         System.out.println("No");
      }
   }
    

   
   public static final String START_WORD = "bye"; //first token to eat
   public static final String END_WORD = "bye"; //last token to eat
   
}

Return to homework page.